From d091ca081ed91f92f81d2e3c0a7985833ae88bda Mon Sep 17 00:00:00 2001 From: neargye Date: Thu, 22 Apr 2021 12:23:54 +0300 Subject: [PATCH] fix hard error if enum is empty or non-reflected --- include/nameof.hpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 1ae15ee..f0f7bdb 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -571,14 +571,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 > @@ -609,10 +613,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 {