Use inheritance for formatter

This commit is contained in:
Shawn Zhong 2023-02-07 03:08:08 -06:00
parent a7d70b5dfd
commit ef3ffef853

View File

@ -675,25 +675,16 @@ template <typename T> class is_container_adaptor_like {
template <typename T, typename Char> template <typename T, typename Char>
struct formatter<T, Char, struct formatter<T, Char,
enable_if_t<detail::is_container_adaptor_like<T>::value>> { enable_if_t<detail::is_container_adaptor_like<T>::value>>
private: : formatter<typename T::container_type, Char> {
struct formatter<typename T::container_type, Char> container_formatter;
public:
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return container_formatter.parse(ctx);
}
template <typename FormatContext> template <typename FormatContext>
auto format(T& t, FormatContext& ctx) const -> auto format(T& t, FormatContext& ctx) const -> decltype(ctx.out()) {
typename FormatContext::iterator {
struct getter : T { struct getter : T {
static auto get(T& t) -> typename T::container_type& { static auto get(T& t) -> typename T::container_type& {
return t.*(&getter::c); // Access c through the derived class. return t.*(&getter::c); // Access c through the derived class.
} }
}; };
return container_formatter.format(getter::get(t), ctx); return formatter<typename T::container_type>::format(getter::get(t), ctx);
} }
}; };