Skip to content

Commit fe86657

Browse files
[libc++] Implement comparison operators for tuple added in C++23 (llvm#148799)
And constrain the new `operator==` since C++26. This patch implements parts of P2165R4, P2944R3, and a possibly improved resolution of LWG3882. Currently, libstdc++ and MSVC STL constrain the new overloads in the same way. Also set feature-test macro `__cpp_lib_constrained_equality` and add related release note, as P2944R3 will completed with this patch. Fixes llvm#136765 Fixes llvm#136770 Fixes llvm#105424
1 parent 80a6bc7 commit fe86657

18 files changed

+852
-425
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ Status
432432
---------------------------------------------------------- -----------------
433433
``__cpp_lib_constexpr_queue`` ``202502L``
434434
---------------------------------------------------------- -----------------
435-
``__cpp_lib_constrained_equality`` *unimplemented*
435+
``__cpp_lib_constrained_equality`` ``202411L``
436436
---------------------------------------------------------- -----------------
437437
``__cpp_lib_copyable_function`` *unimplemented*
438438
---------------------------------------------------------- -----------------

libcxx/docs/ReleaseNotes/21.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Implemented Papers
5353
- P2711R1: Making multi-param constructors of ``views`` ``explicit`` (`Github <https://github.com/llvm/llvm-project/issues/105252>`__)
5454
- P2770R0: Stashing stashing ``iterators`` for proper flattening (`Github <https://github.com/llvm/llvm-project/issues/105250>`__)
5555
- P2655R3: ``common_reference_t`` of ``reference_wrapper`` Should Be a Reference Type (`Github <https://github.com/llvm/llvm-project/issues/105260>`__)
56+
- P2944R3: Comparisons for ``reference_wrapper`` (`Github <https://github.com/llvm/llvm-project/issues/105424>`__)
57+
- P3379R0: Constrain ``std::expected equality`` operators (`Github <https://github.com/llvm/llvm-project/issues/118135>`__)
5658

5759
Improvements and New Features
5860
-----------------------------

libcxx/docs/Status/Cxx23Papers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"`P1642R11 <https://wg21.link/P1642R11>`__","Freestanding ``[utilities]``, ``[ranges]``, and ``[iterators]``","2022-07 (Virtual)","","",""
6161
"`P1899R3 <https://wg21.link/P1899R3>`__","``stride_view``","2022-07 (Virtual)","","",""
6262
"`P2093R14 <https://wg21.link/P2093R14>`__","Formatted output","2022-07 (Virtual)","|Complete|","18",""
63-
"`P2165R4 <https://wg21.link/P2165R4>`__","Compatibility between ``tuple``, ``pair`` and ``tuple-like`` objects","2022-07 (Virtual)","|Partial|","","Only the part for ``zip_view`` is implemented."
63+
"`P2165R4 <https://wg21.link/P2165R4>`__","Compatibility between ``tuple``, ``pair`` and ``tuple-like`` objects","2022-07 (Virtual)","|Partial|","","Changes of ``tuple``, ``adjacent_view``, and ``cartesian_product_view`` are not yet implemented."
6464
"`P2278R4 <https://wg21.link/P2278R4>`__","``cbegin`` should always return a constant iterator","2022-07 (Virtual)","","",""
6565
"`P2286R8 <https://wg21.link/P2286R8>`__","Formatting Ranges","2022-07 (Virtual)","|Complete|","16",""
6666
"`P2291R3 <https://wg21.link/P2291R3>`__","Add Constexpr Modifiers to Functions ``to_chars`` and ``from_chars`` for Integral Types in ``<charconv>`` Header","2022-07 (Virtual)","|Complete|","16",""

libcxx/docs/Status/Cxx2cIssues.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,5 @@
149149
"`LWG3343 <https://wg21.link/LWG3343>`__","Ordering of calls to ``unlock()`` and ``notify_all()`` in Effects element of ``notify_all_at_thread_exit()`` should be reversed","Not Adopted Yet","|Complete|","16",""
150150
"`LWG4139 <https://wg21.link/LWG4139>`__","§[time.zone.leap] recursive constraint in <=>","Not Adopted Yet","|Complete|","20",""
151151
"`LWG3456 <https://wg21.link/LWG3456>`__","Pattern used by std::from_chars is underspecified (option B)","Not Adopted Yet","|Complete|","20",""
152+
"`LWG3882 <https://wg21.link/LWG3882>`__","``tuple`` relational operators have confused friendships","Not Adopted Yet","|Complete|","21","The comparsion operators are constrained harder than the proposed resolution. libstdc++ and MSVC STL do the same."
152153
"","","","","",""

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|","","The changes to ``tuple``'s equality overload from P2165R4 are not yet implemented."
62+
"`P2944R3 <https://wg21.link/P2944R3>`__","Comparisons for ``reference_wrapper``","2024-03 (Tokyo)","|Complete|","21",""
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/tuple

Lines changed: 105 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ public:
106106
107107
void swap(tuple&) noexcept(AND(swap(declval<T&>(), declval<T&>())...)); // constexpr in C++20
108108
constexpr void swap(const tuple&) const noexcept(see-below); // C++23
109+
110+
template<tuple-like UTuple>
111+
friend constexpr bool operator==(const tuple& t, const UTuple& u); // C++23
112+
template<tuple-like UTuple>
113+
friend constexpr auto operator<=>(const tuple& t, const UTuple& u); // C++23
109114
};
110115
111116
@@ -220,6 +225,7 @@ template <class... Types>
220225
# include <__config>
221226
# include <__cstddef/size_t.h>
222227
# include <__fwd/array.h>
228+
# include <__fwd/get.h>
223229
# include <__fwd/pair.h>
224230
# include <__fwd/tuple.h>
225231
# include <__memory/allocator_arg_t.h>
@@ -230,6 +236,7 @@ template <class... Types>
230236
# include <__tuple/sfinae_helpers.h>
231237
# include <__tuple/tuple_element.h>
232238
# include <__tuple/tuple_indices.h>
239+
# include <__tuple/tuple_like.h>
233240
# include <__tuple/tuple_like_ext.h>
234241
# include <__tuple/tuple_size.h>
235242
# include <__tuple/tuple_types.h>
@@ -288,6 +295,68 @@ _LIBCPP_BEGIN_NAMESPACE_STD
288295

