Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace enable_if_t in _Debug_lt_pred with if constexpr and variable template #4610

Merged
merged 9 commits into from
Apr 26, 2024
30 changes: 17 additions & 13 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>
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(declval<_Pr>()(declval<_Ty1>(), declval<_Ty2>()));
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

template <class _Pr, class _Ty1, class _Ty2>
constexpr bool _Debug_lt_pred_order_check_noexcept<_Pr, _Ty1, _Ty2, true> = noexcept(
declval<_Pr>()(declval<_Ty1>(), declval<_Ty2>()))&& noexcept(declval<_Pr>()(declval<_Ty2>(), declval<_Ty1>()));
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

template <class _Pr, class _Ty1, class _Ty2>
constexpr bool _Debug_lt_pred(_Pr&& _Pred, _Ty1&& _Left, _Ty2&& _Right) noexcept(
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
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
_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