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
This commit is contained in:
Alexey Ochapov 2020-12-27 18:23:28 +03:00 committed by GitHub
parent bbd6ed5bc5
commit d09b5c1453
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 5 deletions

View File

@ -2113,9 +2113,14 @@ FMT_CONSTEXPR OutputIt write(OutputIt out, T value) {
return base_iterator(out, it);
}
template <typename Char, typename OutputIt, typename T,
FMT_ENABLE_IF(std::is_enum<T>::value &&
!std::is_same<T, Char>::value)>
// FMT_ENABLE_IF() condition separated to workaround MSVC bug
template <
typename Char, typename OutputIt, typename T,
bool check =
std::is_enum<T>::value && !std::is_same<T, Char>::value &&
mapped_type_constant<T, basic_format_context<OutputIt, Char>>::value !=
type::custom_type,
FMT_ENABLE_IF(check)>
FMT_CONSTEXPR OutputIt write(OutputIt out, T value) {
return write<Char>(
out, static_cast<typename std::underlying_type<T>::type>(value));
@ -3570,7 +3575,7 @@ FMT_FORMAT_AS(Char*, const Char*);
FMT_FORMAT_AS(std::basic_string<Char>, basic_string_view<Char>);
FMT_FORMAT_AS(std::nullptr_t, const void*);
FMT_FORMAT_AS(detail::std_string_view<Char>, basic_string_view<Char>);
#if __cplusplus >= 201703L
#ifdef __cpp_lib_byte
FMT_FORMAT_AS(std::byte, unsigned);
#endif

View File

@ -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) {

View File

@ -1762,7 +1762,7 @@ TEST(FormatTest, JoinArg) {
#endif
}
#if __cplusplus >= 201703L
#ifdef __cpp_lib_byte
TEST(FormatTest, JoinBytes) {
std::vector<std::byte> v = {std::byte(1), std::byte(2), std::byte(3)};
EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, ", ")));