diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index 09669a0..9ea8eb9 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -98,7 +98,7 @@ struct static_string final { constexpr std::size_t size() const noexcept { return chars.size(); } - constexpr operator std::string_view() const noexcept { return {chars.data(), chars.size()}; } + constexpr operator std::string_view() const noexcept { return {data(), size()}; } private: template @@ -119,9 +119,14 @@ struct static_string<0> final { }; template -inline constexpr auto min_v = enum_range::min > (std::numeric_limits>::min)() - ? enum_range::min - : (std::numeric_limits>::min)(); +inline constexpr int min_v = static_cast(enum_range::min > (std::numeric_limits>::min)() + ? enum_range::min + : (std::numeric_limits>::min)()); + +template +inline constexpr int max_v = static_cast(enum_range::max < (std::numeric_limits>::max)() + ? enum_range::max + : (std::numeric_limits>::max)()); template [[nodiscard]] constexpr auto range() { @@ -129,10 +134,8 @@ template static_assert(enum_range::min > (std::numeric_limits::min)(), "magic_enum::enum_range requires min must be greater than INT_MIN."); static_assert(enum_range::max < (std::numeric_limits::max)(), "magic_enum::enum_range requires max must be less than INT_MAX."); static_assert(enum_range::max > enum_range::min, "magic_enum::enum_range requires max > min."); - using U = std::underlying_type_t; - constexpr auto max = enum_range::max < (std::numeric_limits::max)() ? enum_range::max : (std::numeric_limits::max)(); - return std::make_integer_sequence + 1>{}; + return std::make_integer_sequence - min_v + 1>{}; } template @@ -187,9 +190,14 @@ template template [[nodiscard]] constexpr std::string_view name(E value) noexcept { static_assert(std::is_enum_v, "magic_enum::detail::name requires enum type."); + using U = std::underlying_type_t; constexpr auto names = strings(range_v); - if (auto i = static_cast(static_cast(value) - min_v); i < names.size()) { + if (static_cast(value) > static_cast(max_v) || static_cast(value) < static_cast(min_v)) { + return {}; // Value out of range. + } + + if (auto i = static_cast(static_cast(value) - min_v); i < names.size()) { return names[i]; } @@ -199,7 +207,7 @@ template template [[nodiscard]] constexpr auto values(std::integer_sequence) noexcept { static_assert(std::is_enum_v, "magic_enum::detail::values requires enum type."); - constexpr std::array valid{{(name_v(I + min_v)>.size() != 0 )...}}; + constexpr std::array valid{{(name_v(I + min_v)>.size() != 0)...}}; constexpr auto num_valid = ((valid[I] ? 1 : 0) + ...); std::array values{}; @@ -216,7 +224,7 @@ template inline constexpr auto values_v = values(range_v); template -inline constexpr auto count_v = values_v.size(); +inline constexpr std::size_t count_v = values_v.size(); template [[nodiscard]] constexpr auto names(std::integer_sequence) noexcept {