289296
# ifndef _LIBCPP_CXX03_LANG
290297

298+
template <size_t _Ip, class _Tp, class _Up>
299+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool __tuple_compare_equal(const _Tp& __x, const _Up& __y) {
300+
if constexpr (_Ip == 0)
301+
return true;
302+
else
303+
return std::__tuple_compare_equal<_Ip - 1>(__x, __y) && std::get<_Ip - 1>(__x) == std::get<_Ip - 1>(__y);
304+
}
305+
306+
# if _LIBCPP_STD_VER >= 26
307+
template <class _Tp, class _Up, class _IndexSeq = make_index_sequence<tuple_size_v<_Tp>>>
308+
inline constexpr bool __can_tuple_compare_equal = false;
309+
310+
// TODO(LLVM 22): Remove `tuple_size_v<_Tp> == tuple_size_v<_Up>` here once once LLVM-20 support ends
311+
// because the resolution of CWG2369 landed in LLVM-21.
312+
template <class _Tp, class _Up, size_t... _Is>
313+
requires(tuple_size_v<_Tp> == tuple_size_v<_Up>)
314+
inline constexpr bool __can_tuple_compare_equal<_Tp, _Up, index_sequence<_Is...>> =
315+
__all<requires(const tuple_element_t<_Is, _Tp>& __t, const tuple_element_t<_Is, _Up>& __u) {
316+
{ __t == __u } -> __boolean_testable;
317+
}...>::value;
318+
# endif // _LIBCPP_STD_VER >= 26
319+
320+
# if _LIBCPP_STD_VER >= 20
321+
template <class _Ret, class _Tp, class _Up, size_t... _Is>
322+
_LIBCPP_HIDE_FROM_ABI constexpr _Ret __tuple_compare_three_way(const _Tp& __x, const _Up& __y, index_sequence<_Is...>) {
323+
_Ret __result = strong_ordering::equal;
324+
static_cast<void>(
325+
((__result = std::__synth_three_way(std::get<_Is>(__x), std::get<_Is>(__y)), __result != 0) || ...));
326+
return __result;
327+
}
328+
# endif // _LIBCPP_STD_VER >= 20
329+
330+
# if _LIBCPP_STD_VER >= 23
331+
template <class>
332+
inline constexpr bool __is_tuple_v = false;
333+
334+
template <class... _Tp>
335+
inline constexpr bool __is_tuple_v<tuple<_Tp...>> = true;
336+
337+
template <class _Tp>
338+
concept __tuple_like_no_tuple = __tuple_like<_Tp> && !__is_tuple_v<_Tp>;
339+
340+
template <class _Tp, class _Up, class _IndexSeq>
341+
struct __tuple_common_comparison_category_impl {};
342+
343+
// TODO(LLVM 22): Remove `tuple_size_v<_Tp> == tuple_size_v<_Up>` here once once LLVM-20 support ends
344+
// because the resolution of CWG2369 landed in LLVM-21.
345+
template <class _Tp, class _Up, size_t... _Is>
346+
requires(tuple_size_v<_Tp> == tuple_size_v<_Up>) && requires {
347+
typename common_comparison_category_t<
348+
__synth_three_way_result<tuple_element_t<_Is, _Tp>, tuple_element_t<_Is, _Up>>...>;
349+
}
350+
struct __tuple_common_comparison_category_impl<_Tp, _Up, index_sequence<_Is...>> {
351+
using type _LIBCPP_NODEBUG =
352+
common_comparison_category_t<__synth_three_way_result<tuple_element_t<_Is, _Tp>, tuple_element_t<_Is, _Up>>...>;
353+
};
354+
355+
template <__tuple_like _Tp, __tuple_like _Up>
356+
using __tuple_common_comparison_category _LIBCPP_NODEBUG =
357+
__tuple_common_comparison_category_impl<_Tp, _Up, make_index_sequence<tuple_size_v<_Tp>>>::type;
358+
# endif // _LIBCPP_STD_VER >= 23
359+
291360
// __tuple_leaf
292361

