Skip to content

Commit

Permalink
Expand FMT_FORMAT_AS to include implict conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
Arghnews committed Jul 13, 2024
1 parent 0b932de commit 0980555
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3978,9 +3978,16 @@ struct formatter<T, Char, enable_if_t<detail::has_format_as<T>::value>>
}
};

#define FMT_FORMAT_AS(Type, Base) \
template <typename Char> \
struct formatter<Type, Char> : formatter<Base, Char> {}
#define FMT_FORMAT_AS(Type, Base) \
template <typename Char> \
struct formatter<Type, Char> : formatter<Base, Char> { \
template <typename FormatContext> \
auto format(Type value, FormatContext&& ctx) const \
-> decltype(ctx.out()) { \
using base = formatter<Base, Char>; \
return base::format(value, std::forward<FormatContext>(ctx)); \
} \
}

FMT_FORMAT_AS(signed char, int);
FMT_FORMAT_AS(unsigned char, unsigned);
Expand All @@ -3990,23 +3997,13 @@ 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

0 comments on commit 0980555

Please sign in to comment.