From e79588d6c1e654cb2833d496042b14807c8cd818 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 21 Jan 2018 18:11:57 -0800 Subject: [PATCH] Replace a bunch of craft with type_traits --- include/fmt/core.h | 38 ++------------------------------------ include/fmt/ostream.h | 18 ++++-------------- test/util-test.cc | 6 +++--- 3 files changed, 9 insertions(+), 53 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index dd3d689c..ba3859da 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -336,47 +336,13 @@ inline void require_wchar() { "formatting of wide characters into a narrow output is disallowed"); } -using yes = char[1]; -using no = char[2]; - -yes &convert(unsigned long long); -no &convert(...); - -template -struct convert_to_int_impl { - enum { value = ENABLE_CONVERSION }; -}; - -template -struct convert_to_int_impl2 { - enum { value = false }; -}; - -template -struct convert_to_int_impl2 { - enum { - // Don't convert arithmetic types. - value = convert_to_int_impl::value>::value - }; -}; - -template +template struct convert_to_int { enum { - enable_conversion = sizeof(convert(std::declval())) == sizeof(yes) + value = !std::is_arithmetic::value && std::is_convertible::value }; - enum { value = convert_to_int_impl2::value }; }; -#define FMT_DISABLE_CONVERSION_TO_INT(Type) \ - template <> \ - struct convert_to_int { enum { value = 0 }; } - -// Silence warnings about convering float to int. -FMT_DISABLE_CONVERSION_TO_INT(float); -FMT_DISABLE_CONVERSION_TO_INT(double); -FMT_DISABLE_CONVERSION_TO_INT(long double); - template struct named_arg_base; diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 09b85608..6e26817f 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -47,22 +47,12 @@ class FormatBuf : public std::basic_streambuf { } }; -yes &convert(std::ostream &); - -struct DummyStream : std::ostream { - DummyStream(); // Suppress a bogus warning in MSVC. - // Hide all operator<< overloads from std::ostream. - void operator<<(null<>); -}; - -no &operator<<(std::ostream &, int); - -template -struct convert_to_int_impl { +template +struct convert_to_int() << std::declval()) != 0> { // Convert to int only if T doesn't have an overloaded operator<<. enum { - value = sizeof(convert(std::declval() << std::declval())) - == sizeof(no) + value = false }; }; diff --git a/test/util-test.cc b/test/util-test.cc index 65d8e5f8..4f4f04c7 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -827,15 +827,15 @@ TEST(UtilTest, ReportWindowsError) { enum TestEnum2 {}; TEST(UtilTest, ConvertToInt) { - EXPECT_TRUE(fmt::internal::convert_to_int::enable_conversion); - EXPECT_FALSE(fmt::internal::convert_to_int::enable_conversion); + EXPECT_FALSE(fmt::internal::convert_to_int::value); + EXPECT_FALSE(fmt::internal::convert_to_int::value); EXPECT_TRUE(fmt::internal::convert_to_int::value); } #if FMT_USE_ENUM_BASE enum TestEnum : char {TestValue}; TEST(UtilTest, IsEnumConvertibleToInt) { - EXPECT_TRUE(fmt::internal::convert_to_int::enable_conversion); + EXPECT_TRUE(fmt::internal::convert_to_int::value); } #endif