Skip to content

Commit 4ed8f38

Browse files
H-G-HristovZingamphilnik777
authored
[libc++][pair] P2944R3: Constrain std::pair's equality operator (#136672)
Implements https://wg21.link/P2944R3 (partially): - [pairs.spec](https://eel.is/c++draft/pairs.spec) Related issues: - Related to #105424 - Related to #118135 - PR #135759 - PR #117664 Closes: [#136763](#136763) # References - https://eel.is/c++draft/concept.booleantestable - https://eel.is/c++draft/concept.equalitycomparable --------- Co-authored-by: Hristo Hristov <[email protected]> Co-authored-by: Nikolas Klauser <[email protected]>
1 parent e33b7a1 commit 4ed8f38

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

libcxx/docs/Status/Cxx2cPapers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"`P2248R8 <https://wg21.link/P2248R8>`__","Enabling list-initialization for algorithms","2024-03 (Tokyo)","","",""
6060
"`P2810R4 <https://wg21.link/P2810R4>`__","``is_debugger_present`` ``is_replaceable``","2024-03 (Tokyo)","","",""
6161
"`P1068R11 <https://wg21.link/P1068R11>`__","Vector API for random number generation","2024-03 (Tokyo)","","",""
62-
"`P2944R3 <https://wg21.link/P2944R3>`__","Comparisons for ``reference_wrapper``","2024-03 (Tokyo)","|Partial|","19","Implemented comparisons for ``reference_wrapper`` only"
62+
"`P2944R3 <https://wg21.link/P2944R3>`__","Comparisons for ``reference_wrapper``","2024-03 (Tokyo)","|Partial|","","Implemented changes to ``reference_wrapper`` and ``pair``"
6363
"`P2642R6 <https://wg21.link/P2642R6>`__","Padded ``mdspan`` layouts","2024-03 (Tokyo)","","",""
6464
"`P3029R1 <https://wg21.link/P3029R1>`__","Better ``mdspan``'s CTAD","2024-03 (Tokyo)","|Complete|","19",""
6565
"","","","","",""

libcxx/include/__utility/pair.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <__compare/common_comparison_category.h>
1313
#include <__compare/synth_three_way.h>
14+
#include <__concepts/boolean_testable.h>
1415
#include <__concepts/different_from.h>
1516
#include <__config>
1617
#include <__cstddef/size_t.h>
@@ -461,7 +462,14 @@ pair(_T1, _T2) -> pair<_T1, _T2>;
461462

462463
template <class _T1, class _T2, class _U1, class _U2>
463464
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
464-
operator==(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) {
465+
operator==(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y)
466+
#if _LIBCPP_STD_VER >= 26
467+
requires requires {
468+
{ __x.first == __y.first } -> __boolean_testable;
469+
{ __x.second == __y.second } -> __boolean_testable;
470+
}
471+
#endif
472+
{
465473
return __x.first == __y.first && __x.second == __y.second;
466474
}
467475

libcxx/test/std/utilities/utility/pairs/pairs.spec/comparison.pass.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,35 @@
1919

2020
#include <utility>
2121
#include <cassert>
22+
#include <concepts>
2223

2324
#include "test_macros.h"
2425

26+
#if TEST_STD_VER >= 26
27+
28+
// Test SFINAE.
29+
30+
struct EqualityComparable {
31+
constexpr EqualityComparable(int value) : value_{value} {};
32+
33+
friend constexpr bool operator==(const EqualityComparable&, const EqualityComparable&) noexcept = default;
34+
35+
int value_;
36+
};
37+
38+
static_assert(std::equality_comparable<EqualityComparable>);
39+
40+
static_assert(std::equality_comparable<std::pair<EqualityComparable, EqualityComparable>>);
41+
42+
struct NonComparable {};
43+
44+
static_assert(!std::equality_comparable<NonComparable>);
45+
46+
static_assert(!std::equality_comparable<std::pair<EqualityComparable, NonComparable>>);
47+
static_assert(!std::equality_comparable<std::pair<NonComparable, EqualityComparable>>);
48+
49+
#endif // TEST_STD_VER >= 26
50+
2551
int main(int, char**)
2652
{
2753
{

0 commit comments

Comments
 (0)