Skip to content

Commit 45e7c2e

Browse files
Add support for C++20 format API
Signed-off-by: Christian Parpart <[email protected]>
1 parent 17ac598 commit 45e7c2e

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

include/boxed-cpp/boxed.hpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,31 @@ struct hash<boxed::detail::boxed<T, U>>
207207
} // namespace std
208208
// {{{ fmtlib integration
209209
// clang-format off
210-
#if __has_include(<fmt/format.h>)
210+
#if __has_include(<format>)
211+
#include <format>
212+
// clang-format on
211213

214+
template <typename Type, typename Tag>
215+
struct std::formatter<boxed::detail::boxed<Type, Tag>>: std::formatter<Type>
216+
{
217+
template <typename FormatContext>
218+
auto format(boxed::detail::boxed<Type, Tag> const& val, FormatContext& ctx) const
219+
{
220+
return std::formatter<Type>::format(val.value, ctx);
221+
}
222+
};
223+
#elif __has_include(<fmt/format.h>)
224+
225+
// clang-format off
212226
#include <fmt/format.h>
213227
// clang-format on
214228

215229
template <typename Type, typename Tag>
216-
struct fmt::formatter<boxed::detail::boxed<Type, Tag>>
230+
struct fmt::formatter<boxed::detail::boxed<Type, Tag>>: fmt::formatter<Type>
217231
{
218-
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }
219-
220232
auto format(boxed::detail::boxed<Type, Tag> const& val, fmt::format_context& ctx) const
221233
{
222-
return fmt::format_to(ctx.out(), "{}", val.value);
234+
return fmt::formatter<Type>::format(val.value, ctx);
223235
}
224236
};
225237

0 commit comments

Comments
 (0)