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

Issue 3545 #3582

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 11 additions & 0 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ template <typename T> struct is_contiguous : std::false_type {};
template <typename Char>
struct is_contiguous<std::basic_string<Char>> : std::true_type {};

template<typename E, typename = void>
struct is_scoped_enum : std::false_type {};
template<typename E>
struct is_scoped_enum<E, std::enable_if_t<
std::is_enum<E>::value &&
!std::is_convertible<E, typename std::underlying_type<E>::type>::value
# ifdef __cpp_lib_byte
&& !std::is_same<E, std::byte>::value
# endif
>> : std::true_type {};

struct monostate {
constexpr monostate() {}
};
Expand Down
9 changes: 7 additions & 2 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4290,8 +4290,13 @@ auto join(Range&& range, string_view sep)
template <typename T, FMT_ENABLE_IF(!std::is_integral<T>::value)>
inline auto to_string(const T& value) -> std::string {
auto buffer = memory_buffer();
detail::write<char>(appender(buffer), value);
return {buffer.data(), buffer.size()};
if constexpr (is_scoped_enum<T>::value){
detail::write<char>(appender(buffer), static_cast<typename std::underlying_type<T>::type>(value));
return {buffer.data(), buffer.size()};
}else{
detail::write<char>(appender(buffer), value);
return {buffer.data(), buffer.size()};
}
}

template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
Expand Down
4 changes: 4 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1944,11 +1944,15 @@ struct formatter<adl_test::fmt::detail::foo> : formatter<std::string> {
};
FMT_END_NAMESPACE

enum class test_enum_class : char { one='1', two='2' };
auto format_as(test_enum_class e) -> char { return static_cast<char>(e); }

TEST(format_test, to_string) {
EXPECT_EQ(fmt::to_string(42), "42");
EXPECT_EQ(fmt::to_string(reinterpret_cast<void*>(0x1234)), "0x1234");
EXPECT_EQ(fmt::to_string(adl_test::fmt::detail::foo()), "foo");
EXPECT_EQ(fmt::to_string(foo), "0");
EXPECT_EQ(fmt::to_string(test_enum_class::one),"1");

#if FMT_USE_FLOAT128
EXPECT_EQ(fmt::to_string(__float128(0.5)), "0.5");
Expand Down