Skip to content

Commit ab710f4

Browse files
committed
Implement tuple
1 parent a7517fd commit ab710f4

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
lines changed

libcxx/include/tuple

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ template <class... Types>
229229
# include <__tuple/sfinae_helpers.h>
230230
# include <__tuple/tuple_element.h>
231231
# include <__tuple/tuple_indices.h>
232+
// # include <__tuple/tuple_like.h>
232233
# include <__tuple/tuple_like_ext.h>
233234
# include <__tuple/tuple_size.h>
234235
# include <__tuple/tuple_types.h>
@@ -1161,12 +1162,35 @@ struct __tuple_equal<0> {
11611162
};
11621163

11631164
template <class... _Tp, class... _Up>
1165+
# if _LIBCPP_STD_VER >= 26
1166+
requires(requires(const _Tp& __t, const _Up& __u) {
1167+
{ __t == __u } -> __boolean_testable;
1168+
} && ...) && (sizeof...(_Tp) == sizeof...(_Up))
1169+
# endif
11641170
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
11651171
operator==(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) {
1172+
# if _LIBCPP_STD_VER < 26
11661173
static_assert(sizeof...(_Tp) == sizeof...(_Up), "Can't compare tuples of different sizes");
1174+
# endif
11671175
return __tuple_equal<sizeof...(_Tp)>()(__x, __y);
11681176
}
11691177

1178+
// # if _LIBCPP_STD_VER >= 23
1179+
// template <class... _Tp, __tuple_like _UTuple>
1180+
// # if _LIBCPP_STD_VER >= 26
1181+
// requires((requires(const _Tp& __t, const _UTuple& __u) {
1182+
// { __t == __u } -> __boolean_testable;
1183+
// } && ...) && (sizeof...(_Tp) == tuple_size_v<_UTuple>))
1184+
// # endif
1185+
// _LIBCPP_HIDE_FROM_ABI constexpr bool
1186+
// operator==(const tuple<_Tp...>& __t, const _UTuple& __u) {
1187+
// # if _LIBCPP_STD_VER < 26
1188+
// static_assert(sizeof...(_Tp) == tuple_size_v<_UTuple>, "Can't compare a tuple and a tuple-like of different sizes");
1189+
// # endif
1190+
// return __tuple_equal<sizeof...(_Tp)>()(__t, __u);
1191+
// }
1192+
// # endif
1193+
11701194
# if _LIBCPP_STD_VER >= 20
11711195

11721196
// operator<=>

