Enable inheriting from formatter<std::string_view>#4055
Conversation
fd8e243 to
a0d85e6
Compare
include/fmt/format.h
Outdated
| // 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); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Looks like I forgot to actually comment what the requested change was =). Let's apply the change to FMT_FORMAT_AS rather than just the formatter specialization for string_view.
There was a problem hiding this comment.
No problem, have updated review (haven't squashed yet so you can see diffs), with what I believe you're saying. Just check the types of the params and this is what you meant etc.
a0d85e6 to
0b932de
Compare
include/fmt/format.h
Outdated
| template <typename Char> \ | ||
| struct formatter<Type, Char> : formatter<Base, Char> { \ | ||
| template <typename FormatContext> \ | ||
| auto format(Type value, FormatContext&& ctx) const \ |
There was a problem hiding this comment.
We either have to pass Type as a value like here, or turn this parameter into a forwarding reference too otherwise we get const errors. I opted for a value as the types for this are all small (<=16bytes)
Ditto on const issues with passing const format_context& or format_context& so have used a forwarding ref for that too (if there's a better way for this one let me know)
There was a problem hiding this comment.
FormatContext should always be passed by a normal reference (&).
Allows (for example) types convertible to std::string_view to inherit from the fmt::formatter<fmt::string_view> to work etc.
0980555 to
0c06809
Compare
|
Merged, thanks! |
Fixes #4036
This formatter specialization with
base::formatmeans a class implicitly convertible tostd::string_viewwill now be converted by this format function before being passed to thefmt::string_viewformat 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, tofmt::string_view).