From c646c9d76d8b4d5ca2b9f0fdb331950411c3e753 Mon Sep 17 00:00:00 2001 From: neargye Date: Wed, 21 Apr 2021 19:19:03 +0300 Subject: [PATCH] fix hard error if enum is empty or non-reflected --- include/magic_enum.hpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index e475a7d..112bfcf 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -431,14 +431,18 @@ constexpr auto values(std::index_sequence) noexcept { constexpr bool valid[sizeof...(I)] = {is_valid(I)>()...}; constexpr std::size_t count = values_count(valid); - E values[count] = {}; - for (std::size_t i = 0, v = 0; v < count; ++i) { - if (valid[i]) { - values[v++] = value(i); + if constexpr (count > 0) { + E values[count] = {}; + for (std::size_t i = 0, v = 0; v < count; ++i) { + if (valid[i]) { + values[v++] = value(i); + } } - } - return to_array(values, std::make_index_sequence{}); + return to_array(values, std::make_index_sequence{}); + } else { + return std::array{}; + } } template > @@ -469,10 +473,10 @@ template inline constexpr auto count_v = values_v.size(); template > -inline constexpr auto min_v = static_cast(values_v.front()); +inline constexpr auto min_v = (count_v > 0) ? static_cast(values_v.front()) : U{0}; template > -inline constexpr auto max_v = static_cast(values_v.back()); +inline constexpr auto max_v = (count_v >) 0 ? static_cast(values_v.back()) : U{0}; template > constexpr std::size_t range_size() noexcept {