Make convert_to_int public (#818)

This commit is contained in:
Victor Zverovich 2018-08-22 07:40:06 -07:00
parent ba95e36a58
commit 987514761e
5 changed files with 32 additions and 28 deletions

View File

@ -98,7 +98,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
-Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-reserved-id-macro
-Wno-global-constructors -Wno-disabled-macro-expansion
-Wno-switch-enum -Wno-documentation-unknown-command
-Wno-gnu-string-literal-operator-template -Wno-unused-member-function
-Wno-unused-member-function
-Wno-format-nonliteral -Wno-missing-noreturn -Wno-undefined-func-template
-Wno-shadow -Wno-sign-conversion -Wno-used-but-marked-unused
-Wno-covered-switch-default -Wno-missing-variable-declarations
@ -108,13 +108,17 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
check_cxx_compiler_flag(-Wno-zero-as-null-pointer-constant HAS_NULLPTR_WARNING)
if (HAS_NULLPTR_WARNING)
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wno-zero-as-null-pointer-constant)
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
-Wno-zero-as-null-pointer-constant)
endif ()
check_cxx_compiler_flag(-Wno-gnu-string-literal-operator-template HAS_GNU_UDL_WARNING)
if (HAS_GNU_UDL_WARNING)
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS} -Wno-gnu-string-literal-operator-template)
endif ()
endif ()
check_cxx_compiler_flag(
-Wno-gnu-string-literal-operator-template HAS_GNU_UDL_WARNING)
if (HAS_GNU_UDL_WARNING)
set(PEDANTIC_COMPILE_FLAGS ${PEDANTIC_COMPILE_FLAGS}
-Wno-gnu-string-literal-operator-template)
endif ()
if (MSVC)

View File

@ -341,6 +341,13 @@ class basic_format_args;
template <typename T, typename Char = char, typename Enable = void>
struct formatter;
template <typename T, typename Char, typename Enable = void>
struct convert_to_int {
enum {
value = !std::is_arithmetic<T>::value && std::is_convertible<T, int>::value
};
};
namespace internal {
/** A contiguous memory buffer with an optional growing ability. */
@ -490,13 +497,6 @@ FMT_CONSTEXPR bool is_arithmetic(type t) {
return t > internal::none_type && t <= internal::last_numeric_type;
}
template <typename T, typename Char, bool ENABLE = true>
struct convert_to_int {
enum {
value = !std::is_arithmetic<T>::value && std::is_convertible<T, int>::value
};
};
template <typename Char>
struct string_value {
const Char *value;

View File

@ -71,15 +71,6 @@ class is_streamable {
static const bool value = result::value;
};
// Disable conversion to int if T has an overloaded operator<< which is a free
// function (not a member of std::ostream).
template <typename T, typename Char>
class convert_to_int<T, Char, true> {
public:
static const bool value =
convert_to_int<T, Char, false>::value && !is_streamable<T, Char>::value;
};
// Write the content of buf to os.
template <typename Char>
void write(std::basic_ostream<Char> &os, basic_buffer<Char> &buf) {
@ -106,6 +97,15 @@ void format_value(basic_buffer<Char> &buffer, const T &value) {
}
} // namespace internal
// Disable conversion to int if T has an overloaded operator<< which is a free
// function (not a member of std::ostream).
template <typename T, typename Char>
struct convert_to_int<T, Char, void> {
static const bool value =
convert_to_int<T, Char, int>::value &&
!internal::is_streamable<T, Char>::value;
};
// Formats an object of type T that has an overloaded ostream operator<<.
template <typename T, typename Char>
struct formatter<T, Char,

View File

@ -851,15 +851,15 @@ TEST(UtilTest, ReportWindowsError) {
enum TestEnum2 {};
TEST(UtilTest, ConvertToInt) {
EXPECT_FALSE((fmt::internal::convert_to_int<char, char>::value));
EXPECT_FALSE((fmt::internal::convert_to_int<const char *, char>::value));
EXPECT_TRUE((fmt::internal::convert_to_int<TestEnum2, char>::value));
EXPECT_FALSE((fmt::convert_to_int<char, char>::value));
EXPECT_FALSE((fmt::convert_to_int<const char *, char>::value));
EXPECT_TRUE((fmt::convert_to_int<TestEnum2, char>::value));
}
#if FMT_USE_ENUM_BASE
enum TestEnum : char {TestValue};
TEST(UtilTest, IsEnumConvertibleToInt) {
EXPECT_TRUE((fmt::internal::convert_to_int<TestEnum, char>::value));
EXPECT_TRUE((fmt::convert_to_int<TestEnum, char>::value));
}
#endif

View File

@ -37,10 +37,10 @@ static std::wostream &operator<<(std::wostream &os, TestEnum) {
enum TestEnum2 {A};
TEST(OStreamTest, Enum) {
EXPECT_FALSE((fmt::internal::convert_to_int<TestEnum, char>::value));
EXPECT_FALSE((fmt::convert_to_int<TestEnum, char>::value));
EXPECT_EQ("TestEnum", fmt::format("{}", TestEnum()));
EXPECT_EQ("0", fmt::format("{}", A));
EXPECT_FALSE((fmt::internal::convert_to_int<TestEnum, wchar_t>::value));
EXPECT_FALSE((fmt::convert_to_int<TestEnum, wchar_t>::value));
EXPECT_EQ(L"TestEnum", fmt::format(L"{}", TestEnum()));
EXPECT_EQ(L"0", fmt::format(L"{}", A));
}