Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix std::byte formatting with compile-time API #2072

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2111,9 +2111,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));
Expand Down Expand Up @@ -3562,7 +3567,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

Expand Down
3 changes: 3 additions & 0 deletions test/compile-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,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) {
Expand Down
2 changes: 1 addition & 1 deletion test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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, ", ")));
Expand Down