From 9f2e7edaebccf8f271cec5e855cce1223ff3e6d6 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 9 Dec 2019 13:25:08 -0800 Subject: [PATCH] Fix handling of types convertible to std::string_view --- include/fmt/core.h | 18 +++++++++--------- test/format-test.cc | 4 +--- test/ostream-test.cc | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index efe6c8e5..398253cb 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -878,9 +878,7 @@ template struct arg_mapper { FMT_ENABLE_IF( std::is_constructible, T>::value && !std::is_constructible, T>::value && - !is_string::value && - !has_formatter::value && - !has_fallback_formatter::value)> + !is_string::value && !has_formatter::value)> FMT_CONSTEXPR basic_string_view map(const T& val) { return std_string_view(val); } @@ -913,12 +911,14 @@ template struct arg_mapper { map(static_cast::type>(val))) { return map(static_cast::type>(val)); } - template ::value && !is_char::value && - !std::is_constructible, - T>::value && - (has_formatter::value || - has_fallback_formatter::value))> + template < + typename T, + FMT_ENABLE_IF( + !is_string::value && !is_char::value && + !std::is_constructible, T>::value && + (has_formatter::value || + (has_fallback_formatter::value && + !std::is_constructible, T>::value)))> FMT_CONSTEXPR const T& map(const T& val) { return val; } diff --git a/test/format-test.cc b/test/format-test.cc index fa5429f6..42ba77ba 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1691,16 +1691,14 @@ struct explicitly_convertible_to_std_string_view { }; namespace fmt { - template <> struct formatter : formatter { auto format(const explicitly_convertible_to_std_string_view& v, - format_context& ctx) { + format_context& ctx) -> decltype(ctx.out()) { return format_to(ctx.out(), "'{}'", std::string_view(v)); } }; - } // namespace fmt TEST(FormatterTest, FormatExplicitlyConvertibleToStdStringView) { diff --git a/test/ostream-test.cc b/test/ostream-test.cc index 8a55d890..9944618e 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -276,3 +276,25 @@ std::ostream& operator<<(std::ostream& os, TEST(FormatterTest, FormatExplicitlyConvertibleToStringLikeIgnoreInserter) { EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_string_like())); } + +#ifdef FMT_USE_STRING_VIEW +struct explicitly_convertible_to_std_string_view { + explicit operator fmt::internal::std_string_view() const { + return {"foo", 3u}; + } +}; + +TEST(FormatterTest, FormatExplicitlyConvertibleToStdStringView) { + EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_string_like())); +} + +std::ostream& operator<<(std::ostream& os, + explicitly_convertible_to_std_string_view) { + return os << "bar"; +} + +TEST(FormatterTest, FormatExplicitlyConvertibleToStdStringViewIgnoreInserter) { + EXPECT_EQ("foo", + fmt::format("{}", explicitly_convertible_to_std_string_view())); +} +#endif // FMT_USE_STRING_VIEW