Debian Codegraph @

gcc/testsuite/g++.dg/cpp26/decomp4.C

raw
1 // P0963R3 - Structured binding declaration as a condition
2 // { dg-do run { target c++11 } }
3 // { dg-options "" }
4
5 namespace std {
6 template<typename T> struct tuple_size;
7 template<int, typename> struct tuple_element;
8 }
9
10 struct S {
11 int a, b;
12 operator int () const noexcept { return a * 2; }
13 };
14
15 struct T {
16 int a, b, c;
17 static int d;
18 operator long long () const noexcept { d = 42; return a * 4; }
19 template <int I> int &get () { if (d != 42) __builtin_abort (); return I ? a : b; }
20 };
21 int T::d = 0;
22
23 template<> struct std::tuple_size<T> { static const int value = 2; };
24 template<int I> struct std::tuple_element<I,T> { using type = int; };
25
26 void
27 foo (T t)
28 {
29 switch (auto [ i, j ] = S { 53, 62 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
30 {
31 case 2 * 53:
32 if (i != 53 || j != 62)
33 __builtin_abort ();
34 break;
35 default:
36 __builtin_abort ();
37 }
38 T::d = 0;
39 switch (int m = 78; auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
40 { // { dg-warning "init-statement in selection statements only available with" "" { target c++14_down } .-1 }
41 case 4 * 42LL:
42 if (i != 43 || j != 42 || T::d != 42 || m != 78)
43 __builtin_abort ();
44 break;
45 default:
46 break;
47 }
48 switch (auto m = 15; auto [ i, j ] = S { -1, 1 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
49 { // { dg-warning "init-statement in selection statements only available with" "" { target c++14_down } .-1 }
50 case 2 * -1:
51 if (i != -1 || j != 1 || m != 15)
52 __builtin_abort ();
53 break;
54 default:
55 __builtin_abort ();
56 }
57 t.a = -42;
58 T::d = 0;
59 switch (auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
60 {
61 case 4LL * -42:
62 if (i != 43 || j != -42 || T::d != 42)
63 __builtin_abort ();
64 break;
65 default:
66 __builtin_abort ();
67 }
68 }
69
70 int
71 main ()
72 {
73 foo ({ 42, 43, 0 });
74 }