From 1ab80aa92ca5698fa8b511077a267944d52ccea4 Mon Sep 17 00:00:00 2001 From: Deniz Evrenci Date: Thu, 5 Dec 2019 13:12:46 +0900 Subject: [PATCH] Fix handling of types with custom formatters that are convertible to std::string_view --- include/fmt/core.h | 4 +++- test/format-test.cc | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 13d74ba8..1c6c855d 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -878,7 +878,9 @@ template struct arg_mapper { FMT_ENABLE_IF( std::is_constructible, T>::value && !std::is_constructible, T>::value && - !is_string::value)> + !is_string::value && + !has_formatter::value && + !has_fallback_formatter::value)> FMT_CONSTEXPR basic_string_view map(const T& val) { return std_string_view(val); } diff --git a/test/format-test.cc b/test/format-test.cc index f52e1275..2e1a0e70 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1685,6 +1685,28 @@ TEST(FormatterTest, FormatStdStringView) { EXPECT_EQ("test", format("{}", std::string_view("test"))); EXPECT_EQ("foo", format("{}", string_viewable())); } + +struct explicitly_convertible_to_std_string_view { + explicit operator std::string_view() const { return "foo"; } +}; + +namespace fmt { + +template <> +struct formatter + : formatter { + auto format(const explicitly_convertible_to_std_string_view& v, + format_context& ctx) { + return format_to(ctx.out(), "'{}'", std::string_view(v)); + } +}; + +} // namespace fmt + +TEST(FormatterTest, FormatExplicitlyConvertibleToStdStringView) { + EXPECT_EQ("'foo'", + fmt::format("{}", explicitly_convertible_to_std_string_view())); +} #endif FMT_BEGIN_NAMESPACE