From 8a21e328b8dcb62a2901c499598366a0f5f3f4a5 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 4 Feb 2022 11:12:04 -0800 Subject: [PATCH] Remove problematic constructibility check --- include/fmt/core.h | 15 +++++++-------- test/core-test.cc | 12 +++++++++--- test/ranges-test.cc | 4 ++-- test/xchar-test.cc | 6 ++++-- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index f64f1a69..4c41f92d 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1337,20 +1337,19 @@ template struct arg_mapper { } template , T>::value && + std::is_convertible>::value && !is_string::value && !has_formatter::value && !has_fallback_formatter::value)> FMT_CONSTEXPR FMT_INLINE auto map(const T& val) -> basic_string_view { return basic_string_view(val); } - template < - typename T, - FMT_ENABLE_IF( - std::is_constructible, T>::value && - !std::is_constructible, T>::value && - !is_string::value && !has_formatter::value && - !has_fallback_formatter::value)> + template >::value && + !std::is_convertible>::value && + !is_string::value && !has_formatter::value && + !has_fallback_formatter::value)> FMT_CONSTEXPR FMT_INLINE auto map(const T& val) -> basic_string_view { return std_string_view(val); diff --git a/test/core-test.cc b/test/core-test.cc index fc874071..a040d241 100644 --- a/test/core-test.cc +++ b/test/core-test.cc @@ -890,7 +890,10 @@ struct explicitly_convertible_to_string_view { }; TEST(core_test, format_explicitly_convertible_to_string_view) { - EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_string_view())); + // Types explicitly convertible to string_view are not formattable by + // default because it may introduce ODR violations. + static_assert( + !fmt::is_formattable::value, ""); } # ifdef FMT_USE_STRING_VIEW @@ -899,8 +902,11 @@ struct explicitly_convertible_to_std_string_view { }; TEST(core_test, format_explicitly_convertible_to_std_string_view) { - EXPECT_EQ("foo", - fmt::format("{}", explicitly_convertible_to_std_string_view())); + // Types explicitly convertible to string_view are not formattable by + // default because it may introduce ODR violations. + static_assert( + !fmt::is_formattable::value, + ""); } # endif #endif diff --git a/test/ranges-test.cc b/test/ranges-test.cc index 97e84cd7..a12bb9bb 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -133,8 +133,8 @@ TEST(ranges_test, path_like) { struct string_like { const char* begin(); const char* end(); - explicit operator fmt::string_view() const { return "foo"; } - explicit operator std::string_view() const { return "foo"; } + operator fmt::string_view() const { return "foo"; } + operator std::string_view() const { return "foo"; } }; TEST(ranges_test, format_string_like) { diff --git a/test/xchar-test.cc b/test/xchar-test.cc index 025a670b..b07e8e26 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -72,8 +72,10 @@ struct explicitly_convertible_to_wstring_view { }; TEST(xchar_test, format_explicitly_convertible_to_wstring_view) { - EXPECT_EQ(L"foo", - fmt::format(L"{}", explicitly_convertible_to_wstring_view())); + // Types explicitly convertible to wstring_view are not formattable by + // default because it may introduce ODR violations. + static_assert( + !fmt::is_formattable::value, ""); } #endif