Skip to content

Commit

Permalink
Fix handling of types that are explicitly convertible to std::string_…
Browse files Browse the repository at this point in the history
…view
  • Loading branch information
denizevrenci committed Dec 5, 2019
1 parent d22e4ad commit bbcb02c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,9 @@ template <typename Context> struct arg_mapper {
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)>
!is_string<T>::value &&
!has_formatter<T, Context>::value &&
!has_fallback_formatter<T, Context>::value)>
FMT_CONSTEXPR basic_string_view<char_type> map(const T& val) {
return std_string_view<char_type>(val);
}
Expand Down
26 changes: 26 additions & 0 deletions test/custom-formatter-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,29 @@ TEST(CustomFormatterTest, Format) {
EXPECT_EQ("0.00", custom_format("{:.2f}", -.00001));
}
#endif

#ifdef FMT_USE_STRING_VIEW
struct explicitly_convertible_to_std_string_view {
explicit operator std::string_view() const { return "foo"; }
};

namespace fmt {

template <>
class formatter<explicitly_convertible_to_std_string_view>
: public formatter<std::string_view> {
public:
template <typename format_context>
auto format(const explicitly_convertible_to_std_string_view& v,
format_context& ctx) {
return format_to(ctx.out(), "'{}'", static_cast<std::string_view>(v));
}
};

} // namespace fmt

TEST(CustomFormatterTest, FormatExplicitlyConvertibleToStdStringView) {
EXPECT_EQ("'foo'",
fmt::format("{}", explicitly_convertible_to_std_string_view()));
}
#endif

0 comments on commit bbcb02c

Please sign in to comment.