diff --git a/include/fmt/core.h b/include/fmt/core.h index 0d7d15b5..8cdc9cdc 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -828,6 +828,14 @@ template struct arg_mapper { FMT_CONSTEXPR basic_string_view map(const T& val) { return basic_string_view(val); } + template , T>::value && + !std::is_constructible, T>::value && + !is_string::value)> + FMT_CONSTEXPR basic_string_view map(const T& val) { + return std_string_view(val); + } FMT_CONSTEXPR const char* map(const signed char* val) { static_assert(std::is_same::value, "invalid string type"); return reinterpret_cast(val); diff --git a/test/core-test.cc b/test/core-test.cc index 0de26dbe..84d2b9eb 100644 --- a/test/core-test.cc +++ b/test/core-test.cc @@ -613,6 +613,17 @@ TEST(FormatterTest, FormatExplicitlyConvertibleToStringView) { EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_string_view())); } +#ifdef FMT_USE_STRING_VIEW +struct explicitly_convertible_to_std_string_view { + explicit operator std::string_view() const { return "foo"; } +}; + +TEST(FormatterTest, FormatExplicitlyConvertibleToStdStringView) { + EXPECT_EQ("foo", + fmt::format("{}", explicitly_convertible_to_std_string_view())); +} +#endif + struct explicitly_convertible_to_wstring_view { explicit operator fmt::wstring_view() const { return L"foo"; } };