From 45e7c2e393dd1644f1ab18fb48ba59910c75aad0 Mon Sep 17 00:00:00 2001 From: Christian Parpart Date: Sun, 22 Sep 2024 11:09:32 +0200 Subject: [PATCH] Add support for C++20 format API Signed-off-by: Christian Parpart --- include/boxed-cpp/boxed.hpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/include/boxed-cpp/boxed.hpp b/include/boxed-cpp/boxed.hpp index dfc7e46..4677190 100644 --- a/include/boxed-cpp/boxed.hpp +++ b/include/boxed-cpp/boxed.hpp @@ -207,19 +207,31 @@ struct hash> } // namespace std // {{{ fmtlib integration // clang-format off -#if __has_include() +#if __has_include() +#include +// clang-format on +template +struct std::formatter>: std::formatter +{ + template + auto format(boxed::detail::boxed const& val, FormatContext& ctx) const + { + return std::formatter::format(val.value, ctx); + } +}; +#elif __has_include() + +// clang-format off #include // clang-format on template -struct fmt::formatter> +struct fmt::formatter>: fmt::formatter { - constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); } - auto format(boxed::detail::boxed const& val, fmt::format_context& ctx) const { - return fmt::format_to(ctx.out(), "{}", val.value); + return fmt::formatter::format(val.value, ctx); } };