Debian Codegraph @

gcc/testsuite/g++.dg/cpp23/range-for2.C

raw
1 // P2718R0 - Wording for P2644R1 Fix for Range-based for Loop
2 // { dg-do run { target c++11 } }
3
4 #ifndef RANGE_FOR_EXT_TEMPS
5 #define RANGE_FOR_EXT_TEMPS (__cpp_range_based_for >= 202211L)
6 #endif
7
8 extern "C" void abort ();
9
10 int a[4];
11
12 template <typename T>
13 int *
14 begin (const T &)
15 {
16 return &a[0];
17 }
18
19 template <typename T>
20 int *
21 end (const T &)
22 {
23 return &a[4];
24 }
25
26 struct S
27 {
28 S () { ++s; }
29 S (const S &) { ++s; }
30 ~S () { --s; }
31 static int s;
32 };
33
34 int S::s;
35
36 template <typename T>
37 struct U
38 {
39 T t;
40 U () { ++u; }
41 U (const U &) { ++u; }
42 ~U () { --u; }
43 const int *begin () const { return ::begin (t); }
44 const int *end () const { return ::end (t); }
45 U &foo () { return *this; }
46 U bar () { return U (); }
47 static int u;
48 };
49
50 template <typename T>
51 int U<T>::u;
52
53 template <typename T>
54 U<T>
55 foo ()
56 {
57 return U<T> {};
58 }
59
60 template <typename T>
61 T
62 fred (const T &, const T & = T{}, const T & = T{})
63 {
64 return T {};
65 }
66
67 void
68 bar ()
69 {
70 int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
71 if (S::s != 0)
72 abort ();
73 for (auto x : S (), a)
74 {
75 if (S::s != RANGE_FOR_EXT_TEMPS)
76 abort ();
77 }
78 if (S::s != 0)
79 abort ();
80 for (auto x : (void) S (), a)
81 {
82 if (S::s != RANGE_FOR_EXT_TEMPS)
83 abort ();
84 }
85 if (S::s != 0)
86 abort ();
87 for (auto x : static_cast<void> (S ()), a)
88 {
89 if (S::s != RANGE_FOR_EXT_TEMPS)
90 abort ();
91 }
92 if (S::s != 0)
93 abort ();
94 for (auto x : foo <S> ().foo ().bar ().foo ().bar ().foo ().bar ())
95 {
96 if (S::s != 1 + 3 * RANGE_FOR_EXT_TEMPS)
97 abort ();
98 if (U<S>::u != S::s)
99 abort ();
100 }
101 if (S::s != 0 || U<S>::u != 0)
102 abort ();
103 for (auto x : fred (S {}))
104 {
105 if (S::s != 1 + 3 * RANGE_FOR_EXT_TEMPS)
106 abort ();
107 }
108 if (S::s != 0)
109 abort ();
110 for (auto x : fred (fred (S {}, S {})))
111 {
112 if (S::s != 1 + 6 * RANGE_FOR_EXT_TEMPS)
113 abort ();
114 }
115 if (S::s != 0)
116 abort ();
117 }
118
119 template <int N>
120 void
121 baz ()
122 {
123 int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
124 if (S::s != 0)
125 abort ();
126 for (auto x : S (), a)
127 {
128 if (S::s != RANGE_FOR_EXT_TEMPS)
129 abort ();
130 }
131 if (S::s != 0)
132 abort ();
133 for (auto x : (void) S (), a)
134 {
135 if (S::s != RANGE_FOR_EXT_TEMPS)
136 abort ();
137 }
138 if (S::s != 0)
139 abort ();
140 for (auto x : static_cast<void> (S ()), a)
141 {
142 if (S::s != RANGE_FOR_EXT_TEMPS)
143 abort ();
144 }
145 if (S::s != 0)
146 abort ();
147 for (auto x : foo <S> ().foo ().bar ().foo ().bar ().foo ().bar ())
148 {
149 if (S::s != 1 + 3 * RANGE_FOR_EXT_TEMPS)
150 abort ();
151 if (U<S>::u != S::s)
152 abort ();
153 }
154 if (S::s != 0 || U<S>::u != 0)
155 abort ();
156 for (auto x : fred (S {}))
157 {
158 if (S::s != 1 + 3 * RANGE_FOR_EXT_TEMPS)
159 abort ();
160 }
161 if (S::s != 0)
162 abort ();
163 for (auto x : fred (fred (S {}, S {})))
164 {
165 if (S::s != 1 + 6 * RANGE_FOR_EXT_TEMPS)
166 abort ();
167 }
168 if (S::s != 0)
169 abort ();
170 }
171
172 template <typename S>
173 void
174 qux ()
175 {
176 int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
177 if (S::s != 0)
178 abort ();
179 for (auto x : S (), a)
180 {
181 if (S::s != RANGE_FOR_EXT_TEMPS)
182 abort ();
183 }
184 if (S::s != 0)
185 abort ();
186 for (auto x : (void) S (), a)
187 {
188 if (S::s != RANGE_FOR_EXT_TEMPS)
189 abort ();
190 }
191 if (S::s != 0)
192 abort ();
193 for (auto x : static_cast<void> (S ()), a)
194 {
195 if (S::s != RANGE_FOR_EXT_TEMPS)
196 abort ();
197 }
198 if (S::s != 0)
199 abort ();
200 for (auto x : foo <S> ().foo ().bar ().foo ().bar ().foo ().bar ())
201 {
202 if (S::s != 1 + 3 * RANGE_FOR_EXT_TEMPS)
203 abort ();
204 if (U<S>::u != S::s)
205 abort ();
206 }
207 if (S::s != 0 || U<S>::u != 0)
208 abort ();
209 for (auto x : fred (S {}))
210 {
211 if (S::s != 1 + 3 * RANGE_FOR_EXT_TEMPS)
212 abort ();
213 }
214 if (S::s != 0)
215 abort ();
216 for (auto x : fred (fred (S {}, S {})))
217 {
218 if (S::s != 1 + 6 * RANGE_FOR_EXT_TEMPS)
219 abort ();
220 }
221 if (S::s != 0)
222 abort ();
223 }
224
225 int
226 main ()
227 {
228 bar ();
229 baz <0> ();
230 qux <S> ();
231 }