mirror of
https://github.com/fmtlib/fmt.git
synced 2025-04-24 09:02:32 +00:00
Add support for types explicitly convertible to fmt::string_view
This commit is contained in:
parent
6eaa507473
commit
73bed45b7a
@ -672,10 +672,17 @@ inline typename std::enable_if<
|
|||||||
typed_value<C, int_type>>::type
|
typed_value<C, int_type>>::type
|
||||||
make_value(const T &val) { return static_cast<int>(val); }
|
make_value(const T &val) { return static_cast<int>(val); }
|
||||||
|
|
||||||
|
template <typename C, typename T, typename Char = typename C::char_type>
|
||||||
|
inline typename std::enable_if<
|
||||||
|
std::is_constructible<basic_string_view<Char>, T>::value,
|
||||||
|
typed_value<C, string_type>>::type
|
||||||
|
make_value(const T &val) { return string_view(val); }
|
||||||
|
|
||||||
template <typename C, typename T, typename Char = typename C::char_type>
|
template <typename C, typename T, typename Char = typename C::char_type>
|
||||||
inline typename std::enable_if<
|
inline typename std::enable_if<
|
||||||
!convert_to_int<T, Char>::value &&
|
!convert_to_int<T, Char>::value &&
|
||||||
!std::is_convertible<T, basic_string_view<Char>>::value,
|
!std::is_convertible<T, basic_string_view<Char>>::value &&
|
||||||
|
!std::is_constructible<string_view, T>::value,
|
||||||
// Implicit conversion to std::string is not handled here because it's
|
// Implicit conversion to std::string is not handled here because it's
|
||||||
// unsafe: https://github.com/fmtlib/fmt/issues/729
|
// unsafe: https://github.com/fmtlib/fmt/issues/729
|
||||||
typed_value<C, custom_type>>::type
|
typed_value<C, custom_type>>::type
|
||||||
|
@ -1085,14 +1085,25 @@ TEST(FormatterTest, FormatStdStringView) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct ConvertibleToStringView {
|
struct implicitly_convertible_to_string_view {
|
||||||
operator fmt::string_view() const { return "foo"; }
|
operator fmt::string_view() const { return "foo"; }
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST(FormatterTest, FormatConvertibleToStringView) {
|
TEST(FormatterTest, FormatImplicitlyConvertibleToStringView) {
|
||||||
EXPECT_EQ("foo", format("{}", ConvertibleToStringView()));
|
EXPECT_EQ("foo", format("{}", implicitly_convertible_to_string_view()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// std::is_constructible is broken in MSVC until version 2015.
|
||||||
|
#if !FMT_MSC_VER || FMT_MSC_VER >= 1900
|
||||||
|
struct explicitly_convertible_to_string_view {
|
||||||
|
explicit operator fmt::string_view() const { return "foo"; }
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST(FormatterTest, FormatExplicitlyConvertibleToStringView) {
|
||||||
|
EXPECT_EQ("foo", format("{}", explicitly_convertible_to_string_view()));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
template <>
|
template <>
|
||||||
struct formatter<Date> {
|
struct formatter<Date> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user