From db5b8993acc5faea26501489077a094932912129 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 24 Mar 2022 17:22:44 -0700 Subject: [PATCH] Fix formatting of std::byte via format_as --- include/fmt/core.h | 7 +++++++ include/fmt/format.h | 6 ------ test/format-test.cc | 8 +++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 3feb2a7d..18c9ae6c 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -8,6 +8,7 @@ #ifndef FMT_CORE_H_ #define FMT_CORE_H_ +#include // std::byte #include // std::FILE #include // std::strlen #include @@ -1275,6 +1276,12 @@ enum { long_short = sizeof(long) == sizeof(int) }; using long_type = conditional_t; using ulong_type = conditional_t; +#ifdef __cpp_lib_byte +inline auto format_as(std::byte b) -> unsigned char { + return static_cast(b); +} +#endif + // Maps formatting arguments to core types. // arg_mapper reports errors by returning unformattable instead of using // static_assert because it's used in the is_formattable trait. diff --git a/include/fmt/format.h b/include/fmt/format.h index 67e5bcca..60e9b3d7 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -34,7 +34,6 @@ #define FMT_FORMAT_H_ #include // std::signbit -#include // std::byte #include // uint32_t #include // std::memcpy #include // std::numeric_limits @@ -2997,11 +2996,6 @@ constexpr auto format_as(Enum e) noexcept -> underlying_t { } } // namespace enums -#ifdef __cpp_lib_byte -inline auto format_as(std::byte b) -> unsigned char { return underlying(b); } -FMT_FORMAT_AS(std::byte, unsigned char); -#endif - class bytes { private: string_view data_; diff --git a/test/format-test.cc b/test/format-test.cc index 09553af4..1bee1aa4 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1760,9 +1760,15 @@ TEST(format_test, join) { } #ifdef __cpp_lib_byte +TEST(format_test, format_byte) { + using arg_mapper = fmt::detail::arg_mapper; + EXPECT_EQ(arg_mapper().map(std::byte(42)), 42); + EXPECT_EQ(fmt::format("{}", std::byte(42)), "42"); +} + TEST(format_test, join_bytes) { auto v = std::vector{std::byte(1), std::byte(2), std::byte(3)}; - EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, ", "))); + EXPECT_EQ(fmt::format("{}", fmt::join(v, ", ")), "1, 2, 3"); } #endif