From 4172418e5909961142b2bfa4cc842e6e48a028b6 Mon Sep 17 00:00:00 2001 From: neargye Date: Thu, 29 Aug 2019 18:48:54 +0500 Subject: [PATCH] fix type cast nameof_enum --- include/nameof.hpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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]; }