1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-10 23:44:29 +00:00

fix enums with underlying type bool

This commit is contained in:
neargye 2021-06-24 11:47:06 +03:00
parent 5003c436d6
commit 739942ab7d
2 changed files with 18 additions and 2 deletions

View file

@ -383,7 +383,9 @@ constexpr int reflected_min() noexcept {
static_assert(lhs > (std::numeric_limits<std::int16_t>::min)(), "magic_enum::enum_range requires min must be greater than INT16_MIN."); static_assert(lhs > (std::numeric_limits<std::int16_t>::min)(), "magic_enum::enum_range requires min must be greater than INT16_MIN.");
constexpr auto rhs = (std::numeric_limits<U>::min)(); constexpr auto rhs = (std::numeric_limits<U>::min)();
if constexpr (cmp_less(lhs, rhs)) { if constexpr (std::is_same_v<bool, U>) {
return static_cast<int>(rhs);
} else if constexpr (cmp_less(lhs, rhs)) {
return rhs; return rhs;
} else { } else {
static_assert(!is_valid<E, value<E, lhs - 1, IsFlags>(0)>(), "magic_enum::enum_range detects enum value smaller than min range size."); static_assert(!is_valid<E, value<E, lhs - 1, IsFlags>(0)>(), "magic_enum::enum_range detects enum value smaller than min range size.");
@ -403,7 +405,9 @@ constexpr int reflected_max() noexcept {
static_assert(lhs < (std::numeric_limits<std::int16_t>::max)(), "magic_enum::enum_range requires max must be less than INT16_MAX."); static_assert(lhs < (std::numeric_limits<std::int16_t>::max)(), "magic_enum::enum_range requires max must be less than INT16_MAX.");
constexpr auto rhs = (std::numeric_limits<U>::max)(); constexpr auto rhs = (std::numeric_limits<U>::max)();
if constexpr (cmp_less(lhs, rhs)) { if constexpr (std::is_same_v<bool, U>) {
return static_cast<int>(rhs);
} else if constexpr (cmp_less(lhs, rhs)) {
static_assert(!is_valid<E, value<E, lhs + 1, IsFlags>(0)>(), "magic_enum::enum_range detects enum value larger than max range size."); static_assert(!is_valid<E, value<E, lhs + 1, IsFlags>(0)>(), "magic_enum::enum_range detects enum value larger than max range size.");
return lhs; return lhs;
} else { } else {

View file

@ -54,6 +54,12 @@ enum number : unsigned long {
#endif #endif
}; };
enum class Binary : bool
{
ONE,
TWO
};
namespace magic_enum::customize { namespace magic_enum::customize {
template <> template <>
struct enum_range<number> { struct enum_range<number> {
@ -737,3 +743,9 @@ TEST_CASE("cmp_less") {
REQUIRE_FALSE(cmp_less(uint64_t_min, int32_t_min + offset_int64_t)); REQUIRE_FALSE(cmp_less(uint64_t_min, int32_t_min + offset_int64_t));
} }
} }
TEST_CASE("bool") {
REQUIRE(magic_enum::detail::reflected_min_v<Binary> == 0);
REQUIRE(magic_enum::detail::reflected_max_v<Binary> == 1);
REQUIRE(magic_enum::enum_values<Binary>() == std::array<Binary, 2>{{Binary::ONE, Binary::TWO}});
}