From 5a33daa76624b5d4bf10e5ea81050559ebbb33b8 Mon Sep 17 00:00:00 2001 From: neargye Date: Thu, 17 Oct 2019 13:18:37 +0500 Subject: [PATCH] update enum_traits --- include/magic_enum.hpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index b7d6eb0..252504a 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -229,7 +229,7 @@ constexpr std::size_t reflected_size() noexcept { static_assert(reflected_max_v > reflected_min_v, "magic_enum::enum_range requires max > min."); constexpr auto size = reflected_max_v - reflected_min_v + 1; static_assert(size > 0, "magic_enum::enum_range requires valid size."); - static_assert(size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); + static_assert(size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); return static_cast(size); } @@ -330,12 +330,14 @@ struct enum_traits {}; template struct enum_traits>> { using type = E; - using underlying_type = std::underlying_type_t; + using underlying_type = typename detail::underlying_type::type; inline static constexpr std::string_view type_name = detail::type_name_v; - inline static constexpr bool is_unscoped_enum = detail::is_unscoped_enum::value; - inline static constexpr bool is_scoped_enum = detail::is_scoped_enum::value; + inline static constexpr bool is_unscoped = detail::is_unscoped_enum::value; + inline static constexpr bool is_scoped = detail::is_scoped_enum::value; + inline static constexpr bool is_dense = detail::range_size_v == detail::count_v; + inline static constexpr bool is_sparse = detail::range_size_v != detail::count_v; inline static constexpr std::size_t count = detail::count_v; inline static constexpr std::array values = detail::values_v; @@ -348,7 +350,7 @@ struct enum_traits>> { [[nodiscard]] static constexpr int index(E value) noexcept { if (static_cast(value) >= static_cast(min_v) && static_cast(value) <= static_cast(max_v)) { - if constexpr (range_size_v != count_v) { + if constexpr (is_sparse) { if (auto i = indexes[static_cast(value) - min_v]; i != invalid_index_v) { return i; } @@ -361,7 +363,7 @@ struct enum_traits>> { } [[nodiscard]] static constexpr E value(std::size_t index) noexcept { - if constexpr (range_size_v != count_v) { + if constexpr (is_sparse) { return assert(index < count), values[index]; } else { return assert(index < count), static_cast(index + min_v);