Skip to content

Commit 41194ad

Browse files
Fixed the range calculation; fixes #424 (#428)
Thanks to @bryceschober for pointing this out and drafting a fix.
1 parent c36142d commit 41194ad

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

include/rfl/internal/enums/get_enum_names.hpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ consteval auto get_range_min() {
8484
if constexpr (_is_flag) {
8585
return 0;
8686
} else {
87-
return std::max(std::numeric_limits<U>::min(), range_min<T>::value);
87+
return std::max(static_cast<decltype(range_min<T>::value)>(
88+
std::numeric_limits<U>::min()),
89+
range_min<T>::value);
8890
}
8991
}
9092
template <class T, bool _is_flag>
@@ -97,7 +99,9 @@ consteval auto get_range_max() {
9799
return (sizeof(U) * 8 - 1);
98100
}
99101
} else {
100-
return std::min(std::numeric_limits<U>::max(), range_max<T>::value);
102+
return std::min(static_cast<decltype(range_max<T>::value)>(
103+
std::numeric_limits<U>::max()),
104+
range_max<T>::value);
101105
}
102106
}
103107

@@ -126,7 +130,8 @@ consteval auto operator|(Names<EnumType, LiteralType, N, _is_flag, _enums...>,
126130
}
127131
}
128132

129-
template <class EnumType, class LiteralType, size_t N, bool _is_flag, auto... _enums>
133+
template <class EnumType, class LiteralType, size_t N, bool _is_flag,
134+
auto... _enums>
130135
struct NamesCombiner {
131136
template <int... Is>
132137
static consteval auto combine(std::integer_sequence<int, Is...>) {
@@ -145,8 +150,10 @@ struct CombineNames {
145150
}(std::make_integer_sequence<int, End - Start + 1>{});
146151
} else {
147152
constexpr int Mid = Start + (End - Start) / 2;
148-
constexpr auto left = CombineNames<ChunkSize>::template apply<NamesType, Start, Mid>();
149-
constexpr auto right = CombineNames<ChunkSize>::template apply<NamesType, Mid + 1, End>();
153+
constexpr auto left =
154+
CombineNames<ChunkSize>::template apply<NamesType, Start, Mid>();
155+
constexpr auto right =
156+
CombineNames<ChunkSize>::template apply<NamesType, Mid + 1, End>();
150157
return left | right;
151158
}
152159
}
@@ -155,16 +162,18 @@ struct CombineNames {
155162
template <class EnumType, bool _is_flag>
156163
consteval auto get_enum_names() {
157164
static_assert(is_scoped_enum<EnumType>,
158-
"You must use scoped enums (using class or struct) for the parsing to work!");
165+
"You must use scoped enums (using class or struct) for the "
166+
"parsing to work!");
159167
static_assert(std::is_integral_v<std::underlying_type_t<EnumType>>,
160-
"The underlying type of any Enum must be integral!");
168+
"The underlying type of any Enum must be integral!");
161169

162170
constexpr auto max = get_range_max<EnumType, _is_flag>();
163171
constexpr auto min = get_range_min<EnumType, _is_flag>();
164172
constexpr auto range_size = max - min + 1;
165173

166174
static_assert(range_size > 0,
167-
"enum_range requires a valid range size. Ensure that max is greater than min.");
175+
"enum_range requires a valid range size. Ensure that max is "
176+
"greater than min.");
168177

169178
using EmptyNames = Names<EnumType, rfl::Literal<"">, 0, _is_flag>;
170179

tests/generic/test_enum_range_min_max.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace test_enum_range_min_max {
88

99
enum class InnerColor { none = -128, red = -50, green = 0, blue = 128 };
10-
enum class LineColor { yellow = 200, purple = 300, orange = 400 };
10+
enum class LineColor : uint16_t { yellow = 200, purple = 300, orange = 400 };
1111

1212
} // namespace test_enum_range_min_max
1313

@@ -30,4 +30,4 @@ TEST(generic, test_enum_range_min_max) {
3030
EXPECT_EQ(line_max, 400);
3131
}
3232

33-
} // namespace test_enum_range_min_max
33+
} // namespace test_enum_range_min_max

0 commit comments

Comments
 (0)