Skip to content

Commit

Permalink
Enable inheriting from formatter<std::string_view>
Browse files Browse the repository at this point in the history
Fixes #4036
This formatter specialization with base::format means a class implicitly
convertible to std::string_view will now be converted by this format
function before being passed to the fmt::string_view format function.
This wouldn't work previously as the compiler may only perform one
implicit conversion, and we need 2 here (from our type, to
std::string_view, to fmt::string_view).
  • Loading branch information
Arghnews committed Jul 13, 2024
1 parent 6a192f8 commit 0b932de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 11 additions & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3990,13 +3990,23 @@ FMT_FORMAT_AS(long, detail::long_type);
FMT_FORMAT_AS(unsigned long, detail::ulong_type);
FMT_FORMAT_AS(Char*, const Char*);
FMT_FORMAT_AS(std::nullptr_t, const void*);
FMT_FORMAT_AS(detail::std_string_view<Char>, basic_string_view<Char>);
FMT_FORMAT_AS(void*, const void*);

template <typename Char, typename Traits, typename Allocator>
class formatter<std::basic_string<Char, Traits, Allocator>, Char>
: public formatter<basic_string_view<Char>, Char> {};

// Fixes issue #4036
template <typename Char>
struct formatter<detail::std_string_view<Char>, Char>
: public formatter<basic_string_view<Char>, Char> {
auto format(const detail::std_string_view<Char>& value,
format_context& ctx) const -> decltype(ctx.out()) {
using base = formatter<basic_string_view<Char>, Char>;
return base::format(value, ctx);
}
};

template <typename Char, size_t N>
struct formatter<Char[N], Char> : formatter<basic_string_view<Char>, Char> {};

Expand Down
14 changes: 14 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,20 @@ TEST(format_test, format_explicitly_convertible_to_std_string_view) {
EXPECT_EQ("'foo'",
fmt::format("{}", explicitly_convertible_to_std_string_view()));
}

struct convertible_to_std_string_view {
operator std::string_view() const noexcept { return "Hi there"; }
};
FMT_BEGIN_NAMESPACE
template <>
class formatter<convertible_to_std_string_view>
: public formatter<std::string_view> {};
FMT_END_NAMESPACE

TEST(format_test, format_implicitly_convertible_and_inherits_string_view) {
static_assert(fmt::is_formattable<convertible_to_std_string_view>{}, "");
EXPECT_EQ("Hi there", fmt::format("{}", convertible_to_std_string_view{}));
}
#endif

class Answer {};
Expand Down

0 comments on commit 0b932de

Please sign in to comment.