Skip to content

Commit

Permalink
Fix handling of enums in to_string (#2036)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Dec 3, 2020
1 parent aabe0a8 commit 33f9a6d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +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_CONSTEXPR OutputIt write(OutputIt out, T value) {
return write<Char>(
out, static_cast<typename std::underlying_type<T>::type>(value));
}

template <typename Char, typename OutputIt>
constexpr OutputIt write(OutputIt out, bool value) {
return write<Char>(out, string_view(value ? "true" : "false"));
Expand Down
3 changes: 3 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,9 @@ TEST(FormatTest, ToString) {
EXPECT_EQ("42", fmt::to_string(42));
EXPECT_EQ("0x1234", fmt::to_string(reinterpret_cast<void*>(0x1234)));
EXPECT_EQ("foo", fmt::to_string(adl_test::fmt::detail::foo()));

enum test_enum : unsigned char { test_value };
EXPECT_EQ("0", fmt::to_string(test_value));
}

TEST(FormatTest, ToWString) { EXPECT_EQ(L"42", fmt::to_wstring(42)); }
Expand Down

0 comments on commit 33f9a6d

Please sign in to comment.