From d09b5c14531f361dad2fae7ee7dfc284672eb5e2 Mon Sep 17 00:00:00 2001 From: Alexey Ochapov Date: Sun, 27 Dec 2020 18:23:28 +0300 Subject: [PATCH] Fix std::byte formatting with compile-time API (#2072) * add test for byte formatting with `FMT_COMPILE` * fix byte formatting with `FMT_COMPILE`, use `__cpp_lib_byte` macro * use is not custom mapped type check * workaround MSVC bug --- include/fmt/format.h | 13 +++++++++---- test/compile-test.cc | 3 +++ test/format-test.cc | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 580e9f29..3d18b9e1 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2113,9 +2113,14 @@ FMT_CONSTEXPR OutputIt write(OutputIt out, T value) { return base_iterator(out, it); } -template ::value && - !std::is_same::value)> +// FMT_ENABLE_IF() condition separated to workaround MSVC bug +template < + typename Char, typename OutputIt, typename T, + bool check = + std::is_enum::value && !std::is_same::value && + mapped_type_constant>::value != + type::custom_type, + FMT_ENABLE_IF(check)> FMT_CONSTEXPR OutputIt write(OutputIt out, T value) { return write( out, static_cast::type>(value)); @@ -3570,7 +3575,7 @@ FMT_FORMAT_AS(Char*, const Char*); FMT_FORMAT_AS(std::basic_string, basic_string_view); FMT_FORMAT_AS(std::nullptr_t, const void*); FMT_FORMAT_AS(detail::std_string_view, basic_string_view); -#if __cplusplus >= 201703L +#ifdef __cpp_lib_byte FMT_FORMAT_AS(std::byte, unsigned); #endif diff --git a/test/compile-test.cc b/test/compile-test.cc index c2e1ec1c..df76f085 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -130,6 +130,9 @@ TEST(CompileTest, FormatDefault) { EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), "foo")); EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), std::string("foo"))); EXPECT_EQ("foo", fmt::format(FMT_COMPILE("{}"), test_formattable())); +# ifdef __cpp_lib_byte + EXPECT_EQ("42", fmt::format(FMT_COMPILE("{}"), std::byte{42})); +# endif } TEST(CompileTest, FormatWideString) { diff --git a/test/format-test.cc b/test/format-test.cc index 81bc406f..e821e773 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1762,7 +1762,7 @@ TEST(FormatTest, JoinArg) { #endif } -#if __cplusplus >= 201703L +#ifdef __cpp_lib_byte TEST(FormatTest, JoinBytes) { std::vector v = {std::byte(1), std::byte(2), std::byte(3)}; EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, ", ")));