Debian Codegraph @

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

raw
1 // Verify P2321R2 "zip" enhancements to std::tuple.
2 // { dg-do run { target c++23 } }
3 // FIXME [!HOSTED]: avoidable std::allocator usage
4 // { dg-require-effective-target hosted }
5 // { dg-add-options no_pch }
6
7 #include <tuple>
8
9 #if __cpp_lib_ranges_zip != 202110L
10 # error "Feature-test macro __cpp_lib_ranges_zip has wrong value in <tuple>"
11 #endif
12
13 #include <memory>
14 #include <testsuite_hooks.h>
15
16 using std::tuple;
17 using std::pair;
18 using std::allocator;
19 using std::allocator_arg_t;
20 using std::allocator_arg;
21
22 namespace alloc {
23 struct B01;
24 struct B02;
25 struct B03;
26 struct B04;
27 }
28
29 template<> struct std::uses_allocator<alloc::B01, allocator<int>> : std::true_type { };
30 template<> struct std::uses_allocator<alloc::B02, allocator<int>> : std::true_type { };
31 template<> struct std::uses_allocator<alloc::B03, allocator<int>> : std::true_type { };
32 template<> struct std::uses_allocator<alloc::B04, allocator<int>> : std::true_type { };
33
34 struct A { };
35
36 constexpr bool
37 test01()
38 {
39 struct B { bool v; constexpr B(A&) : v(true) { } };
40
41 // template<class... UTypes>
42 // constexpr explicit(false) tuple(tuple<UTypes...>&);
43
44 tuple<A> t1a;
45 tuple<B> t1b = t1a;
46 VERIFY( std::get<0>(t1b).v );
47
48 tuple<A, int> t2a0;
49 tuple<B, int> t2b0 = t2a0;
50 VERIFY( std::get<0>(t2b0).v );
51
52 tuple<int, A> t2a1;
53 tuple<int, B> t2b1 = t2a1;
54 VERIFY( std::get<1>(t2b1).v );
55
56 tuple<A, int, int> t3a0;
57 tuple<B, int, int> t3b0 = t3a0;
58 VERIFY( std::get<0>(t3b0).v );
59
60 tuple<int, A, int> t3a1;
61 tuple<int, B, int> t3b1 = t3a1;
62 VERIFY( std::get<1>(t3b1).v );
63
64 tuple<int, int, A> t3a2;
65 tuple<int, int, B> t3b2 = t3a2;
66 VERIFY( std::get<2>(t3b2).v );
67
68 // template<class... UTypes>
69 // constexpr explicit(false) tuple(pair<UTypes...>&);
70
71 pair<A, int> p2a0;
72 tuple<B, int> p2b0 = p2a0;
73 VERIFY( std::get<0>(p2b0).v );
74
75 pair<int, A> p2a1;
76 tuple<int, B> p2b1 = p2a1;
77 VERIFY( std::get<1>(p2b1).v );
78
79 return true;
80 }
81
82 namespace alloc
83 {
84 struct B01
85 {
86 bool v;
87 B01(A&);
88 constexpr B01(allocator_arg_t, allocator<int>, A&) : v(true) { }
89 };
90
91 constexpr bool
92 test01()
93 {
94 using B = B01;
95
96 // template<class Alloc, class... UTypes>
97 // constexpr explicit(false)
98 // tuple(allocator_arg_t, const Alloc& a, tuple<UTypes...>&);
99
100 tuple<A> t1a;
101 tuple<B> t1b = {allocator_arg, allocator<int>{}, t1a};
102 VERIFY( std::get<0>(t1b).v );
103
104 tuple<A, int> t2a0;
105 tuple<B, int> t2b0 = {allocator_arg, allocator<int>{}, t2a0};
106 VERIFY( std::get<0>(t2b0).v );
107
108 tuple<int, A> t2a1;
109 tuple<int, B> t2b1 = {allocator_arg, allocator<int>{}, t2a1};
110 VERIFY( std::get<1>(t2b1).v );
111
112 tuple<A, int, int> t3a0;
113 tuple<B, int, int> t3b0 = {allocator_arg, allocator<int>{}, t3a0};
114 VERIFY( std::get<0>(t3b0).v );
115
116 tuple<int, A, int> t3a1;
117 tuple<int, B, int> t3b1 = {allocator_arg, allocator<int>{}, t3a1};
118 VERIFY( std::get<1>(t3b1).v );
119
120 tuple<int, int, A> t3a2;
121 tuple<int, int, B> t3b2 = {allocator_arg, allocator<int>{}, t3a2};
122 VERIFY( std::get<2>(t3b2).v );
123
124 // template<class Alloc, class U1, class U2>
125 // constexpr explicit(false)
126 // tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&);
127
128 pair<A, int> p2a0;
129 tuple<B, int> p2b0 = {allocator_arg, allocator<int>{}, p2a0};
130 VERIFY( std::get<0>(p2b0).v );
131
132 pair<int, A> p2a1;
133 tuple<int, B> p2b1 = {allocator_arg, allocator<int>{}, p2a1};
134 VERIFY( std::get<1>(p2b1).v );
135
136 return true;
137 }
138 }
139
140 constexpr bool
141 test02()
142 {
143 struct B { bool v; explicit constexpr B(A&) : v(true) { } };
144
145 // template<class... UTypes>
146 // constexpr explicit(true) tuple(tuple<UTypes...>&);
147
148 static_assert(!std::is_convertible_v<tuple<A>&, tuple<B>>);
149
150 tuple<A> t1a;
151 tuple<B> t1b(t1a);
152 VERIFY( std::get<0>(t1b).v );
153
154 static_assert(!std::is_convertible_v<tuple<A, int>&, tuple<B, int>>);
155 static_assert(!std::is_convertible_v<tuple<int, A>&, tuple<int, B>>);
156
157 tuple<A, int> t2a0;
158 tuple<B, int> t2b0(t2a0);
159 VERIFY( std::get<0>(t2b0).v );
160
161 tuple<int, A> t2a1;
162 tuple<int, B> t2b1(t2a1);
163 VERIFY( std::get<1>(t2b1).v );
164
165 static_assert(!std::is_convertible_v<tuple<A, int, int>&, tuple<B, int, int>>);
166 static_assert(!std::is_convertible_v<tuple<int, A, int>&, tuple<int, B, int>>);
167 static_assert(!std::is_convertible_v<tuple<int, int, A>&, tuple<int, int, B>>);
168
169 tuple<A, int, int> t3a0;
170 tuple<B, int, int> t3b0(t3a0);
171 VERIFY( std::get<0>(t3b0).v );
172
173 tuple<int, A, int> t3a1;
174 tuple<int, B, int> t3b1(t3a1);
175 VERIFY( std::get<1>(t3b1).v );
176
177 tuple<int, int, A> t3a2;
178 tuple<int, int, B> t3b2(t3a2);
179 VERIFY( std::get<2>(t3b2).v );
180
181 // template<class... UTypes>
182 // constexpr explicit(true) tuple(pair<UTypes...>&);
183
184 static_assert(!std::is_convertible_v<pair<A, int>&, tuple<B, int>>);
185 static_assert(!std::is_convertible_v<pair<int, A>&, tuple<int, B>>);
186
187 pair<A, int> p2a0;
188 tuple<B, int> p2b0(p2a0);
189 VERIFY( std::get<0>(p2b0).v );
190
191 pair<int, A> p2a1;
192 tuple<int, B> p2b1(p2a1);
193 VERIFY( std::get<1>(p2b1).v );
194
195 return true;
196 }
197
198 namespace alloc
199 {
200 struct B02
201 {
202 bool v;
203 explicit B02(A&);
204 explicit constexpr B02(allocator_arg_t, allocator<int>, A&) : v(true) { }
205 };
206
207 constexpr bool
208 test02()
209 {
210 using B = B02;
211
212 // template<class Alloc, class... UTypes>
213 // constexpr explicit(true)
214 // tuple(allocator_arg_t, const Alloc& a, tuple<UTypes...>&);
215
216 tuple<A> t1a;
217 tuple<B> t1b(allocator_arg, allocator<int>{}, t1a);
218 VERIFY( std::get<0>(t1b).v );
219
220 tuple<A, int> t2a0;
221 tuple<B, int> t2b0(allocator_arg, allocator<int>{}, t2a0);
222 VERIFY( std::get<0>(t2b0).v );
223
224 tuple<int, A> t2a1;
225 tuple<int, B> t2b1(allocator_arg, allocator<int>{}, t2a1);
226 VERIFY( std::get<1>(t2b1).v );
227
228 tuple<A, int, int> t3a0;
229 tuple<B, int, int> t3b0(allocator_arg, allocator<int>{}, t3a0);
230 VERIFY( std::get<0>(t3b0).v );
231
232 tuple<int, A, int> t3a1;
233 tuple<int, B, int> t3b1(allocator_arg, allocator<int>{}, t3a1);
234 VERIFY( std::get<1>(t3b1).v );
235
236 tuple<int, int, A> t3a2;
237 tuple<int, int, B> t3b2(allocator_arg, allocator<int>{}, t3a2);
238 VERIFY( std::get<2>(t3b2).v );
239
240 // template<class Alloc, class U1, class U2>
241 // constexpr explicit(true)
242 // tuple(allocator_arg_t, const Alloc& a, pair<U1, U2>&);
243
244 pair<A, int> p2a0;
245 tuple<B, int> p2b0(allocator_arg, allocator<int>{}, p2a0);
246 VERIFY( std::get<0>(p2b0).v );
247
248 pair<int, A> p2a1;
249 tuple<int, B> p2b1(allocator_arg, allocator<int>{}, p2a1);
250 VERIFY( std::get<1>(p2b1).v );
251
252 return true;
253 }
254 } // namespace alloc
255
256 constexpr bool
257 test03()
258 {
259 struct B { bool v; constexpr B(const A&&) : v(true) { } };
260
261 // template<class... UTypes>
262 // constexpr explicit(false) tuple(const tuple<UTypes...>&&);
263
264 const tuple<A> t1a;
265 tuple<B> t1b = std::move(t1a);
266 VERIFY( std::get<0>(t1b).v );
267
268 const tuple<A, int> t2a0;
269 tuple<B, int> t2b0 = std::move(t2a0);
270 VERIFY( std::get<0>(t2b0).v );
271
272 const tuple<int, A> t2a1;
273 tuple<int, B> t2b1 = std::move(t2a1);
274 VERIFY( std::get<1>(t2b1).v );
275
276 const tuple<A, int, int> t3a0;
277 tuple<B, int, int> t3b0 = std::move(t3a0);
278 VERIFY( std::get<0>(t3b0).v );
279
280 const tuple<int, A, int> t3a1;
281 tuple<int, B, int> t3b1 = std::move(t3a1);
282 VERIFY( std::get<1>(t3b1).v );
283
284 const tuple<int, int, A> t3a2;
285 tuple<int, int, B> t3b2 = std::move(t3a2);
286 VERIFY( std::get<2>(t3b2).v );
287
288 // template<class... UTypes>
289 // constexpr explicit(false) tuple(const pair<UTypes...>&&);
290
291 const pair<A, int> p2a0;
292 tuple<B, int> p2b0 = std::move(p2a0);
293 VERIFY( std::get<0>(p2b0).v );
294
295 const pair<int, A> p2a1;
296 tuple<int, B> p2b1 = std::move(p2a1);
297 VERIFY( std::get<1>(p2b1).v );
298
299 return true;
300 }
301
302 namespace alloc
303 {
304 struct B03
305 {
306 bool v;
307 B03(const A&&);
308 constexpr B03(allocator_arg_t, allocator<int>, const A&&) : v(true) { }
309 };
310
311 constexpr bool
312 test03()
313 {
314 using B = B03;
315
316 // template<class Alloc, class... UTypes>
317 // constexpr explicit(false)
318 // tuple(allocator_arg_t, const Alloc& a, const tuple<UTypes...>&&);
319
320 const tuple<A> t1a;
321 tuple<B> t1b = {allocator_arg, allocator<int>{}, std::move(t1a)};
322 VERIFY( std::get<0>(t1b).v );
323
324 const tuple<A, int> t2a0;
325 tuple<B, int> t2b0 = {allocator_arg, allocator<int>{}, std::move(t2a0)};
326 VERIFY( std::get<0>(t2b0).v );
327
328 const tuple<int, A> t2a1;
329 tuple<int, B> t2b1 = {allocator_arg, allocator<int>{}, std::move(t2a1)};
330 VERIFY( std::get<1>(t2b1).v );
331
332 const tuple<A, int, int> t3a0;
333 tuple<B, int, int> t3b0 = {allocator_arg, allocator<int>{}, std::move(t3a0)};
334 VERIFY( std::get<0>(t3b0).v );
335
336 const tuple<int, A, int> t3a1;
337 tuple<int, B, int> t3b1 = {allocator_arg, allocator<int>{}, std::move(t3a1)};
338 VERIFY( std::get<1>(t3b1).v );
339
340 const tuple<int, int, A> t3a2;
341 tuple<int, int, B> t3b2 = {allocator_arg, allocator<int>{}, std::move(t3a2)};
342 VERIFY( std::get<2>(t3b2).v );
343
344 // template<class Alloc, class U1, class U2>
345 // constexpr explicit(false)
346 // tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&&);
347
348 const pair<A, int> p2a0;
349 tuple<B, int> p2b0 = {allocator_arg, allocator<int>{}, std::move(p2a0)};
350 VERIFY( std::get<0>(p2b0).v );
351
352 const pair<int, A> p2a1;
353 tuple<int, B> p2b1 = {allocator_arg, allocator<int>{}, std::move(p2a1)};
354 VERIFY( std::get<1>(p2b1).v );
355
356 return true;
357 }
358 };
359
360 constexpr bool
361 test04()
362 {
363 struct B { bool v; explicit constexpr B(const A&&) : v(true) { } };
364
365 // template<class... UTypes>
366 // constexpr explicit(true) tuple(const tuple<UTypes...>&&);
367
368 static_assert(!std::is_convertible_v<tuple<A>&, tuple<B>>);
369
370 const tuple<A> t1a;
371 tuple<B> t1b(std::move(t1a));
372 VERIFY( std::get<0>(t1b).v );
373
374 static_assert(!std::is_convertible_v<tuple<A, int>&, tuple<B, int>>);
375 static_assert(!std::is_convertible_v<tuple<int, A>&, tuple<int, B>>);
376
377 const tuple<A, int> t2a0;
378 tuple<B, int> t2b0(std::move(t2a0));
379 VERIFY( std::get<0>(t2b0).v );
380
381 const tuple<int, A> t2a1;
382 tuple<int, B> t2b1(std::move(t2a1));
383 VERIFY( std::get<1>(t2b1).v );
384
385 static_assert(!std::is_convertible_v<tuple<A, int, int>&, tuple<B, int, int>>);
386 static_assert(!std::is_convertible_v<tuple<int, A, int>&, tuple<int, B, int>>);
387 static_assert(!std::is_convertible_v<tuple<int, int, A>&, tuple<int, int, B>>);
388
389 const tuple<A, int, int> t3a0;
390 tuple<B, int, int> t3b0(std::move(t3a0));
391 VERIFY( std::get<0>(t3b0).v );
392
393 const tuple<int, A, int> t3a1;
394 tuple<int, B, int> t3b1(std::move(t3a1));
395 VERIFY( std::get<1>(t3b1).v );
396
397 const tuple<int, int, A> t3a2;
398 tuple<int, int, B> t3b2(std::move(t3a2));
399 VERIFY( std::get<2>(t3b2).v );
400
401 // template<class... UTypes>
402 // constexpr explicit(true) tuple(const pair<UTypes...>&&);
403
404 static_assert(!std::is_convertible_v<pair<A, int>&, tuple<B, int>>);
405 static_assert(!std::is_convertible_v<pair<int, A>&, tuple<int, B>>);
406
407 const pair<A, int> p2a0;
408 tuple<B, int> p2b0(std::move(p2a0));
409 VERIFY( std::get<0>(p2b0).v );
410
411 const pair<int, A> p2a1;
412 tuple<int, B> p2b1(std::move(p2a1));
413 VERIFY( std::get<1>(p2b1).v );
414
415 return true;
416 }
417
418 namespace alloc
419 {
420 struct B04
421 {
422 bool v;
423 explicit B04(const A&&);
424 explicit constexpr B04(allocator_arg_t, allocator<int>, const A&&) : v(true) { }
425 };
426
427 constexpr bool
428 test04()
429 {
430 using B = B04;
431
432 // template<class Alloc, class... UTypes>
433 // constexpr explicit(true)
434 // tuple(allocator_arg_t, const Alloc& a, const tuple<UTypes...>&&);
435
436 const tuple<A> t1a;
437 tuple<B> t1b(allocator_arg, allocator<int>{}, std::move(t1a));
438 VERIFY( std::get<0>(t1b).v );
439
440 const tuple<A, int> t2a0;
441 tuple<B, int> t2b0(allocator_arg, allocator<int>{}, std::move(t2a0));
442 VERIFY( std::get<0>(t2b0).v );
443
444 const tuple<int, A> t2a1;
445 tuple<int, B> t2b1(allocator_arg, allocator<int>{}, std::move(t2a1));
446 VERIFY( std::get<1>(t2b1).v );
447
448 const tuple<A, int, int> t3a0;
449 tuple<B, int, int> t3b0(allocator_arg, allocator<int>{}, std::move(t3a0));
450 VERIFY( std::get<0>(t3b0).v );
451
452 const tuple<int, A, int> t3a1;
453 tuple<int, B, int> t3b1(allocator_arg, allocator<int>{}, std::move(t3a1));
454 VERIFY( std::get<1>(t3b1).v );
455
456 const tuple<int, int, A> t3a2;
457 tuple<int, int, B> t3b2(allocator_arg, allocator<int>{}, std::move(t3a2));
458 VERIFY( std::get<2>(t3b2).v );
459
460 // template<class Alloc, class U1, class U2>
461 // constexpr explicit(true)
462 // tuple(allocator_arg_t, const Alloc& a, const pair<U1, U2>&&);
463
464 tuple<B, int> p2b0(allocator_arg, allocator<int>{}, std::move(t2a0));
465 VERIFY( std::get<0>(p2b0).v );
466
467 tuple<int, B> p2b1(allocator_arg, allocator<int>{}, std::move(t2a1));
468 VERIFY( std::get<1>(p2b1).v );
469
470 return true;
471 }
472 };
473
474 constexpr bool
475 test05()
476 {
477 struct B
478 {
479 mutable bool v;
480 constexpr const B& operator=(const A&) const { v = true; return *this; }
481 };
482
483 // template<class... UTypes>
484 // constexpr const tuple& operator=(const tuple<UTypes...>&) const;
485
486 const tuple<A> t1a;
487 const tuple<B> t1b;
488 t1b = t1a;
489 VERIFY( std::get<0>(t1b).v );
490
491 const tuple<A, A> t2a;
492 const tuple<B, B> t2b;
493 t2b = t2a;
494 VERIFY( std::get<0>(t2b).v );
495 VERIFY( std::get<1>(t2b).v );
496
497 const tuple<A, A, A> t3a;
498 const tuple<B, B, B> t3b;
499 t3b = t3a;
500 VERIFY( std::get<0>(t3b).v );
501 VERIFY( std::get<1>(t3b).v );
502 VERIFY( std::get<2>(t3b).v );
503
504 // template<class U1, class U2>
505 // constexpr const tuple& operator=(const pair<U1, U2>&) const;
506
507 const pair<A, A> p2a;
508 const tuple<B, B> p2b;
509 p2b = p2a;
510
511 return true;
512 }
513
514 constexpr bool
515 test06()
516 {
517 struct B
518 {
519 mutable bool v;
520 constexpr const B& operator=(A&&) const { v = true; return *this; }
521 };
522
523 // template<class... UTypes>
524 // constexpr const tuple& operator=(tuple<UTypes...>&&) const;
525
526 tuple<A> t1a;
527 const tuple<B> t1b;
528 t1b = std::move(t1a);
529 VERIFY( std::get<0>(t1b).v );
530
531 tuple<A, A> t2a;
532 const tuple<B, B> t2b;
533 t2b = std::move(t2a);
534 VERIFY( std::get<0>(t2b).v );
535 VERIFY( std::get<1>(t2b).v );
536
537 tuple<A, A, A> t3a;
538 const tuple<B, B, B> t3b;
539 t3b = std::move(t3a);
540 VERIFY( std::get<0>(t3b).v );
541 VERIFY( std::get<1>(t3b).v );
542 VERIFY( std::get<2>(t3b).v );
543
544 // template<class U1, class U2>
545 // constexpr const tuple& operator=(pair<U1, U2>&&) const;
546
547 pair<A, A> p2a;
548 const tuple<B, B> p2b;
549 p2b = std::move(p2a);
550
551 return true;
552 }
553
554 constexpr bool
555 test07()
556 {
557 struct B
558 {
559 mutable bool v;
560 constexpr const B& operator=(const B&) const { v = true; return *this; }
561 };
562
563 // constexpr const tuple& operator=(const tuple&) const;
564
565 const tuple<B> t1a;
566 const tuple<B> t1b;
567 t1b = t1a;
568 VERIFY( std::get<0>(t1b).v );
569
570 const tuple<B, B> t2a;
571 const tuple<B, B> t2b;
572 t2b = t2a;
573 VERIFY( std::get<0>(t2b).v );
574 VERIFY( std::get<1>(t2b).v );
575
576 const tuple<B, B, B> t3a;
577 const tuple<B, B, B> t3b;
578 t3b = t3a;
579 VERIFY( std::get<0>(t3b).v );
580 VERIFY( std::get<1>(t3b).v );
581 VERIFY( std::get<2>(t3b).v );
582
583 return true;
584 }
585
586 constexpr bool
587 test08()
588 {
589 struct B
590 {
591 mutable bool v;
592 constexpr const B& operator=(B&&) const { v = true; return *this; }
593 };
594
595 // constexpr const tuple& operator=(tuple&&) const;
596
597 tuple<B> t1a;
598 const tuple<B> t1b;
599 t1b = std::move(t1a);
600 VERIFY( std::get<0>(t1b).v );
601
602 tuple<B, B> t2a;
603 const tuple<B, B> t2b;
604 t2b = std::move(t2a);
605 VERIFY( std::get<0>(t2b).v );
606 VERIFY( std::get<1>(t2b).v );
607
608 tuple<B, B, B> t3a;
609 const tuple<B, B, B> t3b;
610 t3b = std::move(t3a);
611 VERIFY( std::get<0>(t3b).v );
612 VERIFY( std::get<1>(t3b).v );
613 VERIFY( std::get<2>(t3b).v );
614
615 return true;
616 }
617
618 struct S
619 {
620 mutable int v = 0;
621 friend constexpr void swap(S&& x, S&& y) = delete;
622 friend constexpr void swap(const S& x, const S& y) { ++x.v; ++y.v; }
623 };
624
625 constexpr bool
626 test09()
627 {
628 const tuple<S> t1, u1;
629 std::swap(t1, u1);
630 VERIFY( std::get<0>(t1).v == 1 );
631 VERIFY( std::get<0>(u1).v == 1 );
632
633 const tuple<S, S> t2, u2;
634 std::swap(t2, u2);
635 VERIFY( std::get<0>(t2).v == 1 );
636 VERIFY( std::get<0>(u2).v == 1 );
637 VERIFY( std::get<1>(t2).v == 1 );
638 VERIFY( std::get<1>(u2).v == 1 );
639
640 const tuple<S, S, S> t3, u3;
641 std::swap(t3, u3);
642 VERIFY( std::get<0>(t3).v == 1 );
643 VERIFY( std::get<0>(u3).v == 1 );
644 VERIFY( std::get<1>(t3).v == 1 );
645 VERIFY( std::get<1>(u3).v == 1 );
646 VERIFY( std::get<2>(t3).v == 1 );
647 VERIFY( std::get<2>(u3).v == 1 );
648
649 static_assert(!std::is_swappable_v<const tuple<A>&>);
650
651 return true;
652 }
653
654 int
655 main()
656 {
657 static_assert(test01());
658 static_assert(alloc::test01());
659 static_assert(test02());
660 static_assert(alloc::test02());
661 static_assert(test03());
662 static_assert(alloc::test03());
663 static_assert(test04());
664 static_assert(alloc::test04());
665 // FIXME: G++ doesn't support reading mutable members during constexpr (PR c++/92505).
666 test05();
667 test06();
668 test07();
669 test08();
670 test09();
671 }