Fix underflow in out of range check

Fix bool special case
This commit is contained in:
neargye 2021-06-26 12:03:55 +03:00
parent e009769d00
commit b19b00b61a

View file

@ -429,6 +429,10 @@ constexpr bool cmp_less(L lhs, R rhs) noexcept {
if constexpr (std::is_signed_v<L> == std::is_signed_v<R>) { if constexpr (std::is_signed_v<L> == std::is_signed_v<R>) {
// If same signedness (both signed or both unsigned). // If same signedness (both signed or both unsigned).
return lhs < rhs; return lhs < rhs;
} else if constexpr (std::is_same_v<L, bool>) { // bool special case due to msvc's C4804, C4018
return static_cast<R>(lhs) < rhs;
} else if constexpr (std::is_same_v<R, bool>) { // bool special case due to msvc's C4804, C4018
return lhs < static_cast<L>(rhs);
} else if constexpr (std::is_signed_v<R>) { } else if constexpr (std::is_signed_v<R>) {
// If 'right' is negative, then result is 'false', otherwise cast & compare. // If 'right' is negative, then result is 'false', otherwise cast & compare.
return rhs > 0 && lhs < static_cast<std::make_unsigned_t<R>>(rhs); return rhs > 0 && lhs < static_cast<std::make_unsigned_t<R>>(rhs);
@ -520,11 +524,11 @@ constexpr int reflected_min() noexcept {
static_assert(lhs > (std::numeric_limits<std::int16_t>::min)(), "nameof::enum_range requires min must be greater than INT16_MIN."); static_assert(lhs > (std::numeric_limits<std::int16_t>::min)(), "nameof::enum_range requires min must be greater than INT16_MIN.");
constexpr auto rhs = (std::numeric_limits<U>::min)(); constexpr auto rhs = (std::numeric_limits<U>::min)();
if constexpr (cmp_less(lhs, rhs)) { if constexpr (cmp_less(rhs, lhs)) {
return rhs;
} else {
static_assert(!is_valid<E, value<E, lhs - 1, IsFlags>(0)>(), "nameof::enum_range detects enum value smaller than min range size."); static_assert(!is_valid<E, value<E, lhs - 1, IsFlags>(0)>(), "nameof::enum_range detects enum value smaller than min range size.");
return lhs; return lhs;
} else {
return rhs;
} }
} }
} }