293362
template <size_t _Ip, class _Hp, bool = is_empty<_Hp>::value && !__libcpp_is_final<_Hp>::value >
@@ -997,7 +1066,24 @@ public:
9971066
noexcept(__all<is_nothrow_swappable_v<const _Tp&>...>::value) {
9981067
__base_.swap(__t.__base_);
9991068
}
1000-
# endif // _LIBCPP_STD_VER >= 23
1069+
1070+
template <__tuple_like_no_tuple _UTuple>
1071+
# if _LIBCPP_STD_VER >= 26
1072+
requires __can_tuple_compare_equal<tuple, _UTuple> && (sizeof...(_Tp) == tuple_size_v<_UTuple>)
1073+
# endif // _LIBCPP_STD_VER >= 26
1074+
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const tuple& __x, const _UTuple& __y) {
1075+
static_assert(sizeof...(_Tp) == tuple_size_v<_UTuple>, "Can't compare tuple-like values of different sizes");
1076+
return std::__tuple_compare_equal<sizeof...(_Tp)>(__x, __y);
1077+
}
1078+
1079+
template <__tuple_like_no_tuple _UTuple>
1080+
requires(sizeof...(_Tp) == tuple_size_v<_UTuple>)
1081+
_LIBCPP_HIDE_FROM_ABI friend constexpr __tuple_common_comparison_category<tuple, _UTuple>
1082+
operator<=>(const tuple& __x, const _UTuple& __y) {
1083+
return std::__tuple_compare_three_way<__tuple_common_comparison_category<tuple, _UTuple>>(
1084+
__x, __y, index_sequence_for<_Tp...>{});
1085+
}
1086+
# endif // _LIBCPP_STD_VER >= 23
10011087
};
10021088

