[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 https://github.com/llvm/llvm-project/pull/135759
  - PR https://github.com/llvm/llvm-project/pull/117664

Closes: [#136763](https://github.com/llvm/llvm-project/issues/136763)

# References
- https://eel.is/c++draft/concept.booleantestable
- https://eel.is/c++draft/concept.equalitycomparable

---------

Co-authored-by: Hristo Hristov <zingam@outlook.com>
Co-authored-by: Nikolas Klauser <nikolasklauser@berlin.de>
This commit is contained in:
Hristo Hristov
2025-04-29 22:00:16 +03:00
committed by GitHub
parent e33b7a1d63
commit 4ed8f38e81
3 changed files with 36 additions and 2 deletions

View File

@@ -59,7 +59,7 @@
"`P2248R8 <https://wg21.link/P2248R8>`__","Enabling list-initialization for algorithms","2024-03 (Tokyo)","","",""
"`P2810R4 <https://wg21.link/P2810R4>`__","``is_debugger_present`` ``is_replaceable``","2024-03 (Tokyo)","","",""
"`P1068R11 <https://wg21.link/P1068R11>`__","Vector API for random number generation","2024-03 (Tokyo)","","",""
"`P2944R3 <https://wg21.link/P2944R3>`__","Comparisons for ``reference_wrapper``","2024-03 (Tokyo)","|Partial|","19","Implemented comparisons for ``reference_wrapper`` only"
"`P2944R3 <https://wg21.link/P2944R3>`__","Comparisons for ``reference_wrapper``","2024-03 (Tokyo)","|Partial|","","Implemented changes to ``reference_wrapper`` and ``pair``"
"`P2642R6 <https://wg21.link/P2642R6>`__","Padded ``mdspan`` layouts","2024-03 (Tokyo)","","",""
"`P3029R1 <https://wg21.link/P3029R1>`__","Better ``mdspan``'s CTAD","2024-03 (Tokyo)","|Complete|","19",""
"","","","","",""
1 Paper # Paper Name Meeting Status First released version Notes
59 `P2248R8 <https://wg21.link/P2248R8>`__ Enabling list-initialization for algorithms 2024-03 (Tokyo)
60 `P2810R4 <https://wg21.link/P2810R4>`__ ``is_debugger_present`` ``is_replaceable`` 2024-03 (Tokyo)
61 `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 Implemented changes to ``reference_wrapper`` and ``pair``
63 `P2642R6 <https://wg21.link/P2642R6>`__ Padded ``mdspan`` layouts 2024-03 (Tokyo)
64 `P3029R1 <https://wg21.link/P3029R1>`__ Better ``mdspan``'s CTAD 2024-03 (Tokyo) |Complete| 19
65

View File

@@ -11,6 +11,7 @@
#include <__compare/common_comparison_category.h>
#include <__compare/synth_three_way.h>
#include <__concepts/boolean_testable.h>
#include <__concepts/different_from.h>
#include <__config>
#include <__cstddef/size_t.h>
@@ -461,7 +462,14 @@ pair(_T1, _T2) -> pair<_T1, _T2>;
template <class _T1, class _T2, class _U1, class _U2>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
operator==(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y) {
operator==(const pair<_T1, _T2>& __x, const pair<_U1, _U2>& __y)
#if _LIBCPP_STD_VER >= 26
requires requires {
{ __x.first == __y.first } -> __boolean_testable;
{ __x.second == __y.second } -> __boolean_testable;
}
#endif
{
return __x.first == __y.first && __x.second == __y.second;
}

View File

@@ -19,9 +19,35 @@
#include <utility>
#include <cassert>
#include <concepts>
#include "test_macros.h"
#if TEST_STD_VER >= 26
// Test SFINAE.
struct EqualityComparable {
constexpr EqualityComparable(int value) : value_{value} {};
friend constexpr bool operator==(const EqualityComparable&, const EqualityComparable&) noexcept = default;
int value_;
};
static_assert(std::equality_comparable<EqualityComparable>);
static_assert(std::equality_comparable<std::pair<EqualityComparable, EqualityComparable>>);
struct NonComparable {};
static_assert(!std::equality_comparable<NonComparable>);
static_assert(!std::equality_comparable<std::pair<EqualityComparable, NonComparable>>);
static_assert(!std::equality_comparable<std::pair<NonComparable, EqualityComparable>>);
#endif // TEST_STD_VER >= 26
int main(int, char**)
{
{