From 241c6d9906416c9f6af1594ddb296d11b40bb865 Mon Sep 17 00:00:00 2001 From: terik23 Date: Sun, 14 Jul 2019 20:49:14 +0500 Subject: [PATCH] fix assert and cast --- include/nameof.hpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 223bb03..c847b2b 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -63,6 +63,16 @@ struct enum_range final { static_assert(max > min, "nameof::enum_range requires max > min."); }; +static_assert(NAMEOF_ENUM_RANGE_MIN <= 0, + "NAMEOF_ENUM_RANGE_MIN must be less or equals than 0."); +static_assert(NAMEOF_ENUM_RANGE_MIN > (std::numeric_limits::min)(), + "NAMEOF_ENUM_RANGE_MIN must be greater than INT_MIN."); + +static_assert(NAMEOF_ENUM_RANGE_MAX > 0, + "NAMEOF_ENUM_RANGE_MAX must be greater than 0."); +static_assert(NAMEOF_ENUM_RANGE_MAX < (std::numeric_limits::max)(), + "NAMEOF_ENUM_RANGE_MAX must be less than INT_MAX."); + namespace detail { template @@ -219,8 +229,8 @@ template constexpr auto range = std::make_integer_sequence{}; constexpr auto names = detail::enum_names_impl(range); - if (int i = static_cast(value) - min; i >= 0 && static_cast(i) < names.size()) { - return names[static_cast(i)]; + if (auto i = static_cast(static_cast(value) - min); i < names.size()) { + return names[i]; } else { return {}; // Value out of range. }