10031089
_LIBCPP_DIAGNOSTIC_PUSH
@@ -1019,6 +1105,21 @@ public:
10191105
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(tuple&) _NOEXCEPT {}
10201106
# if _LIBCPP_STD_VER >= 23
10211107
_LIBCPP_HIDE_FROM_ABI constexpr void swap(const tuple&) const noexcept {}
1108+
1109+
template <__tuple_like_no_tuple _UTuple>
1110+
# if _LIBCPP_STD_VER >= 26
1111+
requires(tuple_size_v<_UTuple> == 0)
1112+
# endif // _LIBCPP_STD_VER >= 26
1113+
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const tuple&, const _UTuple&) {
1114+
static_assert(tuple_size_v<_UTuple> == 0, "Can't compare tuple-like values of different sizes");
1115+
return true;
1116+
}
1117+
1118+
template <__tuple_like_no_tuple _UTuple>
1119+
requires(tuple_size_v<_UTuple> == 0)
1120+
_LIBCPP_HIDE_FROM_ABI friend constexpr strong_ordering operator<=>(const tuple&, const _UTuple&) {
1121+
return strong_ordering::equal;
1122+
}
10221123
# endif
10231124
};
10241125
_LIBCPP_DIAGNOSTIC_POP
@@ -1137,22 +1238,6 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 tuple<_Tp&&...> forwa
11371238
return tuple<_Tp&&...>(std::forward<_Tp>(__t)...);
11381239
}
11391240

