From 739942ab7d1c85f487b5ab58823fe039ce7b2757 Mon Sep 17 00:00:00 2001 From: neargye Date: Thu, 24 Jun 2021 11:47:06 +0300 Subject: [PATCH] fix enums with underlying type bool --- include/magic_enum.hpp | 8 ++++++-- test/test.cpp | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index f2c9dea..8073c9a 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -383,7 +383,9 @@ constexpr int reflected_min() noexcept { static_assert(lhs > (std::numeric_limits::min)(), "magic_enum::enum_range requires min must be greater than INT16_MIN."); constexpr auto rhs = (std::numeric_limits::min)(); - if constexpr (cmp_less(lhs, rhs)) { + if constexpr (std::is_same_v) { + return static_cast(rhs); + } else if constexpr (cmp_less(lhs, rhs)) { return rhs; } else { static_assert(!is_valid(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::max)(), "magic_enum::enum_range requires max must be less than INT16_MAX."); constexpr auto rhs = (std::numeric_limits::max)(); - if constexpr (cmp_less(lhs, rhs)) { + if constexpr (std::is_same_v) { + return static_cast(rhs); + } else if constexpr (cmp_less(lhs, rhs)) { static_assert(!is_valid(0)>(), "magic_enum::enum_range detects enum value larger than max range size."); return lhs; } else { diff --git a/test/test.cpp b/test/test.cpp index ccaa986..34c11fa 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -54,6 +54,12 @@ enum number : unsigned long { #endif }; +enum class Binary : bool +{ + ONE, + TWO +}; + namespace magic_enum::customize { template <> struct enum_range { @@ -737,3 +743,9 @@ TEST_CASE("cmp_less") { REQUIRE_FALSE(cmp_less(uint64_t_min, int32_t_min + offset_int64_t)); } } + +TEST_CASE("bool") { + REQUIRE(magic_enum::detail::reflected_min_v == 0); + REQUIRE(magic_enum::detail::reflected_max_v == 1); + REQUIRE(magic_enum::enum_values() == std::array{{Binary::ONE, Binary::TWO}}); +}