Debian Codegraph @

libstdc++-v3/testsuite/20_util/tuple/p2165r4.cc

raw
1 // Verify P2165R4 enhancements to std::tuple.
2 // { dg-do run { target c++23 } }
3
4 #include <array>
5 #include <tuple>
6 #include <utility>
7 #include <memory>
8 #include <testsuite_hooks.h>
9
10 using std::array;
11 using std::pair;
12 using std::tuple;
13 using std::allocator;
14 using std::allocator_arg_t;
15 using std::allocator_arg;
16
17 namespace alloc {
18 struct B01;
19 struct B02;
20 }
21
22 template<> struct std::uses_allocator<alloc::B01, allocator<int>> : std::true_type { };
23 template<> struct std::uses_allocator<alloc::B02, allocator<int>> : std::true_type { };
24
25 struct A { };
26
27 template<template<typename> class tuple_like_t>
28 constexpr bool
29 test01()
30 {
31 struct B {
32 int m;
33 constexpr B(A&) : m(0) { }
34 constexpr B(A&&) : m(1) { }
35 constexpr B(const A&) : m(2) { }
36 constexpr B(const A&&) : m(3) { }
37 };
38
39 // template<tuple-like UTuple>
40 // constexpr explicit(false) tuple(UTuple&&);
41
42 tuple_like_t<A> tuple_like;
43
44 [&] {
45 tuple<B, B, B> t3b = tuple_like;
46 VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 );
47 }();
48 [&] {
49 tuple<B, B, B> t3b = std::move(tuple_like);
50 VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 );
51 }();
52 [&] {
53 tuple<B, B, B> t3b = std::as_const(tuple_like);
54 VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 );
55 }();
56 [&] {
57 tuple<B, B, B> t3b = std::move(std::as_const(tuple_like));
58 VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 );
59 }();
60
61 // Verify dangling checks.
62 static_assert( !std::is_constructible_v<tuple<const int&, int, int>, tuple_like_t<long>> );
63 static_assert( !std::is_constructible_v<tuple<int, const int&, int>, tuple_like_t<long>> );
64 static_assert( !std::is_constructible_v<tuple<int, int, const int&>, tuple_like_t<long>> );
65
66 return true;
67 }
68
69 namespace alloc
70 {
71 struct B01 {
72 int m;
73 B01(A&);
74 B01(A&&);
75 B01(const A&);
76 B01(const A&&);
77 constexpr B01(allocator_arg_t, allocator<int>, A&) : m(0) { }
78 constexpr B01(allocator_arg_t, allocator<int>, A&&) : m(1) { }
79 constexpr B01(allocator_arg_t, allocator<int>, const A&) : m(2) { }
80 constexpr B01(allocator_arg_t, allocator<int>, const A&&) : m(3) { }
81 };
82
83 template<template<typename> class tuple_like_t>
84 constexpr bool
85 test01()
86 {
87 using B = B01;
88
89 // template<tuple-like UTuple>
90 // constexpr explicit(false) tuple(allocator_arg_t, const Alloc&, UTuple&&);
91
92 tuple_like_t<A> tuple_like;
93
94 [&] {
95 tuple<B, B, B> t3b = {allocator_arg, allocator<int>{}, tuple_like};
96 VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 );
97 }();
98 [&] {
99 tuple<B, B, B> t3b = {allocator_arg, allocator<int>{}, std::move(tuple_like)};
100 VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 );
101 }();
102 [&] {
103 tuple<B, B, B> t3b = {allocator_arg, allocator<int>{}, std::as_const(tuple_like)};
104 VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 );
105 }();
106 [&] {
107 tuple<B, B, B> t3b = {allocator_arg, allocator<int>{}, std::move(std::as_const(tuple_like))};
108 VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 );
109 }();
110
111 // Verify dangling checks.
112 static_assert( !std::is_constructible_v<tuple<const int&, int, int>,
113 allocator_arg_t, allocator<int>,
114 tuple_like_t<long>> );
115 static_assert( !std::is_constructible_v<tuple<int, const int&, int>,
116 allocator_arg_t, allocator<int>,
117 tuple_like_t<long>> );
118 static_assert( !std::is_constructible_v<tuple<int, int, const int&>,
119 allocator_arg_t, allocator<int>,
120 tuple_like_t<long>> );
121
122 return true;
123 }
124 }
125
126 template<template<typename> class tuple_like_t>
127 constexpr bool
128 test02()
129 {
130 struct B {
131 int m;
132 constexpr explicit B(A&) : m(0) { }
133 constexpr explicit B(A&&) : m(1) { }
134 constexpr explicit B(const A&) : m(2) { }
135 constexpr explicit B(const A&&) : m(3) { }
136 };
137
138 // template<tuple-like UTuple>
139 // constexpr explicit(true) tuple(UTuple&&);
140
141 static_assert( !std::is_convertible_v<tuple_like_t<A>, tuple<B, B, B>> );
142
143 tuple_like_t<A> tuple_like;
144
145 [&] {
146 tuple<B, B, B> t3b{tuple_like};
147 VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 );
148 }();
149 [&] {
150 tuple<B, B, B> t3b{std::move(tuple_like)};
151 VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 );
152 }();
153 [&] {
154 tuple<B, B, B> t3b{std::as_const(tuple_like)};
155 VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 );
156 }();
157 [&] {
158 tuple<B, B, B> t3b{std::move(std::as_const(tuple_like))};
159 VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 );
160 }();
161
162 return true;
163 }
164
165 namespace alloc
166 {
167 struct B02 {
168 int m;
169 explicit B02(A&);
170 explicit B02(A&&);
171 explicit B02(const A&);
172 explicit B02(const A&&);
173 explicit constexpr B02(allocator_arg_t, allocator<int>, A&) : m(0) { }
174 explicit constexpr B02(allocator_arg_t, allocator<int>, A&&) : m(1) { }
175 explicit constexpr B02(allocator_arg_t, allocator<int>, const A&) : m(2) { }
176 explicit constexpr B02(allocator_arg_t, allocator<int>, const A&&) : m(3) { }
177 };
178
179 template<template<typename> class tuple_like_t>
180 constexpr bool
181 test02()
182 {
183 using B = B02;
184
185 // template<tuple-like UTuple>
186 // constexpr explicit(true) tuple(allocator_arg_t, const Alloc&, UTuple&&);
187
188 static_assert( !std::is_convertible_v<tuple_like_t<A>, tuple<B, B, B>> );
189
190 tuple_like_t<A> tuple_like;
191
192 [&] {
193 tuple<B, B, B> t3b{allocator_arg, allocator<int>{}, tuple_like};
194 VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 );
195 }();
196 [&] {
197 tuple<B, B, B> t3b{allocator_arg, allocator<int>{}, std::move(tuple_like)};
198 VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 );
199 }();
200 [&] {
201 tuple<B, B, B> t3b{allocator_arg, allocator<int>{}, std::as_const(tuple_like)};
202 VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 );
203 }();
204 [&] {
205 tuple<B, B, B> t3b{allocator_arg, allocator<int>{}, std::move(std::as_const(tuple_like))};
206 VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 );
207 }();
208
209 return true;
210 }
211 }
212
213
214 template<template<typename> class tuple_like_t>
215 constexpr bool
216 test03()
217 {
218 struct B {
219 int m;
220 constexpr B& operator=(A&) { m = 0; return *this; }
221 constexpr B& operator=(A&&) { m = 1; return *this; }
222 constexpr B& operator=(const A&) { m = 2; return *this; }
223 constexpr B& operator=(const A&&) { m = 3; return *this; }
224 };
225
226 // template<tuple-like UTuple>
227 // constexpr tuple& operator=(UTuple&&);
228
229 tuple_like_t<A> tuple_like;
230
231 tuple<B, B, B> t3b;
232 t3b = tuple_like;
233 VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 );
234 t3b = std::move(tuple_like);
235 VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 );
236 t3b = std::as_const(tuple_like);
237 VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 );
238 t3b = std::move(std::as_const(tuple_like));
239 VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 );
240
241 return true;
242 }
243
244 template<template<typename> class tuple_like_t>
245 constexpr bool
246 test04()
247 {
248 struct B {
249 mutable int m;
250 constexpr const B& operator=(A&) const { m = 0; return *this; }
251 constexpr const B& operator=(A&&) const { m = 1; return *this; }
252 constexpr const B& operator=(const A&) const { m = 2; return *this; }
253 constexpr const B& operator=(const A&&) const { m = 3; return *this; }
254 };
255
256 // template<tuple-like UTuple>
257 // constexpr const tuple& operator=(UTuple&&) const;
258
259 tuple_like_t<A> tuple_like;
260
261 const tuple<B, B, B> t3b;
262 t3b = tuple_like;
263 VERIFY( std::get<0>(t3b).m == 0 && std::get<1>(t3b).m == 0 && std::get<2>(t3b).m == 0 );
264 t3b = std::move(tuple_like);
265 VERIFY( std::get<0>(t3b).m == 1 && std::get<1>(t3b).m == 1 && std::get<2>(t3b).m == 1 );
266 t3b = std::as_const(tuple_like);
267 VERIFY( std::get<0>(t3b).m == 2 && std::get<1>(t3b).m == 2 && std::get<2>(t3b).m == 2 );
268 t3b = std::move(std::as_const(tuple_like));
269 VERIFY( std::get<0>(t3b).m == 3 && std::get<1>(t3b).m == 3 && std::get<2>(t3b).m == 3 );
270
271 return true;
272 }
273
274 template<template<typename> class tuple_like_t>
275 constexpr bool
276 test05()
277 {
278 // template<tuple-like UTuple>
279 // constexpr bool operator==(const tuple&, const UTuple&);
280
281 static_assert( tuple{1, 2, 3} == tuple_like_t{1, 2, 3} );
282 static_assert( tuple{1, 2, 4} != tuple_like_t{1, 2, 3} );
283 static_assert( tuple_like_t{1, 2, 3} == tuple{1, 2, 3} );
284 static_assert( tuple_like_t{1, 2, 3} != tuple{1, 2, 4} );
285
286 // template<tuple-like UTuple>
287 // constexpr bool operator<=>const tuple&, const UTuple&);
288
289 static_assert( (tuple{1, 2, 3} <=> tuple_like_t{1, 2, 3}) == std::strong_ordering::equal );
290 static_assert( (tuple{1, 2, 4} <=> tuple_like_t{1, 2, 3}) == std::strong_ordering::greater );
291 static_assert( (tuple_like_t{1, 2, 3} <=> tuple{1, 2, 3}) == std::strong_ordering::equal );
292 static_assert( (tuple_like_t{1, 2, 3} <=> tuple{1, 2, 4}) == std::strong_ordering::less );
293
294 static_assert( tuple{1, 2, 4} > tuple_like_t{1, 2, 3} );
295 static_assert( tuple_like_t{1, 2, 3} < tuple{1, 2, 4} );
296
297 // template<tuple-like TTuple, tuple-like UTuple, ...>
298 // struct basic_common_reference<TTuple, UTuple, ...>;
299
300 static_assert( std::same_as<std::common_reference_t<tuple_like_t<int>,
301 tuple<int, long, int>>,
302 tuple<int, long, int>> );
303
304 static_assert( std::same_as<std::common_reference_t<tuple<int, long, int>,
305 tuple_like_t<int>>,
306 tuple<int, long, int>> );
307
308 // template<tuple-like TTuple, tuple-like UTuple>
309 // struct common_type<TTuple, UTuple>;
310
311 static_assert( std::same_as<std::common_type_t<tuple_like_t<const int&>,
312 tuple<int, long, int>>,
313 tuple<int, long, int>> );
314
315 static_assert( std::same_as<std::common_type_t<tuple<int, long, int>,
316 tuple_like_t<const int&>>,
317 tuple<int, long, int>> );
318
319 return true;
320 }
321
322 template<typename T>
323 using tuple_like_array = array<T, 3>;
324
325 int
326 main()
327 {
328 static_assert( test01<tuple_like_array>() );
329 static_assert( alloc::test01<tuple_like_array>() );
330 static_assert( test02<tuple_like_array>() );
331 static_assert( alloc::test02<tuple_like_array>() );
332 static_assert( test03<tuple_like_array>() );
333 static_assert( test04<tuple_like_array>() );
334 static_assert( test05<tuple_like_array>() );
335 }