Debian Codegraph @

gcc/testsuite/g++.dg/cpp26/decomp5.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 explicit operator bool () const noexcept { return a == b; }
13 };
14
15 struct T {
16 int a, b, c;
17 static int d;
18 explicit operator bool () const noexcept { d = 42; return a == b; }
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 template <int N>
27 void
28 foo (T t)
29 {
30 if (auto [ i, j ] = S { 1, 1 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
31 {
32 if (i != 1 || j != 1)
33 __builtin_abort ();
34 }
35 else
36 {
37 ++i;
38 ++j;
39 __builtin_abort ();
40 }
41 T::d = 0;
42 if (int m = 78; auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
43 { // { dg-warning "init-statement in selection statements only available with" "" { target c++14_down } .-1 }
44 if (i != 42 || j != 42 || T::d != 42 || m != 78)
45 __builtin_abort ();
46 }
47 else
48 {
49 ++i;
50 ++j;
51 ++m;
52 __builtin_abort ();
53 }
54 if (auto m = 15; auto [ i, j ] = S { -1, 1 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
55 { // { dg-warning "init-statement in selection statements only available with" "" { target c++14_down } .-1 }
56 ++i;
57 ++j;
58 ++m;
59 __builtin_abort ();
60 }
61 else
62 {
63 if (i != -1 || j != 1 || m != 15)
64 __builtin_abort ();
65 }
66 t.a = -42;
67 T::d = 0;
68 if (auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
69 {
70 ++i;
71 ++j;
72 __builtin_abort ();
73 }
74 else
75 {
76 if (i != 42 || j != -42 || T::d != 42)
77 __builtin_abort ();
78 }
79 }
80
81 template <int N>
82 void
83 bar (T t)
84 {
85 int cnt = 0;
86 while (auto [ i, j ] = S { 7, 7 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
87 {
88 if (i != 7 || j != 7)
89 __builtin_abort ();
90 if (++cnt == 5)
91 break;
92 }
93 if (cnt != 5)
94 __builtin_abort ();
95 T::d = 0;
96 while (auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
97 {
98 if (i != 31 || j != 31 || T::d != 42)
99 __builtin_abort ();
100 T::d = 0;
101 if (++cnt == 10)
102 break;
103 }
104 if (cnt != 10)
105 __builtin_abort ();
106 while (auto [ i, j ] = S { 7, -7 }) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
107 {
108 ++i;
109 ++j;
110 __builtin_abort ();
111 }
112 t.a = -31;
113 T::d = 0;
114 while (auto & [ i, j ] = t) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
115 {
116 ++i;
117 ++j;
118 __builtin_abort ();
119 }
120 }
121
122 template <int N>
123 void
124 baz (T t)
125 {
126 int cntc = 0;
127 for (int cnt = 0; auto [ i, j ] = S { 12, 12 }; ++cnt) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
128 {
129 if (i != 12 || j != 12)
130 __builtin_abort ();
131 cntc = cnt;
132 if (cnt == 5)
133 break;
134 }
135 if (cntc != 5)
136 __builtin_abort ();
137 T::d = 0;
138 for (int cnt = 5; auto & [ i, j ] = t; ++cnt) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
139 {
140 if (i != -15 || j != -15 || T::d != 42)
141 __builtin_abort ();
142 T::d = 0;
143 cntc = cnt;
144 if (cnt == 10)
145 break;
146 }
147 if (cntc != 10)
148 __builtin_abort ();
149 for (int cnt = 0; auto [ i, j ] = S { -27, 27 }; ++cnt) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
150 {
151 ++i;
152 ++j;
153 __builtin_abort ();
154 }
155 t.a = 15;
156 T::d = 0;
157 for (int cnt = 0; auto & [ i, j ] = t; ++cnt) // { dg-warning "structured bindings in conditions only available with" "" { target c++23_down } }
158 {
159 ++i;
160 ++j;
161 __builtin_abort ();
162 }
163 }
164
165 int
166 main ()
167 {
168 foo<0> ({ 42, 42, 0 });
169 bar<0> ({ 31, 31, 7 });
170 baz<0> ({ -15, -15, 6 });
171 }