Skip to content

Commit

Permalink
Replace enable_if_t in _Debug_lt_pred with if constexpr and var…
Browse files Browse the repository at this point in the history
…iable template (#4610)

Co-authored-by: Stephan T. Lavavej <stl@nuwen.net>
  • Loading branch information
AlexGuteniev and StephanTLavavej authored Apr 26, 2024
1 parent cbaea37 commit d74be47
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions stl/inc/xutility
Original file line number Diff line number Diff line change
Expand Up @@ -1513,26 +1513,30 @@ using _Unwrap_enum_t = typename _Unwrap_enum<_Elem>::type;
#define _DEBUG_ORDER_SET_UNWRAPPED(otherIter, first, last, pred) \
_STD _Debug_order_set_unchecked<otherIter>(first, last, pred)

template <class _Pr, class _Ty1, class _Ty2,
enable_if_t<is_same_v<_Remove_cvref_t<_Ty1>, _Remove_cvref_t<_Ty2>>, int> = 0>
constexpr bool _Debug_lt_pred(_Pr&& _Pred, _Ty1&& _Left, _Ty2&& _Right) noexcept(
noexcept(_Pred(_Left, _Right)) && noexcept(_Pred(_Right, _Left))) {
// test if _Pred(_Left, _Right) and _Pred is strict weak ordering, when the arguments are the cv-same-type
template <class _Pr, class _Ty1, class _Ty2>
constexpr bool _Enable_debug_lt_pred_order_check = is_same_v<_Remove_cvref_t<_Ty1>, _Remove_cvref_t<_Ty2>>;

template <class _Pr, class _Ty1, class _Ty2, bool _Order_check = _Enable_debug_lt_pred_order_check<_Pr, _Ty1, _Ty2>>
constexpr bool _Debug_lt_pred_order_check_noexcept =
noexcept(_STD declval<_Pr&>()(_STD declval<_Ty2&>(), _STD declval<_Ty1&>()));

template <class _Pr, class _Ty1, class _Ty2>
constexpr bool _Debug_lt_pred_order_check_noexcept<_Pr, _Ty1, _Ty2, false> = true;

template <class _Pr, class _Ty1, class _Ty2>
_NODISCARD constexpr bool _Debug_lt_pred(_Pr&& _Pred, _Ty1&& _Left, _Ty2&& _Right) noexcept(
noexcept(_Pred(_Left, _Right)) && _Debug_lt_pred_order_check_noexcept<_Pr, _Ty1, _Ty2>) {
const auto _Result = static_cast<bool>(_Pred(_Left, _Right));
if (_Result) {
_STL_VERIFY(!_Pred(_Right, _Left), "invalid comparator");

if constexpr (_Enable_debug_lt_pred_order_check<_Pr, _Ty1, _Ty2>) {
if (_Result) {
_STL_VERIFY(!_Pred(_Right, _Left), "invalid comparator");
}
}

return _Result;
}

template <class _Pr, class _Ty1, class _Ty2,
enable_if_t<!is_same_v<_Remove_cvref_t<_Ty1>, _Remove_cvref_t<_Ty2>>, int> = 0>
constexpr bool _Debug_lt_pred(_Pr&& _Pred, _Ty1&& _Left, _Ty2&& _Right) noexcept(noexcept(_Pred(_Left, _Right))) {
// test if _Pred(_Left, _Right); no debug checks as the types differ
return static_cast<bool>(_Pred(_Left, _Right));
}

template <class _InIt, class _Sentinel, class _Pr>
constexpr void _Debug_order_unchecked(_InIt _First, _Sentinel _Last, _Pr&& _Pred) {
// test if range is ordered by predicate
Expand Down

0 comments on commit d74be47

Please sign in to comment.