1140-
template <size_t _Ip>
1141-
struct __tuple_equal {
1142-
template <class _Tp, class _Up>
1143-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp& __x, const _Up& __y) {
1144-
return __tuple_equal<_Ip - 1>()(__x, __y) && std::get<_Ip - 1>(__x) == std::get<_Ip - 1>(__y);
1145-
}
1146-
};
1147-
1148-
template <>
1149-
struct __tuple_equal<0> {
1150-
template <class _Tp, class _Up>
1151-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator()(const _Tp&, const _Up&) {
1152-
return true;
1153-
}
1154-
};
1155-
11561241
template <class... _Tp, class... _Up>
11571242
# if _LIBCPP_STD_VER >= 26
11581243
requires(__all<requires(const _Tp& __t, const _Up& __u) {
@@ -1162,27 +1247,19 @@ template <class... _Tp, class... _Up>
11621247
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
11631248
operator==(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) {
11641249
static_assert(sizeof...(_Tp) == sizeof...(_Up), "Can't compare tuples of different sizes");
1165-
return __tuple_equal<sizeof...(_Tp)>()(__x, __y);
1250+
return std::__tuple_compare_equal<sizeof...(_Tp)>(__x, __y);
11661251
}
11671252

11681253
# if _LIBCPP_STD_VER >= 20
11691254

11701255
// operator<=>
11711256

1172-
template <class... _Tp, class... _Up, size_t... _Is>
1173-
_LIBCPP_HIDE_FROM_ABI constexpr auto
1174-
__tuple_compare_three_way(const tuple<_Tp...>& __x, const tuple<_Up...>& __y, index_sequence<_Is...>) {
1175-
common_comparison_category_t<__synth_three_way_result<_Tp, _Up>...> __result = strong_ordering::equal;
1176-
static_cast<void>(
1177-
((__result = std::__synth_three_way(std::get<_Is>(__x), std::get<_Is>(__y)), __result != 0) || ...));
1178-
return __result;
1179-
}
1180-
11811257
template <class... _Tp, class... _Up>
11821258
requires(sizeof...(_Tp) == sizeof...(_Up))
11831259
_LIBCPP_HIDE_FROM_ABI constexpr common_comparison_category_t<__synth_three_way_result<_Tp, _Up>...>
11841260
operator<=>(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) {
1185-
return std::__tuple_compare_three_way(__x, __y, index_sequence_for<_Tp...>{});
1261+
return std::__tuple_compare_three_way<common_comparison_category_t<__synth_three_way_result<_Tp, _Up>...>>(
1262+
__x, __y, index_sequence_for<_Tp...>{});
11861263
}
11871264

11881265
# else // _LIBCPP_STD_VER >= 20

libcxx/include/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ __cpp_lib_void_t 201411L <type_traits>
555555
# define __cpp_lib_constexpr_new 202406L
556556
# endif
557557
# define __cpp_lib_constexpr_queue 202502L
558-
// # define __cpp_lib_constrained_equality 202411L
558+
# define __cpp_lib_constrained_equality 202411L
559559
// # define __cpp_lib_copyable_function 202306L
560560
// # define __cpp_lib_debugging 202311L
561561
// # define __cpp_lib_default_template_type_for_algorithm_values 202403L

libcxx/test/std/language.support/support.limits/support.limits.general/expected.version.compile.pass.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,11 @@
9393

9494
#elif TEST_STD_VER > 23
9595

96-
# if !defined(_LIBCPP_VERSION)
97-
# ifndef __cpp_lib_constrained_equality
98-
# error "__cpp_lib_constrained_equality should be defined in c++26"
99-
# endif
100-
# if __cpp_lib_constrained_equality != 202411L
101-
# error "__cpp_lib_constrained_equality should have the value 202411L in c++26"
102-
# endif
103-
# else
104-
# ifdef __cpp_lib_constrained_equality
105-
# error "__cpp_lib_constrained_equality should not be defined because it is unimplemented in libc++!"
106-
# endif
96+
# ifndef __cpp_lib_constrained_equality
97+
# error "__cpp_lib_constrained_equality should be defined in c++26"
98+
# endif
99+
# if __cpp_lib_constrained_equality != 202411L
100+
# error "__cpp_lib_constrained_equality should have the value 202411L in c++26"
107101
# endif
108102

109103
# ifndef __cpp_lib_expected

libcxx/test/std/language.support/support.limits/support.limits.general/optional.version.compile.pass.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,11 @@
119119

120120
#elif TEST_STD_VER > 23
121121

122-
# if !defined(_LIBCPP_VERSION)
123-
# ifndef __cpp_lib_constrained_equality
124-
# error "__cpp_lib_constrained_equality should be defined in c++26"
125-
# endif
126-
# if __cpp_lib_constrained_equality != 202411L
127-
# error "__cpp_lib_constrained_equality should have the value 202411L in c++26"
128-
# endif
129-
# else
130-
# ifdef __cpp_lib_constrained_equality
131-
# error "__cpp_lib_constrained_equality should not be defined because it is unimplemented in libc++!"
132-
# endif
122+
# ifndef __cpp_lib_constrained_equality
123+
# error "__cpp_lib_constrained_equality should be defined in c++26"
124+
# endif
125+
# if __cpp_lib_constrained_equality != 202411L
126+
# error "__cpp_lib_constrained_equality should have the value 202411L in c++26"
133127
# endif
134128

135129
# if !defined(_LIBCPP_VERSION)

libcxx/test/std/language.support/support.limits/support.limits.general/tuple.version.compile.pass.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,11 @@
270270
# error "__cpp_lib_constexpr_tuple should have the value 201811L in c++26"
271271
# endif
272272

273-
# if !defined(_LIBCPP_VERSION)
274-
# ifndef __cpp_lib_constrained_equality
275-
# error "__cpp_lib_constrained_equality should be defined in c++26"
276-
# endif
277-
# if __cpp_lib_constrained_equality != 202411L
278-
# error "__cpp_lib_constrained_equality should have the value 202411L in c++26"
279-
# endif
280-
# else
281-
# ifdef __cpp_lib_constrained_equality
282-
# error "__cpp_lib_constrained_equality should not be defined because it is unimplemented in libc++!"
283-
# endif
273+
# ifndef __cpp_lib_constrained_equality
274+
# error "__cpp_lib_constrained_equality should be defined in c++26"
275+
# endif
276+
# if __cpp_lib_constrained_equality != 202411L
277+
# error "__cpp_lib_constrained_equality should have the value 202411L in c++26"
284278
# endif
285279

286280
# ifndef __cpp_lib_make_from_tuple

0 commit comments

Comments
 (0)