From 987514761eb29e3ce9d9ec93b3a2ca6cfeb77fd9 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 22 Aug 2018 07:40:06 -0700 Subject: [PATCH] Make convert_to_int public (#818) --- CMakeLists.txt | 16 ++++++++++------ include/fmt/core.h | 14 +++++++------- include/fmt/ostream.h | 18 +++++++++--------- test/core-test.cc | 8 ++++---- test/ostream-test.cc | 4 ++-- 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fe1a6f9d..3d2d59e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/include/fmt/core.h b/include/fmt/core.h index 1adca5ae..dbcaad90 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -341,6 +341,13 @@ class basic_format_args; template struct formatter; +template +struct convert_to_int { + enum { + value = !std::is_arithmetic::value && std::is_convertible::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 -struct convert_to_int { - enum { - value = !std::is_arithmetic::value && std::is_convertible::value - }; -}; - template struct string_value { const Char *value; diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 0b1a4a02..e467f1ae 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -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 -class convert_to_int { - public: - static const bool value = - convert_to_int::value && !is_streamable::value; -}; - // Write the content of buf to os. template void write(std::basic_ostream &os, basic_buffer &buf) { @@ -106,6 +97,15 @@ void format_value(basic_buffer &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 +struct convert_to_int { + static const bool value = + convert_to_int::value && + !internal::is_streamable::value; +}; + // Formats an object of type T that has an overloaded ostream operator<<. template struct formatter::value)); - EXPECT_FALSE((fmt::internal::convert_to_int::value)); - EXPECT_TRUE((fmt::internal::convert_to_int::value)); + EXPECT_FALSE((fmt::convert_to_int::value)); + EXPECT_FALSE((fmt::convert_to_int::value)); + EXPECT_TRUE((fmt::convert_to_int::value)); } #if FMT_USE_ENUM_BASE enum TestEnum : char {TestValue}; TEST(UtilTest, IsEnumConvertibleToInt) { - EXPECT_TRUE((fmt::internal::convert_to_int::value)); + EXPECT_TRUE((fmt::convert_to_int::value)); } #endif diff --git a/test/ostream-test.cc b/test/ostream-test.cc index 0f6bb793..96f6066c 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -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::value)); + EXPECT_FALSE((fmt::convert_to_int::value)); EXPECT_EQ("TestEnum", fmt::format("{}", TestEnum())); EXPECT_EQ("0", fmt::format("{}", A)); - EXPECT_FALSE((fmt::internal::convert_to_int::value)); + EXPECT_FALSE((fmt::convert_to_int::value)); EXPECT_EQ(L"TestEnum", fmt::format(L"{}", TestEnum())); EXPECT_EQ(L"0", fmt::format(L"{}", A)); }