From ccd3e8bbf3bf715f291b006f672779545fc18470 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 27 Oct 2018 17:10:19 -0700 Subject: [PATCH] Make is_constructible public (#918) --- include/fmt/core.h | 20 ++++++++++---------- test/core-test.cc | 40 ++++++++++++++++++++++++++++++++++++++++ test/format-test.cc | 39 --------------------------------------- 3 files changed, 50 insertions(+), 49 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 02c89c29..676e7f7d 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -208,14 +208,6 @@ FMT_CONSTEXPR typename std::make_unsigned::type to_unsigned(Int value) { return static_cast::type>(value); } -#if FMT_GCC_VERSION && FMT_GCC_VERSION < 405 -template -struct is_constructible: std::false_type {}; -#else -template -struct is_constructible : std::is_constructible {}; -#endif - /** A contiguous memory buffer with an optional growing ability. */ template class basic_buffer { @@ -337,6 +329,14 @@ template struct no_formatter_error : std::false_type {}; } // namespace internal +#if FMT_GCC_VERSION && FMT_GCC_VERSION < 405 +template +struct is_constructible: std::false_type {}; +#else +template +struct is_constructible : std::is_constructible {}; +#endif + /** An implementation of ``std::basic_string_view`` for pre-C++17. It provides a subset of the API. ``fmt::basic_string_view`` is used for format strings even @@ -725,7 +725,7 @@ inline typename std::enable_if< template inline typename std::enable_if< - internal::is_constructible, T>::value && + is_constructible, T>::value && !internal::is_string::value, init, string_type>>::type make_value(const T &val) { return basic_string_view(val); } @@ -734,7 +734,7 @@ template inline typename std::enable_if< !convert_to_int::value && !std::is_same::value && !std::is_convertible>::value && - !internal::is_constructible, T>::value && + !is_constructible, T>::value && !internal::is_string::value, // Implicit conversion to std::string is not handled here because it's // unsafe: https://github.com/fmtlib/fmt/issues/729 diff --git a/test/core-test.cc b/test/core-test.cc index 02fe7267..d8a6500c 100644 --- a/test/core-test.cc +++ b/test/core-test.cc @@ -569,3 +569,43 @@ TEST(CoreTest, FormatForeignStrings) { EXPECT_EQ(fmt::format(QString(L"{}"), my_string(L"42")), L"42"); EXPECT_EQ(fmt::format(my_string(L"{}"), QString(L"42")), L"42"); } + +struct implicitly_convertible_to_string_view { + operator fmt::string_view() const { return "foo"; } +}; + +TEST(FormatterTest, FormatImplicitlyConvertibleToStringView) { + EXPECT_EQ("foo", fmt::format("{}", implicitly_convertible_to_string_view())); +} + +// std::is_constructible is broken in MSVC until version 2015. +#if FMT_USE_EXPLICIT && (!FMT_MSC_VER || FMT_MSC_VER >= 1900) +struct explicitly_convertible_to_string_view { + explicit operator fmt::string_view() const { return "foo"; } +}; + +TEST(FormatterTest, FormatExplicitlyConvertibleToStringView) { + EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_string_view())); +} + +struct explicitly_convertible_to_wstring_view { + explicit operator fmt::wstring_view() const { return L"foo"; } +}; + +TEST(FormatterTest, FormatExplicitlyConvertibleToWStringView) { + EXPECT_EQ(L"foo", + fmt::format(L"{}", explicitly_convertible_to_wstring_view())); +} + +struct explicitly_convertible_to_string_like { + template < + typename String, + typename = typename std::enable_if< + std::is_constructible::value>::type> + FMT_EXPLICIT operator String() const { return String("foo", 3u); } +}; + +TEST(FormatterTest, FormatExplicitlyConvertibleToStringLike) { + EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_string_like())); +} +#endif diff --git a/test/format-test.cc b/test/format-test.cc index 4157ad68..d6bbd19c 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1549,45 +1549,6 @@ TEST(FormatterTest, FormatStdStringView) { } #endif -struct implicitly_convertible_to_string_view { - operator fmt::string_view() const { return "foo"; } -}; - -TEST(FormatterTest, FormatImplicitlyConvertibleToStringView) { - EXPECT_EQ("foo", format("{}", implicitly_convertible_to_string_view())); -} - -// std::is_constructible is broken in MSVC until version 2015. -#if FMT_USE_EXPLICIT && (!FMT_MSC_VER || FMT_MSC_VER >= 1900) -struct explicitly_convertible_to_string_view { - explicit operator fmt::string_view() const { return "foo"; } -}; - -TEST(FormatterTest, FormatExplicitlyConvertibleToStringView) { - EXPECT_EQ("foo", format("{}", explicitly_convertible_to_string_view())); -} - -struct explicitly_convertible_to_wstring_view { - explicit operator fmt::wstring_view() const { return L"foo"; } -}; - -TEST(FormatterTest, FormatExplicitlyConvertibleToWStringView) { - EXPECT_EQ(L"foo", format(L"{}", explicitly_convertible_to_wstring_view())); -} - -struct explicitly_convertible_to_string_like { - template < - typename String, - typename = typename std::enable_if< - std::is_constructible::value>::type> - FMT_EXPLICIT operator String() const { return String("foo", 3u); } -}; - -TEST(FormatterTest, FormatExplicitlyConvertibleToStringLike) { - EXPECT_EQ("foo", format("{}", explicitly_convertible_to_string_like())); -} -#endif - FMT_BEGIN_NAMESPACE template <> struct formatter {