diff --git a/include/nameof.hpp b/include/nameof.hpp index 327787d..e68d20e 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -281,11 +281,15 @@ template static_assert(enum_range::max < (std::numeric_limits::max)(), "nameof::enum_range requires max must be less than INT_MAX."); static_assert(enum_range::max > enum_range::min, "nameof::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)(); - constexpr auto min = enum_range::min > (std::numeric_limits::min)() ? enum_range::min : (std::numeric_limits::min)(); + constexpr int max = static_cast(enum_range::max < (std::numeric_limits::max)() ? enum_range::max : (std::numeric_limits::max)()); + constexpr int min = static_cast(enum_range::min > (std::numeric_limits::min)() ? enum_range::min : (std::numeric_limits::min)()); constexpr auto names = detail::enum_names(std::make_integer_sequence{}); - if (auto i = static_cast(static_cast(value) - min); i < names.size()) { + if (static_cast(value) > static_cast(max) || static_cast(value) < static_cast(min)) { + return {}; // Value out of range. + } + + if (auto i = static_cast(static_cast(value) - min); i < names.size()) { return names[i]; }