From 492d2b6a18ec8263fbab2fceb4f2b25ef700c84d Mon Sep 17 00:00:00 2001 From: Bela Schaum Date: Fri, 18 Mar 2022 10:59:09 +0100 Subject: [PATCH] Sparse only if have an element. Switch only if -. (#169) --- include/magic_enum.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index 7b88bd8..1b52c38 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -601,7 +601,7 @@ constexpr bool is_sparse() noexcept { constexpr auto min = is_flags_v ? log2(min_v) : min_v; constexpr auto range_size = max - min + 1; - return range_size != count_v; + return range_size != count_v && count_v > 0; } template @@ -1092,7 +1092,7 @@ template > return static_cast(result); } return {}; // Invalid value or out of range. - } else { + } else if constexpr (detail::count_v > 0) { constexpr bool default_predicate = std::is_same_v, std::equal_to> || std::is_same_v, std::equal_to<>>; @@ -1110,6 +1110,8 @@ template > } return {}; // Invalid value or out of range. } + } else { + return {}; } }