-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc++][pair] P2944R3: Constrain std::pair
's equality operator
#136672
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
[libc++][pair] P2944R3: Constrain std::pair
's equality operator
#136672
Conversation
Implements https://wg21.link/P2944R3 (partially): - [pairs.spec](https://eel.is/c++draft/pairs.spec) Related issues: - Related to llvm#105424 - Related to llvm#118135
✅ With the latest revision this PR passed the C/C++ code formatter. |
std::pair
's equality operator
@llvm/pr-subscribers-libcxx Author: Hristo Hristov (H-G-Hristov) ChangesImplements https://wg21.link/P2944R3 (partially): Related issues:
Closes: #136763 ReferencesFull diff: https://github.com/llvm/llvm-project/pull/136672.diff 3 Files Affected:
diff --git a/libcxx/docs/Status/Cxx2cPapers.csv b/libcxx/docs/Status/Cxx2cPapers.csv
index 0cc41d2058dd5..0ed0e9f3ccb7c 100644
--- a/libcxx/docs/Status/Cxx2cPapers.csv
+++ b/libcxx/docs/Status/Cxx2cPapers.csv
@@ -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|","21","Implemented changes to ``reference_wrapper``, ``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",""
"","","","","",""
diff --git a/libcxx/include/__utility/pair.h b/libcxx/include/__utility/pair.h
index 1f596a87f7cc7..72363cac9ebec 100644
--- a/libcxx/include/__utility/pair.h
+++ b/libcxx/include/__utility/pair.h
@@ -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>
@@ -447,7 +448,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;
}
diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.spec/comparison.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.spec/comparison.pass.cpp
index 76f9771f2b99b..c472906c5ed7f 100644
--- a/libcxx/test/std/utilities/utility/pairs/pairs.spec/comparison.pass.cpp
+++ b/libcxx/test/std/utilities/utility/pairs/pairs.spec/comparison.pass.cpp
@@ -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**)
{
{
|
requires requires { | ||
{ __x.first == __y.first } -> __boolean_testable; | ||
{ __x.second == __y.second } -> __boolean_testable; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have the same situation as in #135759? (that __boolean_testable
might be too strict)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The standard requirements are inconsistent among these new constraints. boolean-testable
is used for pair
and tuple
's operator==
s (perhaps due to boolean-testable
in old Preconditions added by P2167R3), while plain implicit convertibility is used elsewhere.
I guess the difference was because of that &&
(and possibly ||
?) is expected to be used in pair
and tuple
's operator==
s, but not in other mentioned operators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @frederick-vs-ja pointed out these are described as __boolean_testable
in the standard so I prefer to keep them as such for consistency with the Standard but I'll update the reference_wrapper
with the proposed concept in #135759 later.
Co-authored-by: Nikolas Klauser <nikolasklauser@berlin.de>
@philnik777 Do you have any further comments? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM % nit.
Co-authored-by: Nikolas Klauser <nikolasklauser@berlin.de>
Thank you! |
…vm#136672) Implements https://wg21.link/P2944R3 (partially): - [pairs.spec](https://eel.is/c++draft/pairs.spec) Related issues: - Related to llvm#105424 - Related to llvm#118135 - PR llvm#135759 - PR llvm#117664 Closes: [llvm#136763](llvm#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>
…vm#136672) Implements https://wg21.link/P2944R3 (partially): - [pairs.spec](https://eel.is/c++draft/pairs.spec) Related issues: - Related to llvm#105424 - Related to llvm#118135 - PR llvm#135759 - PR llvm#117664 Closes: [llvm#136763](llvm#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>
…vm#136672) Implements https://wg21.link/P2944R3 (partially): - [pairs.spec](https://eel.is/c++draft/pairs.spec) Related issues: - Related to llvm#105424 - Related to llvm#118135 - PR llvm#135759 - PR llvm#117664 Closes: [llvm#136763](llvm#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>
…vm#136672) Implements https://wg21.link/P2944R3 (partially): - [pairs.spec](https://eel.is/c++draft/pairs.spec) Related issues: - Related to llvm#105424 - Related to llvm#118135 - PR llvm#135759 - PR llvm#117664 Closes: [llvm#136763](llvm#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>
…vm#136672) Implements https://wg21.link/P2944R3 (partially): - [pairs.spec](https://eel.is/c++draft/pairs.spec) Related issues: - Related to llvm#105424 - Related to llvm#118135 - PR llvm#135759 - PR llvm#117664 Closes: [llvm#136763](llvm#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>
Implements https://wg21.link/P2944R3 (partially):
Related issues:
reference_wrapper
#105424std::expected
equality operators #135759std::expected
equality operators #117664Closes: #136763
References