From a9813bd7a15c92293384f2505e44bcb4005b97ba Mon Sep 17 00:00:00 2001 From: neargye Date: Thu, 22 Apr 2021 12:41:25 +0300 Subject: [PATCH] fix range check --- include/nameof.hpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index f0f7bdb..5c5cff6 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -498,6 +498,17 @@ constexpr bool is_valid() noexcept { return n(V)>().size() != 0; } +template > +constexpr E value(std::size_t i) noexcept { + static_assert(is_enum_v, "nameof::detail::value requires enum type."); + + if constexpr (IsFlags) { + return static_cast(U{1} << static_cast(static_cast(i) + O)); + } else { + return static_cast(static_cast(i) + O); + } +} + template > constexpr int reflected_min() noexcept { static_assert(is_enum_v, "nameof::detail::reflected_min requires enum type."); @@ -512,6 +523,7 @@ constexpr int reflected_min() noexcept { if constexpr (cmp_less(lhs, rhs)) { return rhs; } else { + static_assert(!is_valid(0)>(), "nameof::enum_range detects enum value smaller than min range size."); return lhs; } } @@ -529,6 +541,7 @@ constexpr int reflected_max() noexcept { constexpr auto rhs = (std::numeric_limits::max)(); if constexpr (cmp_less(lhs, rhs)) { + static_assert(!is_valid(0)>(), "nameof::enum_range detects enum value larger than max range size."); return lhs; } else { return rhs; @@ -542,17 +555,6 @@ inline constexpr auto reflected_min_v = reflected_min(); template inline constexpr auto reflected_max_v = reflected_max(); -template > -constexpr E value(std::size_t i) noexcept { - static_assert(is_enum_v, "nameof::detail::value requires enum type."); - - if constexpr (IsFlags) { - return static_cast(U{1} << static_cast(static_cast(i) + O)); - } else { - return static_cast(static_cast(i) + O); - } -} - template constexpr std::size_t values_count(const bool (&valid)[N]) noexcept { auto count = std::size_t{0}; @@ -593,12 +595,6 @@ constexpr auto values() noexcept { constexpr auto range_size = max - min + 1; static_assert(range_size > 0, "nameof::enum_range requires valid size."); static_assert(range_size < (std::numeric_limits::max)(), "nameof::enum_range requires valid size."); - if constexpr (cmp_less((std::numeric_limits::min)(), min) && !IsFlags) { - static_assert(!is_valid(0)>(), "nameof::enum_range detects enum value smaller than min range size."); - } - if constexpr (cmp_less(range_size, (std::numeric_limits::max)()) && !IsFlags) { - static_assert(!is_valid(range_size + 1)>(), "nameof::enum_range detects enum value larger than max range size."); - } return values>(std::make_index_sequence{}); }