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

Map not int enum to correct underlying_type #1286

Merged
merged 2 commits into from
Aug 31, 2019
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
4 changes: 2 additions & 2 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,8 @@ template <typename Context> struct arg_mapper {
FMT_ENABLE_IF(std::is_enum<T>::value &&
!has_formatter<T, Context>::value &&
!has_fallback_formatter<T, Context>::value)>
FMT_CONSTEXPR int map(const T& val) {
return static_cast<int>(val);
FMT_CONSTEXPR auto map(const T& val) -> decltype(map(static_cast<typename std::underlying_type<T>::type>(val))) {
return map(static_cast<typename std::underlying_type<T>::type>(val));
}
template <typename T,
FMT_ENABLE_IF(!is_string<T>::value && !is_char<T>::value &&
Expand Down
4 changes: 2 additions & 2 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1906,9 +1906,9 @@ TEST(FormatTest, FormatterNotSpecialized) {
}

#if FMT_HAS_FEATURE(cxx_strong_enums)
enum TestFixedEnum : short { B };
enum TestFixedEnum : short { B = 1 };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it actually formatted differently if we don't map to underlying type?

Copy link
Contributor Author

@agmt agmt Sep 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the problem clearly but if map() returns short (which doesn't enumerated in enum type)

FMT_ENABLE_IF(std::is_enum<T>::value &&
                          !has_formatter<T, Context>::value &&
                          !has_fallback_formatter<T, Context>::value)>
  FMT_CONSTEXPR typename std::underlying_type<T>::type map(const T& val) {
    return static_cast<typename std::underlying_type<T>::type>(val);
  }

then after that will be called map(short) with correct argument and map(int) with garbage ( =0 almost always). It means that fmt::format(short_enum_value) will return "0" regardless of the argument.


TEST(FormatTest, FixedEnum) { EXPECT_EQ("0", fmt::format("{}", B)); }
TEST(FormatTest, FixedEnum) { EXPECT_EQ("1", fmt::format("{}", B)); }
#endif

using buffer_range = fmt::buffer_range<char>;
Expand Down