From 41194ad33cc29efc285c67b4e16ce0fe2f24d1fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Mon, 5 May 2025 11:52:45 +0800 Subject: [PATCH] Fixed the range calculation; fixes #424 (#428) Thanks to @bryceschober for pointing this out and drafting a fix. --- include/rfl/internal/enums/get_enum_names.hpp | 25 +++++++++++++------ tests/generic/test_enum_range_min_max.cpp | 4 +-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/rfl/internal/enums/get_enum_names.hpp b/include/rfl/internal/enums/get_enum_names.hpp index 1f66eb95..d5a31569 100644 --- a/include/rfl/internal/enums/get_enum_names.hpp +++ b/include/rfl/internal/enums/get_enum_names.hpp @@ -84,7 +84,9 @@ consteval auto get_range_min() { if constexpr (_is_flag) { return 0; } else { - return std::max(std::numeric_limits::min(), range_min::value); + return std::max(static_cast::value)>( + std::numeric_limits::min()), + range_min::value); } } template @@ -97,7 +99,9 @@ consteval auto get_range_max() { return (sizeof(U) * 8 - 1); } } else { - return std::min(std::numeric_limits::max(), range_max::value); + return std::min(static_cast::value)>( + std::numeric_limits::max()), + range_max::value); } } @@ -126,7 +130,8 @@ consteval auto operator|(Names, } } -template +template struct NamesCombiner { template static consteval auto combine(std::integer_sequence) { @@ -145,8 +150,10 @@ struct CombineNames { }(std::make_integer_sequence{}); } else { constexpr int Mid = Start + (End - Start) / 2; - constexpr auto left = CombineNames::template apply(); - constexpr auto right = CombineNames::template apply(); + constexpr auto left = + CombineNames::template apply(); + constexpr auto right = + CombineNames::template apply(); return left | right; } } @@ -155,16 +162,18 @@ struct CombineNames { template consteval auto get_enum_names() { static_assert(is_scoped_enum, - "You must use scoped enums (using class or struct) for the parsing to work!"); + "You must use scoped enums (using class or struct) for the " + "parsing to work!"); static_assert(std::is_integral_v>, - "The underlying type of any Enum must be integral!"); + "The underlying type of any Enum must be integral!"); constexpr auto max = get_range_max(); constexpr auto min = get_range_min(); constexpr auto range_size = max - min + 1; static_assert(range_size > 0, - "enum_range requires a valid range size. Ensure that max is greater than min."); + "enum_range requires a valid range size. Ensure that max is " + "greater than min."); using EmptyNames = Names, 0, _is_flag>; diff --git a/tests/generic/test_enum_range_min_max.cpp b/tests/generic/test_enum_range_min_max.cpp index 08188538..16221a74 100644 --- a/tests/generic/test_enum_range_min_max.cpp +++ b/tests/generic/test_enum_range_min_max.cpp @@ -7,7 +7,7 @@ namespace test_enum_range_min_max { enum class InnerColor { none = -128, red = -50, green = 0, blue = 128 }; -enum class LineColor { yellow = 200, purple = 300, orange = 400 }; +enum class LineColor : uint16_t { yellow = 200, purple = 300, orange = 400 }; } // namespace test_enum_range_min_max @@ -30,4 +30,4 @@ TEST(generic, test_enum_range_min_max) { EXPECT_EQ(line_max, 400); } -} // namespace test_enum_range_min_max \ No newline at end of file +} // namespace test_enum_range_min_max