Debian Codegraph @

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