From dc127d3d4d232c4b7e4daae199ea3fec35e1063a Mon Sep 17 00:00:00 2001 From: neargye Date: Sat, 18 Apr 2020 13:43:45 +0500 Subject: [PATCH] improve mixed_sign_less --- include/nameof.hpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 98834de..a7782f0 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -388,18 +388,18 @@ inline constexpr auto enum_name_v = n(); namespace enums { template -constexpr bool mixed_sign_less(L lhs, R rhs) noexcept { - static_assert(std::is_integral_v && std::is_integral_v, "nameof::detail::mixed_sign_less requires integral type."); +constexpr bool cmp_less(L lhs, R rhs) noexcept { + static_assert(std::is_integral_v && std::is_integral_v, "nameof::detail::cmp_less requires integral type."); - if constexpr (std::is_signed_v && std::is_unsigned_v) { - // If 'left' is negative, then result is 'true', otherwise cast & compare. - return lhs < 0 || static_cast>(lhs) < rhs; - } else if constexpr (std::is_unsigned_v && std::is_signed_v) { - // If 'right' is negative, then result is 'false', otherwise cast & compare. - return rhs >= 0 && lhs < static_cast>(rhs); - } else { + if constexpr (std::is_signed_v == std::is_signed_v) { // If same signedness (both signed or both unsigned). return lhs < rhs; + } else if constexpr (std::is_signed_v) { + // If 'right' is negative, then result is 'false', otherwise cast & compare. + return rhs > 0 && lhs < static_cast>(rhs); + } else { + // If 'left' is negative, then result is 'true', otherwise cast & compare. + return lhs < 0 || static_cast>(lhs) < rhs; } } @@ -410,7 +410,7 @@ constexpr int reflected_min() noexcept { static_assert(lhs > (std::numeric_limits::min)(), "nameof::enum_range requires min must be greater than INT16_MIN."); constexpr auto rhs = (std::numeric_limits>::min)(); - return mixed_sign_less(lhs, rhs) ? rhs : lhs; + return cmp_less(lhs, rhs) ? rhs : lhs; } template @@ -420,7 +420,7 @@ constexpr int reflected_max() noexcept { static_assert(lhs < (std::numeric_limits::max)(), "nameof::enum_range requires max must be less than INT16_MAX."); constexpr auto rhs = (std::numeric_limits>::max)(); - return mixed_sign_less(lhs, rhs) ? lhs : rhs; + return cmp_less(lhs, rhs) ? lhs : rhs; } template