Skip to content

Commit

Permalink
Fix handling of types convertible to std::string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Dec 9, 2019
1 parent fd52de0 commit 9f2e7ed
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
18 changes: 9 additions & 9 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -878,9 +878,7 @@ 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 &&
!has_formatter<T, Context>::value &&
!has_fallback_formatter<T, Context>::value)>
!is_string<T>::value && !has_formatter<T, Context>::value)>
FMT_CONSTEXPR basic_string_view<char_type> map(const T& val) {
return std_string_view<char_type>(val);
}
Expand Down Expand Up @@ -913,12 +911,14 @@ template <typename Context> struct arg_mapper {
map(static_cast<typename std::underlying_type<T>::type>(val))) {
return map(static_cast<typename std::underlying_type<T>::type>(val));
}
template <typename T,
FMT_ENABLE_IF(!is_string<T>::value && !is_char<T>::value &&
!std::is_constructible<basic_string_view<char_type>,
T>::value &&
(has_formatter<T, Context>::value ||
has_fallback_formatter<T, Context>::value))>
template <
typename T,
FMT_ENABLE_IF(
!is_string<T>::value && !is_char<T>::value &&
!std::is_constructible<basic_string_view<char_type>, T>::value &&
(has_formatter<T, Context>::value ||
(has_fallback_formatter<T, Context>::value &&
!std::is_constructible<std_string_view<char_type>, T>::value)))>
FMT_CONSTEXPR const T& map(const T& val) {
return val;
}
Expand Down
4 changes: 1 addition & 3 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1691,16 +1691,14 @@ struct explicitly_convertible_to_std_string_view {
};

namespace fmt {

template <>
struct formatter<explicitly_convertible_to_std_string_view>
: formatter<std::string_view> {
auto format(const explicitly_convertible_to_std_string_view& v,
format_context& ctx) {
format_context& ctx) -> decltype(ctx.out()) {
return format_to(ctx.out(), "'{}'", std::string_view(v));
}
};

} // namespace fmt

TEST(FormatterTest, FormatExplicitlyConvertibleToStdStringView) {
Expand Down
22 changes: 22 additions & 0 deletions test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,25 @@ std::ostream& operator<<(std::ostream& os,
TEST(FormatterTest, FormatExplicitlyConvertibleToStringLikeIgnoreInserter) {
EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_string_like()));
}

#ifdef FMT_USE_STRING_VIEW
struct explicitly_convertible_to_std_string_view {
explicit operator fmt::internal::std_string_view<char>() const {
return {"foo", 3u};
}
};

TEST(FormatterTest, FormatExplicitlyConvertibleToStdStringView) {
EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_string_like()));
}

std::ostream& operator<<(std::ostream& os,
explicitly_convertible_to_std_string_view) {
return os << "bar";
}

TEST(FormatterTest, FormatExplicitlyConvertibleToStdStringViewIgnoreInserter) {
EXPECT_EQ("foo",
fmt::format("{}", explicitly_convertible_to_std_string_view()));
}
#endif // FMT_USE_STRING_VIEW

0 comments on commit 9f2e7ed

Please sign in to comment.