Skip to content

Commit

Permalink
Add support for C++20 format API
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Parpart <[email protected]>
  • Loading branch information
christianparpart committed Sep 22, 2024
1 parent 17ac598 commit 45e7c2e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions include/boxed-cpp/boxed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,31 @@ struct hash<boxed::detail::boxed<T, U>>
} // namespace std
// {{{ fmtlib integration
// clang-format off
#if __has_include(<fmt/format.h>)
#if __has_include(<format>)
#include <format>
// clang-format on

template <typename Type, typename Tag>
struct std::formatter<boxed::detail::boxed<Type, Tag>>: std::formatter<Type>
{
template <typename FormatContext>
auto format(boxed::detail::boxed<Type, Tag> const& val, FormatContext& ctx) const
{
return std::formatter<Type>::format(val.value, ctx);
}
};
#elif __has_include(<fmt/format.h>)

// clang-format off
#include <fmt/format.h>
// clang-format on

template <typename Type, typename Tag>
struct fmt::formatter<boxed::detail::boxed<Type, Tag>>
struct fmt::formatter<boxed::detail::boxed<Type, Tag>>: fmt::formatter<Type>
{
constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.begin(); }

auto format(boxed::detail::boxed<Type, Tag> const& val, fmt::format_context& ctx) const
{
return fmt::format_to(ctx.out(), "{}", val.value);
return fmt::formatter<Type>::format(val.value, ctx);
}
};

Expand Down

0 comments on commit 45e7c2e

Please sign in to comment.