1 /* Test C2x constexpr. Valid code, compilation tests. */
2 /* { dg-do compile } */
3 /* { dg-options "-std=c2x -pedantic-errors" } */
8 static_assert (v1 == 1);
9 extern typeof (v1) *pci;
10 extern const int *pci;
11 extern typeof (&(constexpr int) {}) pci;
12 /* Redeclaring a constexpr object is OK (although it can't be declared before
13 the definition without undefined behavior). */
15 static_assert (v1 == 1);
16 unsigned int constexpr v2 = 2;
17 static_assert (v2 == 2);
18 extern typeof (v2) *pcui;
19 extern const unsigned int *pcui;
20 static constexpr char v3 = 3;
21 static_assert (v3 == 3);
22 extern typeof (v3) *pcc;
23 extern const char *pcc;
24 constexpr void *v4 = 0;
25 extern typeof (v4) *pcpv;
26 extern void *const *pcpv;
27 constexpr int *v5 = nullptr;
28 extern typeof (v5) *pcpi;
29 extern int *const *pcpi;
30 constexpr double v6 = 3.5;
31 extern typeof (v6) *pcd;
32 extern const double *pcd;
33 auto constexpr v7 = 1.0;
34 extern typeof (v7) *pcd;
35 constexpr auto v8 = 1.5f;
36 extern typeof (v8) *pcf;
37 extern const float *pcf;
38 constexpr static long v9 = 2ULL;
39 static_assert (v9 == 2);
40 extern typeof (v9) *pcl;
41 extern const long *pcl;
42 const short *v10 = &(constexpr short) { 10 };
43 /* Qualifiers that aren't permitted on a constexpr object itself are OK in a
45 constexpr volatile int *v11 = nullptr;
46 extern typeof (v11) *pcpvi;
47 extern volatile int *const *pcpvi;
48 constexpr _Atomic int *v12 = nullptr;
49 extern typeof (v12) *pcpai;
50 extern _Atomic int *const *pcpai;
51 constexpr int *restrict *v13 = nullptr;
52 extern typeof (v13) cprpi;
53 extern int *restrict *const cprpi;
55 constexpr restrict P *v14 = nullptr;
56 extern typeof (v14) cprpi;
57 struct s15 { volatile int a; _Atomic int b; int *restrict p; };
58 constexpr struct s15 *v16 = nullptr;
59 constexpr char v17[3] = { 1, 2, 3 };
60 struct s18 { int a; int *b; double c; };
61 constexpr struct s18 v19 = { 12345ULL, 0, 19.0L };
62 static_assert (v19.a == 12345);
63 union u20 { int a; float b; };
64 constexpr union u20 v21 = { 1 };
65 static_assert (v21.a == 1);
66 constexpr union u20 v22 = { .b = 23.0 };
67 constexpr float v23 = (float) (1.0f / 3.0f);
68 constexpr double v24 = (double) (1.0 / 3.0);
69 constexpr struct s18 v25 = { 0, 0, (double) (1.0 / 3.0) };
70 static_assert (v25.a == 0);
71 constexpr char v26[] = "abc\xfe";
72 constexpr unsigned char v27[] = u8"xyz\xff";
73 constexpr unsigned char v28[] = "\x12\x7f";
74 constexpr signed char v29[] = "\x34\x66";
75 constexpr double v30 = (int) (double) 3.0 - (long) (double) 2.0;
76 constexpr int v31 = 1 + 2 + (int) 3.0;
77 static_assert (v31 == 6);
78 constexpr typeof (nullptr) v32 = nullptr;
79 constexpr _Complex double v33 = __builtin_complex (1.0f, 3.0f / 2.0f);
80 constexpr float v34 = 1234.0L;
81 constexpr char v35 = 127ULL;
82 #if FLT_MIN_EXP == -125 && FLT_MANT_DIG == 24
83 constexpr float v36 = 0x1p-149;
84 constexpr float _Complex v37 = __builtin_complex (0x1p-149, 0x1p127);
85 constexpr float v38 = 0xffffffUL;
86 constexpr float v39 = -0xffffffL;
87 constexpr float v40 = 0xffffff0L;
88 constexpr float v41 = 1ULL << 63;
90 #if DBL_MIN_EXP == -1021 && DBL_MANT_DIG == 53
91 constexpr double v42 = 0x1p-1074L;
92 constexpr _Complex double v43 = __builtin_complex (0x1p1023L, 0x1p-1074L);
93 constexpr double v44 = 0x1fffffffffffffULL;
94 constexpr double v45 = -0x1fffffffffffffLL;
95 constexpr double v46 = 0x3ffffffffffffeULL;
96 constexpr double v47 = 1ULL << 63;
98 constexpr void *v48 = (void *) 0;
99 constexpr int *v49 = (void *) 0L;
100 constexpr long *v50 = 0LL;
101 constexpr int v51 = {};
102 static_assert (v51 == 0);
103 constexpr float v52 = {};
104 constexpr long double v53 = {};
105 constexpr int *v54 = {};
106 constexpr void *v55 = {};
107 constexpr typeof (nullptr) v56 = {};
108 struct s57 { int *p; };
109 union u58 { int *p; };
110 constexpr int *v59 = 0;
111 constexpr int *v60 = { 0 };
112 constexpr struct s57 v61 = { 0 };
113 constexpr struct s57 v62 = { { } }; /* { dg-warning "braces around scalar initializer" } */
114 constexpr struct s57 v63 = { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
115 constexpr union u58 v64 = { 0 };
116 constexpr union u58 v65 = { { } }; /* { dg-warning "braces around scalar initializer" } */
117 constexpr union u58 v66 = { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
118 struct s67 { int a; float b; void *c; int *d; typeof (nullptr) e; int f; int g[2]; };
119 struct s68 { struct s67 x; };
120 union u69 { int a; float b; void *c; int *d; struct s68 e; };
121 struct s70 { union u69 x; };
122 constexpr struct s67 v71 = { 1, 2.0, 0, 0, nullptr, 7, { 3, 4 } };
123 static_assert (v71.a == 1);
124 static_assert (v71.f == 7);
125 constexpr struct s67 v72 = v71;
126 static_assert (v72.a == 1);
127 static_assert (v72.f == 7);
128 extern const struct s67 v71;
129 constexpr auto v73 = v71;
130 static_assert (v73.a == 1);
131 static_assert (v73.f == 7);
133 constexpr struct s68 v75 = { v72 };
134 static_assert (v75.x.a == 1);
135 static_assert (v75.x.f == 7);
136 constexpr union u69 v76 = { };
137 static_assert (v76.a == 0);
138 constexpr union u69 v77 = { .e = v75 };
139 static_assert (v77.e.x.a == 1);
140 static_assert (v77.e.x.f == 7);
141 constexpr union u69 v78 = { .a = 1 };
142 static_assert (v78.a == 1);
143 constexpr union u69 v79 = { .e = { v72 } };
144 static_assert (v79.e.x.a == 1);
145 static_assert (v79.e.x.f == 7);
146 enum e80 { E80 = v79.e.x.f };
147 static_assert (E80 == 7);
148 constexpr struct s70 v81 = { v79 };
149 static_assert (v81.x.e.x.f == 7);
150 constexpr struct s68 v82 = { (constexpr struct s67) { 5, 6, 0, 0, nullptr, 9, { 1, 2 } } };
151 static_assert (v82.x.a == 5);
152 static_assert (v82.x.f == 9);
153 constexpr auto v83 = (constexpr int) { (constexpr int) { 0 } };
154 /* These are null pointers but not null pointer constants. */
155 constexpr typeof (nullptr) v84 = nullptr;
156 constexpr void *v85 = 0;
159 typeof (1 ? v85 : (int *) 0) v88;
161 typeof (1 ? (void *) 0 : (int *) 0) v89;
163 constexpr struct s68 v90 = { };
164 static_assert (v90.x.a == 0);
165 static_assert (v90.x.f == 0);
166 constexpr int v91 = { 123 };
167 static_assert (v91 == 123);
168 constexpr int v92 = { v91 };
169 static_assert (v92 == 123);
170 /* Verify that constexpr values can be used in various contexts requiring
171 (integer) constant expressions. */
172 struct s93 { int x : v79.e.x.f; };
173 constexpr int v94 = alignof (int);
174 alignas (v94) int v95;
175 constexpr int v97[100] = { [v82.x.f] = 7 };
177 constexpr _Complex double v99 = 1.0;
178 constexpr _Complex float v100 = 12345;
183 constexpr int fv0 = 3;
184 static_assert (fv0 == 3);
185 auto constexpr fv1 = 4;
186 static_assert (fv1 == 4);
187 register constexpr float fv2 = 1.0;
188 constexpr auto fv3 = 123;
189 static_assert (fv3 == 123);
190 constexpr register void *fv4 = (void *) 0;
191 const int *fv5 = &(constexpr int) { 234 };
192 const int *fv6 = &(constexpr static int) { 234 };
193 const int *fv7 = &(static constexpr int) { 234 };
194 typeof ((constexpr register int) { 234 }) *fv8;
195 typeof ((register constexpr int) { 234 }) *fv9;
196 int fv10 = (constexpr int) { 1 } + sizeof (struct fs *);
197 constexpr auto fv11 = (constexpr int) { (constexpr int) { 0 } };
198 static_assert (fv11 == 0);
199 constexpr char fv12[3] = { 1, 2, 3 };
200 (constexpr short [4]) { 9, 8, 7, -6 };
201 constexpr struct s18 fv13 = { 1234ULL, 0, 13.0f };
202 (constexpr struct s18) { 123, (void *) 0, 11 };
203 constexpr union u20 fv14 = { 2 };
204 (constexpr union u20) { 5 };
205 constexpr union u20 fv15 = { .b = 15.0 };
206 (constexpr union u20) { .b = 20 };
207 (constexpr float) { (float) (1.0f / 3.0f) };
208 (constexpr double) { (double) (1.0 / 3.0) };
209 (constexpr struct s18) { 0, 0, (double) (1.0 / 3.0) };
210 (constexpr char []) { "abc\xfe" };
211 (constexpr unsigned char []) { u8"xyz\xff" };
212 (constexpr unsigned char []) { "\x12\x7f" };
213 (constexpr signed char []) { "\x34\x66" };
214 (constexpr double) { (int) (double) 3.0 - (long) (double) 2.0 };
215 (constexpr int) { 1 + 2 + (int) 3.0 };
216 (constexpr typeof (nullptr)) { nullptr };
217 (constexpr _Complex double) { __builtin_complex (1.0f, 3.0f / 2.0f) };
218 (constexpr float) { 1234.0L };
219 (constexpr char) { 127ULL };
220 #if FLT_MIN_EXP == -125 && FLT_MANT_DIG == 24
221 (constexpr float) { 0x1p-149 };
222 (constexpr float _Complex) { __builtin_complex (0x1p-149, 0x1p127) };
223 (constexpr float) { 0xffffffUL };
224 (constexpr float) { -0xffffffL };
225 (constexpr float) { 0xffffff0L };
226 (constexpr float) { 1ULL << 63 };
228 #if DBL_MIN_EXP == -1021 && DBL_MANT_DIG == 53
229 (constexpr double) { 0x1p-1074L };
230 (constexpr _Complex double) { __builtin_complex (0x1p1023L, 0x1p-1074L) };
231 (constexpr double) { 0x1fffffffffffffULL };
232 (constexpr double) { -0x1fffffffffffffLL };
233 (constexpr double) { 0x3ffffffffffffeULL };
234 (constexpr double) { 1ULL << 63 };
236 (constexpr void *) { (void *) 0 };
237 (constexpr int *) { (void *) 0L };
238 (constexpr long *) { 0LL };
240 (constexpr float) {};
241 (constexpr long double) {};
242 (constexpr int *) {};
243 (constexpr void *) {};
244 (constexpr typeof (nullptr)) {};
245 (constexpr int *) { 0 };
246 (constexpr struct s57) { 0 };
247 (constexpr struct s57) { { } }; /* { dg-warning "braces around scalar initializer" } */
248 (constexpr struct s57) { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
249 (constexpr union u58) { 0 };
250 (constexpr union u58) { { } }; /* { dg-warning "braces around scalar initializer" } */
251 (constexpr union u58) { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
252 (constexpr _Complex double) { 1.0 };
253 (constexpr _Complex float) { 12345 };
254 /* It's not entirely clear if constexpr declarations are allowed in this
255 position in a for loop; presume they are, as implicitly auto just as if no
256 storage class specifiers were used. */
257 for (constexpr int fv16 = 1;;)
259 constexpr struct s67 fv17 = { 1, 2.0, 0, 0, nullptr, 7, { 3, 4 } };
260 static_assert (fv17.a == 1);
261 static_assert (fv17.f == 7);
262 constexpr struct s67 fv18 = fv17;
263 static_assert (fv18.a == 1);
264 static_assert (fv18.f == 7);
265 constexpr auto fv19 = fv17;
266 static_assert (fv19.a == 1);
267 static_assert (fv19.f == 7);
269 constexpr struct s68 fv21 = { fv18 };
270 static_assert (fv21.x.a == 1);
271 static_assert (fv21.x.f == 7);
272 constexpr union u69 fv22 = { };
273 static_assert (fv22.a == 0);
274 constexpr union u69 fv23 = { .e = fv21 };
275 static_assert (fv23.e.x.a == 1);
276 static_assert (fv23.e.x.f == 7);
277 constexpr union u69 fv24 = { .a = 1 };
278 static_assert (fv24.a == 1);
279 constexpr union u69 fv25 = { .e = { fv18 } };
280 static_assert (fv25.e.x.a == 1);
281 static_assert (fv25.e.x.f == 7);
282 enum fe80 { FE80 = fv25.e.x.f };
283 static_assert (FE80 == 7);
284 constexpr struct s70 fv26 = { fv25 };
285 static_assert (fv26.x.e.x.f == 7);
286 constexpr struct s68 fv27 = { (constexpr struct s67) { 5, 6, 0, 0, nullptr, 9, { 1, 2 } } };
287 static_assert (fv27.x.a == 5);
288 static_assert (fv27.x.f == 9);
289 constexpr struct s68 fv28 = { };
290 static_assert (fv28.x.a == 0);
291 static_assert (fv28.x.f == 0);
292 constexpr int fv29 = { 123 };
293 static_assert (fv29 == 123);
294 constexpr int fv30 = { fv29 };
295 static_assert (fv30 == 123);
296 static_assert ((constexpr struct s67) { 1, 2.0, 0, 0, nullptr, 7, { 3, 4 } }.f == 7);
297 static_assert ((constexpr struct s68) { fv18 }.x.a == 1);
298 static_assert ((constexpr union u69) { }.a == 0);
299 static_assert ((constexpr union u69) { .e = fv21 }.e.x.f == 7);
300 static_assert ((constexpr union u69) { .a = 1 }.a == 1);
301 static_assert ((constexpr union u69) { .e = { fv18 } }.e.x.a == 1);
302 static_assert ((constexpr struct s70) { fv25 }.x.e.x.f == 7);
303 static_assert ((constexpr struct s68) { (constexpr struct s67) { 5, 6, 0, 0, nullptr, 9, { 1, 2 } } }.x.f == 9);
304 static_assert ((constexpr struct s68) { }.x.f == 0);
305 /* Verify that constexpr values can be used in various contexts requiring
306 (integer) constant expressions. */
307 struct fs93 { int x : fv25.e.x.f; };
308 constexpr int fv31 = alignof (int);
309 alignas (fv31) int fv32;
310 constexpr int fv33[100] = { [fv27.x.f] = 7 };
311 static int fv34[fv31];