Add xchar support for std::byte formatter (#4480)

Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
This commit is contained in:
Vladislav Shchapov
2025-06-24 02:02:11 +05:00
committed by GitHub
parent f4345467fc
commit 571c02d475
2 changed files with 10 additions and 2 deletions

View File

@ -3904,13 +3904,14 @@ constexpr auto format_as(Enum e) noexcept -> underlying_t<Enum> {
} // namespace enums
#ifdef __cpp_lib_byte
template <> struct formatter<std::byte> : formatter<unsigned> {
template <typename Char>
struct formatter<std::byte, Char> : formatter<unsigned, Char> {
static auto format_as(std::byte b) -> unsigned char {
return static_cast<unsigned char>(b);
}
template <typename Context>
auto format(std::byte b, Context& ctx) const -> decltype(ctx.out()) {
return formatter<unsigned>::format(format_as(b), ctx);
return formatter<unsigned, Char>::format(format_as(b), ctx);
}
};
#endif

View File

@ -171,6 +171,13 @@ TEST(xchar_test, join) {
EXPECT_EQ(fmt::format(L"({})", fmt::join(t, L", ")), L"(a, 1, 2)");
}
#ifdef __cpp_lib_byte
TEST(xchar_test, join_bytes) {
auto v = std::vector<std::byte>{std::byte(1), std::byte(2), std::byte(3)};
EXPECT_EQ(fmt::format(L"{}", fmt::join(v, L", ")), L"1, 2, 3");
}
#endif
enum streamable_enum {};
std::wostream& operator<<(std::wostream& os, streamable_enum) {