diff --git a/libcxx/include/__algorithm/three_way_comp_ref_type.h b/libcxx/include/__algorithm/three_way_comp_ref_type.h --- a/libcxx/include/__algorithm/three_way_comp_ref_type.h +++ b/libcxx/include/__algorithm/three_way_comp_ref_type.h @@ -27,6 +27,13 @@ _Comp& __comp_; _LIBCPP_HIDE_FROM_ABI constexpr __debug_three_way_comp(_Comp& __c) : __comp_(__c) {} + template + _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(const _Tp& __x, const _Up& __y) { + auto __r = __comp_(__x, __y); + __do_compare_assert(0, __y, __x, __r); + return __r; + } + template _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp& __x, _Up& __y) { auto __r = __comp_(__x, __y); @@ -55,7 +62,7 @@ // Pass the comparator by lvalue reference. Or in debug mode, using a // debugging wrapper that stores a reference. -# ifndef _LIBCPP_ENABLE_DEBUG_MODE +# ifdef _LIBCPP_ENABLE_DEBUG_MODE template using __three_way_comp_ref_type = __debug_three_way_comp<_Comp>; # else diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.three.way/lexicographical_compare_three_way_comp.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.three.way/lexicographical_compare_three_way_comp.pass.cpp --- a/libcxx/test/std/algorithms/alg.sorting/alg.three.way/lexicographical_compare_three_way_comp.pass.cpp +++ b/libcxx/test/std/algorithms/alg.sorting/alg.three.way/lexicographical_compare_three_way_comp.pass.cpp @@ -16,15 +16,15 @@ // Cmp comp) // -> decltype(comp(*b1, *b2)); -#include #include +#include #include #include #include #include -#include "test_macros.h" #include "test_iterators.h" +#include "test_macros.h" using std::array; @@ -155,7 +155,14 @@ // The comparator is invoked only `min(left.size(), right.size())` times test_lexicographical_compare( std::array{0, 1, 2}, std::array{0, 1, 2, 3}, compare_last_digit_counting, std::strong_ordering::less); - assert(compare_invocation_count == 3); + int expected_compare_invocation_count = 3; +#ifdef _LIBCPP_ENABLE_DEBUG_MODE + if constexpr (!std::is_constant_evaluated()) { + // In debug mode the comparator will be called twice by the debugging wrapper `__debug_three_way_comp` + expected_compare_invocation_count *= 2; + } +#endif + assert(compare_invocation_count == expected_compare_invocation_count); } constexpr bool test() {