libcxx/include/variant

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ struct __convert_to_bool {
14561456
template <class... _Types>
14571457
# if _LIBCPP_STD_VER >= 26
14581458
requires(requires(const _Types& __t) {
1459-
{ __t == __t } -> __core_convertible_to<bool>
1459+
{ __t == __t } -> __core_convertible_to<bool>;
14601460
} && ...)
14611461
# endif
14621462
_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
@@ -1493,7 +1493,7 @@ operator<=>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
14931493
template <class... _Types>
14941494
# if _LIBCPP_STD_VER >= 26
14951495
requires(requires(const _Types& __t) {
1496-
{ __t != __t } -> __core_convertible_to<bool>
1496+
{ __t != __t } -> __core_convertible_to<bool>;
14971497
} && ...)
14981498
# endif
14991499
_LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
@@ -1508,7 +1508,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr bool operator!=(const variant<_Types...>& __lhs,
15081508
template <class... _Types>
15091509
# if _LIBCPP_STD_VER >= 26
15101510
requires(requires(const _Types& __t) {
1511-
{ __t < __t } -> __core_convertible_to<bool>
1511+
{ __t < __t } -> __core_convertible_to<bool>;
15121512
} && ...)
15131513
# endif
15141514
_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
@@ -1527,7 +1527,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const variant<_Types...>& __lhs,
15271527
template <class... _Types>
15281528
# if _LIBCPP_STD_VER >= 26
15291529
requires(requires(const _Types& __t) {
1530-
{ __t > __t } -> __core_convertible_to<bool>
1530+
{ __t > __t } -> __core_convertible_to<bool>;
15311531
} && ...)
15321532
# endif
15331533
_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
@@ -1546,7 +1546,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const variant<_Types...>& __lhs,
15461546
template <class... _Types>
15471547
# if _LIBCPP_STD_VER >= 26
15481548
requires(requires(const _Types& __t) {
1549-
{ __t <= __t } -> __core_convertible_to<bool>
1549+
{ __t <= __t } -> __core_convertible_to<bool>;
15501550
} && ...)
15511551
# endif
15521552
_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {
@@ -1565,7 +1565,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const variant<_Types...>& __lhs,
15651565
template <class... _Types>
15661566
# if _LIBCPP_STD_VER >= 26
15671567
requires(requires(const _Types& __t) {
1568-
{ __t >= __t } -> __core_convertible_to<bool>
1568+
{ __t >= __t } -> __core_convertible_to<bool>;
15691569
} && ...)
15701570
# endif
15711571
_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const variant<_Types...>& __lhs, const variant<_Types...>& __rhs) {

libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/eq.pass.cpp

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,61 @@
1111
// template <class... Types> class tuple;
1212

1313
// template<class... TTypes, class... UTypes>
14-
// bool
15-
// operator==(const tuple<TTypes...>& t, const tuple<UTypes...>& u);
14+
// constexpr bool operator==(const tuple<TTypes...>&, const tuple<UTypes...>&); // constexpr since C++14
15+
16+
// constexpr bool operator==(const tuple<TTypes...>&, const UTuple&);
17+
// template<class... TTypes, class... UTypes> // Since C++23
1618

1719
// UNSUPPORTED: c++03
1820

19-
#include <tuple>
20-
#include <string>
21+
#include <array>
2122
#include <cassert>
23+
#include <tuple>
2224

25+
#include "test_comparisons.h"
2326
#include "test_macros.h"
2427

28+
#if TEST_STD_VER >= 26
29+
30+
// Test SFINAE.
31+
32+
// ==(const tuple<>&, const tuple<>&);
33+
34+
static_assert(std::equality_comparable<std::tuple<EqualityComparable>>);
35+
static_assert(std::equality_comparable<std::tuple<EqualityComparable, EqualityComparable>>);
36+
37+
static_assert(!std::equality_comparable<std::tuple<NonComparable>>);
38+
static_assert(!std::equality_comparable<std::tuple<EqualityComparable, NonComparable>>);
39+
static_assert(!std::equality_comparable<std::tuple<NonComparable, EqualityComparable>>);
40+
static_assert(!std::equality_comparable_with<std::tuple<EqualityComparable>, std::tuple<NonComparable>>);
41+
static_assert(!std::equality_comparable_with<std::tuple<NonComparable>, std::tuple<EqualityComparable>>);
42+
// Size mismatch.
43+
static_assert(!std::equality_comparable_with<std::tuple<EqualityComparable>, std::tuple<EqualityComparable, EqualityComparable>>);
44+
static_assert(!std::equality_comparable_with<std::tuple<EqualityComparable, EqualityComparable>, std::tuple<EqualityComparable>>);
45+
46+
// ==(const tuple<>&, const tuple-like&);
47+
48+
// static_assert(std::equality_comparable_with<std::tuple<EqualityComparable, EqualityComparable>, std::pair<EqualityComparable, EqualityComparable>>);
49+
// static_assert(std::equality_comparable_with<std::tuple<EqualityComparable, EqualityComparable>, std::array<EqualityComparable, 2>>);
50+
51+
// static_assert(!std::equality_comparable_with<std::tuple<EqualityComparable, NonComparable>, std::pair<EqualityComparable, NonComparable>>);
52+
// static_assert(!std::equality_comparable_with<std::tuple<EqualityComparable, NonComparable>, std::array<EqualityComparable, 2>>);
53+
// // Size mismatch.
54+
// static_assert(!std::equality_comparable_with<std::tuple<EqualityComparable>, std::pair<EqualityComparable, EqualityComparable>>);
55+
// static_assert(!std::equality_comparable_with<std::tuple<EqualityComparable, EqualityComparable>, std::array<EqualityComparable, 94>>);
56+
57+
#endif
58+
2559
int main(int, char**)
2660
{
61+
// {
62+
// using T1 = std::tuple<int>;
63+
// using T2 = std::array<int, 1>;
64+
// const T1 t1(1);
65+
// const T2 t2{1};
66+
// assert(t1 == t2);
67+
// assert(!(t1 != t2));
68+
// }
2769
{
2870
typedef std::tuple<> T1;
2971
typedef std::tuple<> T2;

libcxx/test/std/utilities/tuple/tuple.tuple/tuple.rel/size_incompatible_comparison.verify.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@
2121

2222
#include <tuple>
2323

24+
#include "test_macros.h"
25+
26+
#if TEST_STD_VER >= 26
27+
// expected-no-diagnostics
28+
#else
2429
void f(std::tuple<int> t1, std::tuple<int, long> t2) {
2530
// We test only the core comparison operators and trust that the others
2631
// fall back on the same implementations prior to C++20.
2732
static_cast<void>(t1 == t2); // expected-error@*:* {{}}
2833
static_cast<void>(t1 < t2); // expected-error@*:* {{}}
2934
}
35+
#endif

0 commit comments

Comments
 (0)