Fix handling of types convertible to std::string_view

This commit is contained in:
Victor Zverovich 2019-10-04 17:21:10 -07:00
parent 0571013709
commit 0c7650373c
2 changed files with 19 additions and 0 deletions

View File

@ -828,6 +828,14 @@ template <typename Context> struct arg_mapper {
FMT_CONSTEXPR basic_string_view<char_type> map(const T& val) { FMT_CONSTEXPR basic_string_view<char_type> map(const T& val) {
return basic_string_view<char_type>(val); return basic_string_view<char_type>(val);
} }
template <typename T,
FMT_ENABLE_IF(
std::is_constructible<std_string_view<char_type>, T>::value &&
!std::is_constructible<basic_string_view<char_type>, T>::value &&
!is_string<T>::value)>
FMT_CONSTEXPR basic_string_view<char_type> map(const T& val) {
return std_string_view<char_type>(val);
}
FMT_CONSTEXPR const char* map(const signed char* val) { FMT_CONSTEXPR const char* map(const signed char* val) {
static_assert(std::is_same<char_type, char>::value, "invalid string type"); static_assert(std::is_same<char_type, char>::value, "invalid string type");
return reinterpret_cast<const char*>(val); return reinterpret_cast<const char*>(val);

View File

@ -613,6 +613,17 @@ TEST(FormatterTest, FormatExplicitlyConvertibleToStringView) {
EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_string_view())); 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 { struct explicitly_convertible_to_wstring_view {
explicit operator fmt::wstring_view() const { return L"foo"; } explicit operator fmt::wstring_view() const { return L"foo"; }
}; };