Debian Codegraph @

clang/test/SemaCXX/constant-expression-cxx11.cpp

raw
1 // RUN: %clang_cc1 -std=c++23 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx20_23,cxx23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
2 // RUN: %clang_cc1 -std=c++20 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx11_20,cxx20_23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
3 // RUN: %clang_cc1 -std=c++11 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx11_20,cxx11 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
4
5 namespace StaticAssertFoldTest {
6
7 int x;
8 static_assert(++x, "test"); // expected-error {{not an integral constant expression}}
9 // cxx20_23-note@-1 {{cannot modify an object that is visible outside that expression}}
10 static_assert(false, "test"); // expected-error {{test}}
11
12 }
13
14 int array[(long)(char *)0]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
15 // expected-warning {{variable length array folded to constant array as an extension}} \
16 // expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
17
18 typedef decltype(sizeof(char)) size_t;
19
20 template<typename T> constexpr T id(const T &t) { return t; }
21 template<typename T> constexpr T min(const T &a, const T &b) {
22 return a < b ? a : b;
23 }
24 template<typename T> constexpr T max(const T &a, const T &b) {
25 return a < b ? b : a;
26 }
27 template<typename T, size_t N> constexpr T *begin(T (&xs)[N]) { return xs; }
28 template<typename T, size_t N> constexpr T *end(T (&xs)[N]) { return xs + N; }
29
30 struct MemberZero {
31 constexpr int zero() const { return 0; }
32 };
33
34 constexpr int arr[]; // expected-error {{constexpr variable 'arr' must be initialized by a constant expression}}
35 constexpr int arr2[2]; // expected-error {{constexpr variable 'arr2' must be initialized by a constant expression}}
36 constexpr int arr3[2] = {};
37
38 namespace DerivedToVBaseCast {
39
40 struct U { int n; };
41 struct V : U { int n; };
42 struct A : virtual V { int n; };
43 struct Aa { int n; };
44 struct B : virtual A, Aa {};
45 struct C : virtual A, Aa {};
46 struct D : B, C {};
47
48 D d;
49 constexpr B *p = &d;
50 constexpr C *q = &d;
51
52 static_assert((void*)p != (void*)q, "");
53 static_assert((A*)p == (A*)q, "");
54 static_assert((Aa*)p != (Aa*)q, "");
55
56 constexpr B &pp = d;
57 constexpr C &qq = d;
58 static_assert((void*)&pp != (void*)&qq, "");
59 static_assert(&(A&)pp == &(A&)qq, "");
60 static_assert(&(Aa&)pp != &(Aa&)qq, "");
61
62 constexpr V *v = p;
63 constexpr V *w = q;
64 constexpr V *x = (A*)p;
65 static_assert(v == w, "");
66 static_assert(v == x, "");
67
68 static_assert((U*)&d == p, "");
69 static_assert((U*)&d == q, "");
70 static_assert((U*)&d == v, "");
71 static_assert((U*)&d == w, "");
72 static_assert((U*)&d == x, "");
73
74 struct X {};
75 struct Y1 : virtual X {};
76 struct Y2 : X {};
77 struct Z : Y1, Y2 {};
78 Z z;
79 static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
80 }
81
82 namespace ConstCast {
83
84 constexpr int n1 = 0;
85 constexpr int n2 = const_cast<int&>(n1);
86 constexpr int *n3 = const_cast<int*>(&n1);
87 constexpr int n4 = *const_cast<int*>(&n1);
88 constexpr const int * const *n5 = const_cast<const int* const*>(&n3);
89 constexpr int **n6 = const_cast<int**>(&n3);
90 constexpr int n7 = **n5;
91 constexpr int n8 = **n6;
92
93 // const_cast from prvalue to xvalue.
94 struct A { int n; };
95 constexpr int n9 = (const_cast<A&&>(A{123})).n;
96 static_assert(n9 == 123, "");
97
98 }
99
100 namespace TemplateArgumentConversion {
101 template<int n> struct IntParam {};
102
103 using IntParam0 = IntParam<0>;
104 using IntParam0 = IntParam<id(0)>;
105 using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}}
106 }
107
108 namespace CaseStatements {
109 int x;
110 void f(int n) {
111 switch (n) {
112 case MemberZero().zero: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}}
113 case id(0): // expected-error {{duplicate case value '0'}}
114 return;
115 case __builtin_constant_p(true) ? (__SIZE_TYPE__)&x : 0:; // expected-error {{constant}}
116 }
117 }
118 }
119
120 extern int &Recurse1;
121 int &Recurse2 = Recurse1; // expected-note {{declared here}}
122 int &Recurse1 = Recurse2;
123 constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
124
125 extern const int RecurseA;
126 const int RecurseB = RecurseA; // expected-note {{declared here}}
127 const int RecurseA = 10;
128 constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
129
130 namespace MemberEnum {
131 struct WithMemberEnum {
132 enum E { A = 42 };
133 } wme;
134
135 static_assert(wme.A == 42, "");
136 }
137
138 namespace DefaultArguments {
139
140 const int z = int();
141 constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
142 return a + b + *c + d;
143 }
144 const int four = 4;
145 constexpr int eight = 8;
146 constexpr const int twentyseven = 27;
147 static_assert(Sum() == 0, "");
148 static_assert(Sum(1) == 1, "");
149 static_assert(Sum(1, four) == 5, "");
150 static_assert(Sum(1, eight, &twentyseven) == 36, "");
151 static_assert(Sum(1, 2, &four, eight) == 15, "");
152
153 }
154
155 namespace Ellipsis {
156
157 // Note, values passed through an ellipsis can't actually be used.
158 constexpr int F(int a, ...) { return a; }
159 static_assert(F(0) == 0, "");
160 static_assert(F(1, 0) == 1, "");
161 static_assert(F(2, "test") == 2, "");
162 static_assert(F(3, &F) == 3, "");
163 int k = 0; // expected-note {{here}}
164 static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
165
166 }
167
168 namespace Recursion {
169 constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
170 static_assert(fib(11) == 89, "");
171
172 constexpr int gcd_inner(int a, int b) {
173 return b == 0 ? a : gcd_inner(b, a % b);
174 }
175 constexpr int gcd(int a, int b) {
176 return gcd_inner(max(a, b), min(a, b));
177 }
178
179 static_assert(gcd(1749237, 5628959) == 7, "");
180 }
181
182 namespace FunctionCast {
183 // When folding, we allow functions to be cast to different types. Such
184 // cast functions cannot be called, even if they're constexpr.
185 constexpr int f() { return 1; }
186 typedef double (*DoubleFn)();
187 typedef int (*IntFn)();
188 int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{Clang extension}}
189 int b[(int)IntFn(f)()]; // ok
190 }
191
192 namespace StaticMemberFunction {
193 struct S {
194 static constexpr int k = 42;
195 static constexpr int f(int n) { return n * k + 2; }
196 } s;
197
198 constexpr int n = s.f(19);
199 static_assert(S::f(19) == 800, "");
200 static_assert(s.f(19) == 800, "");
201 static_assert(n == 800, "");
202
203 constexpr int (*sf1)(int) = &S::f;
204 constexpr int (*sf2)(int) = &s.f;
205 constexpr const int *sk = &s.k;
206
207 // Note, out_of_lifetime returns an invalid pointer value, but we don't do
208 // anything with it (other than copy it around), so there's no UB there.
209 constexpr S *out_of_lifetime(S s) { return &s; } // expected-warning {{address of stack}}
210 static_assert(out_of_lifetime({})->k == 42, "");
211 static_assert(out_of_lifetime({})->f(3) == 128, "");
212
213 // Similarly, using an inactive union member breaks no rules.
214 union U {
215 int n;
216 S s;
217 };
218 constexpr U u = {0};
219 static_assert(u.s.k == 42, "");
220 static_assert(u.s.f(1) == 44, "");
221
222 // And likewise for a past-the-end pointer.
223 static_assert((&s)[1].k == 42, "");
224 static_assert((&s)[1].f(1) == 44, "");
225 }
226
227 namespace ParameterScopes {
228
229 const int k = 42;
230 constexpr const int &ObscureTheTruth(const int &a) { return a; }
231 constexpr const int &MaybeReturnJunk(bool b, const int a) {
232 return ObscureTheTruth(b ? a : k);
233 }
234 static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
235 constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
236
237 constexpr const int MaybeReturnNonstaticRef(bool b, const int a) {
238 return ObscureTheTruth(b ? a : k);
239 }
240 static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
241 constexpr int b = MaybeReturnNonstaticRef(true, 0); // ok
242
243 constexpr int InternalReturnJunk(int n) {
244 return MaybeReturnJunk(true, n); // expected-note {{read of object outside its lifetime}}
245 }
246 constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
247
248 constexpr int LToR(int &n) { return n; }
249 constexpr int GrabCallersArgument(bool which, int a, int b) {
250 return LToR(which ? b : a);
251 }
252 static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
253 static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
254
255 }
256
257 namespace Pointers {
258
259 constexpr int f(int n, const int *a, const int *b, const int *c) {
260 return n == 0 ? 0 : *a + f(n-1, b, c, a);
261 }
262
263 const int x = 1, y = 10, z = 100;
264 static_assert(f(23, &x, &y, &z) == 788, "");
265
266 constexpr int g(int n, int a, int b, int c) {
267 return f(n, &a, &b, &c);
268 }
269 static_assert(g(23, x, y, z) == 788, "");
270
271 }
272
273 namespace FunctionPointers {
274
275 constexpr int Double(int n) { return 2 * n; }
276 constexpr int Triple(int n) { return 3 * n; }
277 constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
278 constexpr int Quadruple(int n) { return Twice(Double, n); }
279 constexpr auto Select(int n) -> int (*)(int) {
280 return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
281 }
282 constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{'F' evaluates to a null function pointer}}
283
284 static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
285
286 constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(nullptr, 0)'}}
287
288 }
289
290 namespace PointerComparison {
291
292 int x, y;
293 static_assert(&x == &y, "false"); // expected-error {{false}}
294 static_assert(&x != &y, "");
295 constexpr bool g1 = &x == &y;
296 constexpr bool g2 = &x != &y;
297 constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
298 constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
299 constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
300 constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
301
302 struct S { int x, y; } s;
303 static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
304 static_assert(&s.x != &s.y, "");
305 static_assert(&s.x <= &s.y, "");
306 static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
307 static_assert(&s.x < &s.y, "");
308 static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
309
310 static_assert(0 == &y, "false"); // expected-error {{false}}
311 static_assert(0 != &y, "");
312 constexpr bool n3 = (int*)0 <= &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
313 constexpr bool n4 = (int*)0 >= &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
314 constexpr bool n5 = (int*)0 < &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
315 constexpr bool n6 = (int*)0 > &y; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
316
317 static_assert(&x == 0, "false"); // expected-error {{false}}
318 static_assert(&x != 0, "");
319 constexpr bool n9 = &x <= (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
320 constexpr bool n10 = &x >= (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
321 constexpr bool n11 = &x < (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
322 constexpr bool n12 = &x > (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
323
324 static_assert(&x == &x, "");
325 static_assert(&x != &x, "false"); // expected-error {{false}}
326 static_assert(&x <= &x, "");
327 static_assert(&x >= &x, "");
328 static_assert(&x < &x, "false"); // expected-error {{false}}
329 static_assert(&x > &x, "false"); // expected-error {{false}}
330
331 constexpr S* sptr = &s;
332 constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // cxx11-error {{constant expression}} cxx11-note {{dynamic_cast}}
333
334 struct U {};
335 struct Str {
336 int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
337 cxx11-warning {{not an integral constant expression}} \
338 cxx11-note {{dynamic_cast is not allowed in a constant expression}}
339 int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
340 expected-warning {{not an integral constant expression}} \
341 expected-note {{reinterpret_cast is not allowed in a constant expression}}
342 int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
343 expected-warning {{not an integral constant expression}} \
344 expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
345 int d : (S*)(42) == (S*)(42); // \
346 expected-warning {{not an integral constant expression}} \
347 expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
348 int e : (Str*)(sptr) == (Str*)(sptr); // \
349 expected-warning {{not an integral constant expression}} \
350 expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
351 int f : &(U&)(*sptr) == &(U&)(*sptr); // \
352 expected-warning {{not an integral constant expression}} \
353 expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
354 int g : (S*)(void*)(sptr) == sptr; // \
355 expected-warning {{not an integral constant expression}} \
356 expected-note {{cast from 'void *' is not allowed in a constant expression}}
357 };
358
359 extern char externalvar[];
360 constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}} expected-note {{reinterpret_cast}}
361 constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}}
362 // expected-note@-1 {{comparison of addresses of literals has unspecified value}}
363 // cxx20_23-warning@-2 {{comparison between two arrays is deprecated}}
364 static_assert(0 != "foo", "");
365
366 }
367
368 namespace MaterializeTemporary {
369
370 constexpr int f(const int &r) { return r; }
371 constexpr int n = f(1);
372
373 constexpr bool same(const int &a, const int &b) { return &a == &b; }
374 constexpr bool sameTemporary(const int &n) { return same(n, n); }
375
376 static_assert(n, "");
377 static_assert(!same(4, 4), "");
378 static_assert(same(n, n), "");
379 static_assert(sameTemporary(9), "");
380
381 struct A { int &&r; };
382 struct B { A &&a1; A &&a2; };
383
384 constexpr B b1 { { 1 }, { 2 } }; // expected-note {{temporary created here}}
385 static_assert(&b1.a1 != &b1.a2, "");
386 static_assert(&b1.a1.r != &b1.a2.r, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
387
388 constexpr B &&b2 { { 3 }, { 4 } }; // expected-note {{temporary created here}}
389 static_assert(&b1 != &b2, "");
390 static_assert(&b1.a1 != &b2.a1, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
391
392 constexpr thread_local B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
393 void foo() {
394 constexpr static B b1 { { 1 }, { 2 } }; // ok
395 constexpr thread_local B b2 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
396 constexpr B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
397 }
398
399 constexpr B &&b4 = ((1, 2), 3, 4, B { {10}, {{20}} });
400 static_assert(&b4 != &b2, "");
401
402 // Proposed DR: copy-elision doesn't trigger lifetime extension.
403 // cxx11-warning@+1 2{{temporary whose address is used as value of local variable 'b5' will be destroyed at the end of the full-expression}}
404 constexpr B b5 = B{ {0}, {0} }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
405
406 namespace NestedNonStatic {
407 // Proposed DR: for a reference constant expression to refer to a static
408 // storage duration temporary, that temporary must itself be initialized
409 // by a constant expression (a core constant expression is not enough).
410 struct A { int &&r; };
411 struct B { A &&a; };
412 constexpr B a = { A{0} }; // ok
413 // cxx11-warning@+1 {{temporary bound to reference member of local variable 'b' will be destroyed at the end of the full-expression}}
414 constexpr B b = { A(A{0}) }; // cxx11-error {{constant expression}} cxx11-note {{reference to temporary}} cxx11-note {{here}}
415 }
416
417 namespace FakeInitList {
418 struct init_list_3_ints { const int (&x)[3]; };
419 struct init_list_2_init_list_3_ints { const init_list_3_ints (&x)[2]; };
420 constexpr init_list_2_init_list_3_ints ils = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } };
421 }
422
423 namespace ConstAddedByReference {
424 const int &r = (0);
425 constexpr int n = r;
426
427 int &&r2 = 0; // expected-note {{created here}}
428 constexpr int n2 = r2; // expected-error {{constant}} expected-note {{read of temporary}}
429
430 struct A { constexpr operator int() const { return 0; }};
431 struct B { constexpr operator const int() const { return 0; }};
432 const int &ra = A();
433 const int &rb = B();
434 constexpr int na = ra;
435 constexpr int nb = rb;
436
437 struct C { int &&r; };
438 constexpr C c1 = {1};
439 constexpr int &c1r = c1.r;
440 constexpr const C &c2 = {2};
441 constexpr int &c2r = c2.r;
442 constexpr C &&c3 = {3}; // expected-note {{created here}}
443 constexpr int &c3r = c3.r; // expected-error {{constant}} expected-note {{read of temporary}}
444 }
445
446 }
447
448 constexpr int strcmp_ce(const char *p, const char *q) {
449 return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
450 }
451
452 namespace StringLiteral {
453
454 template<typename Char>
455 constexpr int MangleChars(const Char *p) {
456 return *p + 3 * (*p ? MangleChars(p+1) : 0);
457 }
458
459 static_assert(MangleChars("constexpr!") == 1768383, "");
460 static_assert(MangleChars(u8"constexpr!") == 1768383, "");
461 static_assert(MangleChars(L"constexpr!") == 1768383, "");
462 static_assert(MangleChars(u"constexpr!") == 1768383, "");
463 static_assert(MangleChars(U"constexpr!") == 1768383, "");
464
465 constexpr char c0 = "nought index"[0];
466 constexpr char c1 = "nice index"[10];
467 constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
468 constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-note {{cannot refer to element -1 of array of 15 elements}}
469 constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} expected-note {{cast that performs the conversions of a reinterpret_cast}}
470
471 constexpr const char *p = "test" + 2;
472 static_assert(*p == 's', "");
473
474 constexpr const char *max_iter(const char *a, const char *b) {
475 return *a < *b ? b : a;
476 }
477 constexpr const char *max_element(const char *a, const char *b) {
478 return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
479 }
480
481 constexpr char str[] = "the quick brown fox jumped over the lazy dog";
482 constexpr const char *max = max_element(begin(str), end(str));
483 static_assert(*max == 'z', "");
484 static_assert(max == str + 38, "");
485
486 static_assert(strcmp_ce("hello world", "hello world") == 0, "");
487 static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
488 static_assert(strcmp_ce("constexpr", "test") < 0, "");
489 static_assert(strcmp_ce("", " ") < 0, "");
490
491 struct S {
492 int n : "foo"[4]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
493 };
494
495 struct T {
496 char c[6];
497 constexpr T() : c{"foo"} {}
498 };
499 constexpr T t;
500
501 static_assert(t.c[0] == 'f', "");
502 static_assert(t.c[1] == 'o', "");
503 static_assert(t.c[2] == 'o', "");
504 static_assert(t.c[3] == 0, "");
505 static_assert(t.c[4] == 0, "");
506 static_assert(t.c[5] == 0, "");
507 static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
508
509 struct U {
510 wchar_t chars[6];
511 int n;
512 } constexpr u = { { L"test" }, 0 };
513 static_assert(u.chars[2] == L's', "");
514
515 struct V {
516 char c[4];
517 constexpr V() : c("hi!") {}
518 };
519 static_assert(V().c[1] == "i"[0], "");
520
521 namespace Parens {
522 constexpr unsigned char a[] = ("foo"), b[] = {"foo"}, c[] = {("foo")},
523 d[4] = ("foo"), e[5] = {"foo"}, f[6] = {("foo")};
524 static_assert(a[0] == 'f', "");
525 static_assert(b[1] == 'o', "");
526 static_assert(c[2] == 'o', "");
527 static_assert(d[0] == 'f', "");
528 static_assert(e[1] == 'o', "");
529 static_assert(f[2] == 'o', "");
530 static_assert(f[5] == 0, "");
531 static_assert(f[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
532 }
533
534 }
535
536 namespace Array {
537
538 template<typename Iter>
539 constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) {
540 return begin == end ? 0 : *begin + Sum(begin+1, end);
541 }
542
543 constexpr int xs[] = { 1, 2, 3, 4, 5 };
544 constexpr int ys[] = { 5, 4, 3, 2, 1 };
545 constexpr int sum_xs = Sum(begin(xs), end(xs));
546 static_assert(sum_xs == 15, "");
547
548 constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
549 const int *xs, const int *ys, int c) {
550 return n ? F(
551 *xs, // expected-note {{read of dereferenced one-past-the-end pointer}}
552 *ys,
553 ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
554 expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
555 expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
556 : c;
557 }
558 constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
559 constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
560 static_assert(InnerProduct == 35, "");
561
562 constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
563 constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
564 static_assert(DiffProd == 8, "");
565 static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
566 expected-error {{constant expression}} \
567 expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
568
569 constexpr const int *p = xs + 3;
570 constexpr int xs4 = p[1]; // ok
571 constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
572 constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}}
573 constexpr int xs0 = p[-3]; // ok
574 constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
575
576 constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
577 static_assert(zs[0][0][0][0] == 1, "");
578 static_assert(zs[1][1][1][1] == 16, "");
579 static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
580 static_assert((&zs[0][0][0][2])[-1] == 2, "");
581 static_assert(**(**(zs + 1) + 1) == 11, "");
582 static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of array of 2 elements in a constant expression}}
583 static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
584 constexpr int err_zs_1_2_0_0 = zs[1][2][0][0]; // \
585 expected-error {{constant expression}} \
586 expected-note {{cannot access array element of pointer past the end}}
587
588 constexpr int fail(const int &p) {
589 return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
590 }
591 static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \
592 expected-error {{static assertion expression is not an integral constant expression}} \
593 expected-note {{in call to 'fail(zs[1][0][1][0])'}}
594
595 constexpr int arr[40] = { 1, 2, 3, [8] = 4 };
596 constexpr int SumNonzero(const int *p) {
597 return *p + (*p ? SumNonzero(p+1) : 0);
598 }
599 constexpr int CountZero(const int *p, const int *q) {
600 return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
601 }
602 static_assert(SumNonzero(arr) == 6, "");
603 static_assert(CountZero(arr, arr + 40) == 36, "");
604
605 struct ArrayElem {
606 constexpr ArrayElem() : n(0) {}
607 int n;
608 constexpr int f() const { return n; }
609 };
610 struct ArrayRVal {
611 constexpr ArrayRVal() {}
612 ArrayElem elems[10];
613 };
614 static_assert(ArrayRVal().elems[3].f() == 0, "");
615
616 namespace CopyCtor {
617 struct A {
618 constexpr A() {}
619 constexpr A(const A &) {}
620 };
621 struct B {
622 A a;
623 int arr[10];
624 };
625 constexpr B b{{}, {1, 2, 3, 4, 5}};
626 constexpr B c = b;
627 static_assert(c.arr[2] == 3, "");
628 static_assert(c.arr[7] == 0, "");
629
630 // OK: the copy ctor for X doesn't read any members.
631 struct X { struct Y {} y; } x1;
632 constexpr X x2 = x1;
633 }
634
635 constexpr int selfref[2][2][2] = {
636 1, selfref[0][0][0] + 1,
637 1, selfref[0][1][0] + 1,
638 1, selfref[0][1][1] + 1 };
639 static_assert(selfref[0][0][0] == 1, "");
640 static_assert(selfref[0][0][1] == 2, "");
641 static_assert(selfref[0][1][0] == 1, "");
642 static_assert(selfref[0][1][1] == 2, "");
643 static_assert(selfref[1][0][0] == 1, "");
644 static_assert(selfref[1][0][1] == 3, "");
645 static_assert(selfref[1][1][0] == 0, "");
646 static_assert(selfref[1][1][1] == 0, "");
647
648 constexpr int badselfref[2][2][2] = { // expected-error {{constant expression}}
649 badselfref[1][0][0] // expected-note {{outside its lifetime}}
650 };
651
652 struct TrivialDefCtor { int n; };
653 typedef TrivialDefCtor TDCArray[2][2];
654 static_assert(TDCArray{}[1][1].n == 0, "");
655
656 struct NonAggregateTDC : TrivialDefCtor {};
657 typedef NonAggregateTDC NATDCArray[2][2];
658 static_assert(NATDCArray{}[1][1].n == 0, "");
659
660 }
661
662 // Per current CWG direction, we reject any cases where pointer arithmetic is
663 // not statically known to be valid.
664 namespace ArrayOfUnknownBound {
665 extern int arr[];
666 constexpr int *a = arr;
667 constexpr int *b = &arr[0];
668 static_assert(a == b, "");
669 constexpr int *c = &arr[1]; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
670 constexpr int *d = &a[1]; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
671 constexpr int *e = a + 1; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
672
673 struct X {
674 int a;
675 int b[]; // expected-warning {{C99}}
676 };
677 extern X x;
678 constexpr int *xb = x.b; // expected-error {{constant}} expected-note {{not supported}}
679
680 struct Y { int a; };
681 extern Y yarr[];
682 constexpr Y *p = yarr;
683 constexpr int *q = &p->a;
684
685 extern const int carr[]; // expected-note {{here}}
686 constexpr int n = carr[0]; // expected-error {{constant}} expected-note {{non-constexpr variable}}
687
688 constexpr int local_extern[] = {1, 2, 3};
689 void f() { extern const int local_extern[]; }
690 static_assert(local_extern[1] == 2, "");
691 }
692
693 namespace DependentValues {
694
695 struct I { int n; typedef I V[10]; };
696 I::V x, y;
697 int g(); // expected-note {{declared here}}
698 template<bool B, typename T> struct S : T {
699 int k;
700 void f() {
701 I::V &cells = B ? x : y;
702 I &i = cells[k];
703 switch (i.n) {}
704
705 constexpr int n = g(); // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr function 'g'}}
706
707 constexpr int m = this->g(); // ok, could be constexpr
708 }
709 };
710
711 extern const int n; // expected-note {{declared here}}
712 template<typename T> void f() {
713 // This is ill-formed, because a hypothetical instantiation at the point of
714 // template definition would be ill-formed due to a construct that does not
715 // depend on a template parameter.
716 constexpr int k = n; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'n' is unknown}}
717 }
718 // It doesn't matter that the instantiation could later become valid:
719 constexpr int n = 4;
720 template void f<int>();
721
722 }
723
724 namespace Class {
725
726 struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
727 constexpr int fn(const A &a) { return a.k; }
728 static_assert(fn(A(4,5)) == 9, "");
729
730 struct B { int n; int m; } constexpr b = { 0, b.n };
731 struct C {
732 constexpr C(C *this_) : m(42), n(this_->m) {} // ok
733 int m, n;
734 };
735 struct D {
736 C c;
737 constexpr D() : c(&c) {}
738 };
739 static_assert(D().c.n == 42, "");
740
741 struct E {
742 constexpr E() : p(&p) {}
743 void *p;
744 };
745 constexpr const E &e1 = E();
746 // This is a constant expression if we elide the copy constructor call, and
747 // is not a constant expression if we don't! But we do, so it is.
748 constexpr E e2 = E();
749 static_assert(e2.p == &e2.p, "");
750 constexpr E e3;
751 static_assert(e3.p == &e3.p, "");
752
753 extern const class F f;
754 struct F {
755 constexpr F() : p(&f.p) {}
756 const void *p;
757 };
758 constexpr F f;
759
760 struct G {
761 struct T {
762 constexpr T(T *p) : u1(), u2(p) {}
763 union U1 {
764 constexpr U1() {}
765 int a, b = 42;
766 } u1;
767 union U2 {
768 constexpr U2(T *p) : c(p->u1.b) {}
769 int c, d;
770 } u2;
771 } t;
772 constexpr G() : t(&t) {}
773 } constexpr g;
774
775 static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
776 static_assert(g.t.u1.b == 42, "");
777 static_assert(g.t.u2.c == 42, "");
778 static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
779
780 struct S {
781 int a, b;
782 const S *p;
783 double d;
784 const char *q;
785
786 constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
787 };
788
789 S global(43, &global);
790
791 static_assert(S(15, &global).b == 15, "");
792
793 constexpr bool CheckS(const S &s) {
794 return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
795 }
796 static_assert(CheckS(S(27, &global)), "");
797
798 struct Arr {
799 char arr[3];
800 constexpr Arr() : arr{'x', 'y', 'z'} {}
801 };
802 constexpr int hash(Arr &&a) {
803 return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
804 }
805 constexpr int k = hash(Arr());
806 static_assert(k == 0x007a7978, "");
807
808
809 struct AggregateInit {
810 const char &c;
811 int n;
812 double d;
813 int arr[5];
814 void *p;
815 };
816
817 constexpr AggregateInit agg1 = { "hello"[0] };
818
819 static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
820 static_assert(agg1.n == 0, "");
821 static_assert(agg1.d == 0.0, "");
822 static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
823 static_assert(agg1.arr[0] == 0, "");
824 static_assert(agg1.arr[4] == 0, "");
825 static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
826 static_assert(agg1.p == nullptr, "");
827
828 static constexpr const unsigned char uc[] = { "foo" };
829 static_assert(uc[0] == 'f', "");
830 static_assert(uc[3] == 0, "");
831
832 namespace SimpleDerivedClass {
833
834 struct B {
835 constexpr B(int n) : a(n) {}
836 int a;
837 };
838 struct D : B {
839 constexpr D(int n) : B(n) {}
840 };
841 constexpr D d(3);
842 static_assert(d.a == 3, "");
843
844 }
845
846 struct Bottom { constexpr Bottom() {} };
847 struct Base : Bottom {
848 constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
849 int a;
850 const char *b;
851 };
852 struct Base2 : Bottom {
853 constexpr Base2(const int &r) : r(r) {}
854 int q = 123;
855 const int &r;
856 };
857 struct Derived : Base, Base2 {
858 constexpr Derived() : Base(76), Base2(a) {}
859 int c = r + b[1];
860 };
861
862 constexpr bool operator==(const Base &a, const Base &b) {
863 return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
864 }
865
866 constexpr Base base;
867 constexpr Base base2(76);
868 constexpr Derived derived;
869 static_assert(derived.a == 76, "");
870 static_assert(derived.b[2] == 's', "");
871 static_assert(derived.c == 76 + 'e', "");
872 static_assert(derived.q == 123, "");
873 static_assert(derived.r == 76, "");
874 static_assert(&derived.r == &derived.a, "");
875
876 static_assert(!(derived == base), "");
877 static_assert(derived == base2, "");
878
879 constexpr Bottom &bot1 = (Base&)derived;
880 constexpr Bottom &bot2 = (Base2&)derived;
881 static_assert(&bot1 != &bot2, "");
882
883 constexpr Bottom *pb1 = (Base*)&derived;
884 constexpr Bottom *pb2 = (Base2*)&derived;
885 static_assert(&pb1 != &pb2, "");
886 static_assert(pb1 == &bot1, "");
887 static_assert(pb2 == &bot2, "");
888
889 constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base2'}}
890 constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base'}}
891 constexpr Base2 &ok2 = (Base2&)bot2;
892 static_assert(&ok2 == &derived, "");
893
894 constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base2'}}
895 constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base'}}
896 constexpr Base2 *pok2 = (Base2*)pb2;
897 static_assert(pok2 == &derived, "");
898 static_assert(&ok2 == pok2, "");
899 static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
900 static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
901
902 // Core issue 903: we do not perform constant evaluation when checking for a
903 // null pointer in C++11. Just check for an integer literal with value 0.
904 constexpr Base *nullB = 42 - 6 * 7; // expected-error {{cannot initialize a variable of type 'Base *const' with an rvalue of type 'int'}}
905 constexpr Base *nullB1 = 0;
906 static_assert((Bottom*)nullB == 0, "");
907 static_assert((Derived*)nullB1 == 0, "");
908 static_assert((void*)(Bottom*)nullB1 == (void*)(Derived*)nullB1, "");
909 Base *nullB2 = '\0'; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'char'}}
910 Base *nullB3 = (0);
911 Base *nullB4 = false; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'bool'}}
912 Base *nullB5 = ((0ULL));
913 Base *nullB6 = 0.; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'double'}}
914 enum Null { kNull };
915 Base *nullB7 = kNull; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'Class::Null'}}
916 static_assert(nullB1 == (1 - 1), ""); // expected-error {{comparison between pointer and integer}}
917
918
919
920 namespace ConversionOperators {
921
922 struct T {
923 constexpr T(int n) : k(5*n - 3) {}
924 constexpr operator int() const { return k; }
925 int k;
926 };
927
928 struct S {
929 constexpr S(int n) : k(2*n + 1) {}
930 constexpr operator int() const { return k; }
931 constexpr operator T() const { return T(k); }
932 int k;
933 };
934
935 constexpr bool check(T a, T b) { return a == b.k; }
936
937 static_assert(S(5) == 11, "");
938 static_assert(check(S(5), 11), "");
939
940 namespace PR14171 {
941
942 struct X {
943 constexpr (operator int)() const { return 0; }
944 };
945 static_assert(X() == 0, "");
946
947 }
948
949 }
950
951 struct This {
952 constexpr int f() const { return 0; }
953 static constexpr int g() { return 0; }
954 void h() {
955 constexpr int x = f(); // expected-error {{must be initialized by a constant}}
956 // expected-note@-1 {{implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
957 constexpr int y = this->f(); // expected-error {{must be initialized by a constant}}
958 // expected-note-re@-1 {{{{^}}use of 'this' pointer}}
959 constexpr int z = g();
960 static_assert(z == 0, "");
961 }
962 };
963
964 }
965
966 namespace Temporaries {
967
968 struct S {
969 constexpr S() {}
970 constexpr int f() const;
971 constexpr int g() const;
972 };
973 struct T : S {
974 constexpr T(int n) : S(), n(n) {}
975 int n;
976 };
977 constexpr int S::f() const {
978 return static_cast<const T*>(this)->n; // expected-note 5{{cannot cast}}
979 }
980 constexpr int S::g() const {
981 // FIXME: Better diagnostic for this.
982 return this->*(int(S::*))&T::n; // expected-note {{subexpression}}
983 }
984 // The T temporary is implicitly cast to an S subobject, but we can recover the
985 // T full-object via a base-to-derived cast, or a derived-to-base-casted member
986 // pointer.
987 static_assert(S().f(), ""); // expected-error {{constant expression}} expected-note {{in call to 'S().f()'}}
988 static_assert(S().g(), ""); // expected-error {{constant expression}} expected-note {{in call to 'S().g()'}}
989 constexpr S sobj;
990 constexpr const S& slref = sobj;
991 constexpr const S&& srref = S();
992 constexpr const S *sptr = &sobj;
993 static_assert(sobj.f(), ""); // expected-error {{constant expression}} \
994 expected-note {{in call to 'sobj.f()'}}
995 static_assert(sptr->f(), ""); // expected-error {{constant expression}} \
996 expected-note {{in call to 'sptr->f()'}}
997 static_assert(slref.f(), ""); // expected-error {{constant expression}} \
998 expected-note {{in call to 'slref.f()'}}
999 static_assert(srref.f(), ""); // expected-error {{constant expression}} \
1000 expected-note {{in call to 'srref.f()'}}
1001 static_assert(T(3).f() == 3, "");
1002 static_assert(T(4).g() == 4, "");
1003
1004 constexpr int f(const S &s) {
1005 return static_cast<const T&>(s).n;
1006 }
1007 constexpr int n = f(T(5));
1008 static_assert(f(T(5)) == 5, "");
1009
1010 constexpr bool b(int n) { return &n; }
1011 static_assert(b(0), "");
1012
1013 struct NonLiteral {
1014 NonLiteral();
1015 int f();
1016 };
1017 constexpr int k = NonLiteral().f(); // expected-error {{constant expression}} expected-note {{non-literal type 'NonLiteral'}}
1018
1019 }
1020
1021 namespace Union {
1022
1023 union U {
1024 int a;
1025 int b;
1026 };
1027
1028 constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } };
1029 static_assert(u[0].a == 0, "");
1030 static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
1031 static_assert(u[1].b == 1, "");
1032 static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
1033 static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}}
1034 static_assert((&(u[1]) + 1 + 1)->b == 3, "");
1035
1036 constexpr U v = {};
1037 static_assert(v.a == 0, "");
1038
1039 union Empty {};
1040 constexpr Empty e = {};
1041
1042 // Make sure we handle trivial copy constructors for unions.
1043 constexpr U x = {42};
1044 constexpr U y = x;
1045 static_assert(y.a == 42, "");
1046 static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}}
1047
1048 }
1049
1050 namespace MemberPointer {
1051 struct A {
1052 constexpr A(int n) : n(n) {}
1053 int n;
1054 constexpr int f() const { return n + 3; }
1055 };
1056 constexpr A a(7);
1057 static_assert(A(5).*&A::n == 5, "");
1058 static_assert((&a)->*&A::n == 7, "");
1059 static_assert((A(8).*&A::f)() == 11, "");
1060 static_assert(((&a)->*&A::f)() == 10, "");
1061
1062 struct B : A {
1063 constexpr B(int n, int m) : A(n), m(m) {}
1064 int m;
1065 constexpr int g() const { return n + m + 1; }
1066 };
1067 constexpr B b(9, 13);
1068 static_assert(B(4, 11).*&A::n == 4, "");
1069 static_assert(B(4, 11).*&B::m == 11, "");
1070 static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
1071 static_assert((&b)->*&A::n == 9, "");
1072 static_assert((&b)->*&B::m == 13, "");
1073 static_assert((&b)->*(int(A::*))&B::m == 13, "");
1074 static_assert((B(4, 11).*&A::f)() == 7, "");
1075 static_assert((B(4, 11).*&B::g)() == 16, "");
1076 static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
1077 static_assert(((&b)->*&A::f)() == 12, "");
1078 static_assert(((&b)->*&B::g)() == 23, "");
1079 static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
1080
1081 struct S {
1082 constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
1083 m(m), n(n), pf(pf), pn(pn) {}
1084 constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
1085
1086 constexpr int f() const { return this->*pn; }
1087 virtual int g() const;
1088
1089 int m, n;
1090 int (S::*pf)() const;
1091 int S::*pn;
1092 };
1093
1094 constexpr int S::*pm = &S::m;
1095 constexpr int S::*pn = &S::n;
1096 constexpr int (S::*pf)() const = &S::f;
1097 constexpr int (S::*pg)() const = &S::g;
1098
1099 constexpr S s(2, 5, &S::f, &S::m);
1100
1101 static_assert((s.*&S::f)() == 2, "");
1102 static_assert((s.*s.pf)() == 2, "");
1103
1104 static_assert(pf == &S::f, "");
1105 static_assert(pf == s.*&S::pf, "");
1106 static_assert(pm == &S::m, "");
1107 static_assert(pm != pn, "");
1108 static_assert(s.pn != pn, "");
1109 static_assert(s.pn == pm, "");
1110 static_assert(pg != nullptr, "");
1111 static_assert(pf != nullptr, "");
1112 static_assert((int S::*)nullptr == nullptr, "");
1113 static_assert(pg == pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
1114 static_assert(pf != pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
1115
1116 template<int n> struct T : T<n-1> {};
1117 template<> struct T<0> { int n; };
1118 template<> struct T<30> : T<29> { int m; };
1119
1120 T<17> t17;
1121 T<30> t30;
1122
1123 constexpr int (T<10>::*deepn) = &T<0>::n;
1124 static_assert(&(t17.*deepn) == &t17.n, "");
1125 static_assert(deepn == &T<2>::n, "");
1126
1127 constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
1128 constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
1129 static_assert(&(t30.*deepm) == &t30.m, "");
1130 static_assert(deepm == &T<50>::m, "");
1131 static_assert(deepm != deepn, "");
1132
1133 constexpr T<5> *p17_5 = &t17;
1134 constexpr T<13> *p17_13 = (T<13>*)p17_5;
1135 constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'T<17>' to type 'T<23>'}}
1136 static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
1137 static_assert(&(p17_13->*deepn) == &t17.n, "");
1138 constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
1139
1140 constexpr T<5> *p30_5 = &t30;
1141 constexpr T<23> *p30_23 = (T<23>*)p30_5;
1142 constexpr T<13> *p30_13 = p30_23;
1143 static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
1144 static_assert(&(p30_13->*deepn) == &t30.n, "");
1145 static_assert(&(p30_23->*deepn) == &t30.n, "");
1146 static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
1147 static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
1148 static_assert(&(p30_23->*deepm) == &t30.m, "");
1149
1150 struct Base { int n; };
1151 template<int N> struct Mid : Base {};
1152 struct Derived : Mid<0>, Mid<1> {};
1153 static_assert(&Mid<0>::n == &Mid<1>::n, "");
1154 static_assert((int Derived::*)(int Mid<0>::*)&Mid<0>::n !=
1155 (int Derived::*)(int Mid<1>::*)&Mid<1>::n, "");
1156 static_assert(&Mid<0>::n == (int Mid<0>::*)&Base::n, "");
1157
1158 constexpr int apply(const A &a, int (A::*f)() const) {
1159 return (a.*f)();
1160 }
1161 static_assert(apply(A(2), &A::f) == 5, "");
1162 }
1163
1164 namespace ArrayBaseDerived {
1165
1166 struct Base {
1167 constexpr Base() {}
1168 int n = 0;
1169 };
1170 struct Derived : Base {
1171 constexpr Derived() {}
1172 constexpr const int *f() const { return &n; }
1173 };
1174
1175 constexpr Derived a[10];
1176 constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
1177 constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
1178 static_assert(pb3 == pd3, "");
1179
1180 // pb3 does not point to an array element.
1181 constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
1182 constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}}
1183 constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}}
1184 constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}}
1185 constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}}
1186 constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}}
1187 constexpr Base *pb3a = pb4 - 1;
1188
1189 // pb4 does not point to a Derived.
1190 constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}}
1191 constexpr Derived *pd3a = (Derived*)pb3a;
1192 constexpr int pd3n = pd3a->n;
1193
1194 // pd3a still points to the Derived array.
1195 constexpr Derived *pd6 = pd3a + 3;
1196 static_assert(pd6 == &a[6], "");
1197 constexpr Derived *pd9 = pd6 + 3;
1198 constexpr Derived *pd10 = pd6 + 4;
1199 constexpr int pd9n = pd9->n; // ok
1200 constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}}
1201 constexpr int pd0n = pd10[-10].n;
1202 constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}}
1203
1204 constexpr Base *pb9 = pd9;
1205 constexpr const int *(Base::*pfb)() const =
1206 static_cast<const int *(Base::*)() const>(&Derived::f);
1207 static_assert((pb9->*pfb)() == &a[9].n, "");
1208 }
1209
1210 namespace Complex {
1211
1212 class complex {
1213 int re, im;
1214 public:
1215 constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
1216 constexpr complex(const complex &o) : re(o.re), im(o.im) {}
1217 constexpr complex operator-() const { return complex(-re, -im); }
1218 friend constexpr complex operator+(const complex &l, const complex &r) {
1219 return complex(l.re + r.re, l.im + r.im);
1220 }
1221 friend constexpr complex operator-(const complex &l, const complex &r) {
1222 return l + -r;
1223 }
1224 friend constexpr complex operator*(const complex &l, const complex &r) {
1225 return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
1226 }
1227 friend constexpr bool operator==(const complex &l, const complex &r) {
1228 return l.re == r.re && l.im == r.im;
1229 }
1230 constexpr bool operator!=(const complex &r) const {
1231 return re != r.re || im != r.im;
1232 }
1233 constexpr int real() const { return re; }
1234 constexpr int imag() const { return im; }
1235 };
1236
1237 constexpr complex i = complex(0, 1);
1238 constexpr complex k = (3 + 4*i) * (6 - 4*i);
1239 static_assert(complex(1,0).real() == 1, "");
1240 static_assert(complex(1,0).imag() == 0, "");
1241 static_assert(((complex)1).imag() == 0, "");
1242 static_assert(k.real() == 34, "");
1243 static_assert(k.imag() == 12, "");
1244 static_assert(k - 34 == 12*i, "");
1245 static_assert((complex)1 == complex(1), "");
1246 static_assert((complex)1 != complex(0, 1), "");
1247 static_assert(complex(1) == complex(1), "");
1248 static_assert(complex(1) != complex(0, 1), "");
1249 constexpr complex makeComplex(int re, int im) { return complex(re, im); }
1250 static_assert(makeComplex(1,0) == complex(1), "");
1251 static_assert(makeComplex(1,0) != complex(0, 1), "");
1252
1253 class complex_wrap : public complex {
1254 public:
1255 constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
1256 constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
1257 };
1258
1259 static_assert((complex_wrap)1 == complex(1), "");
1260 static_assert((complex)1 != complex_wrap(0, 1), "");
1261 static_assert(complex(1) == complex_wrap(1), "");
1262 static_assert(complex_wrap(1) != complex(0, 1), "");
1263 constexpr complex_wrap makeComplexWrap(int re, int im) {
1264 return complex_wrap(re, im);
1265 }
1266 static_assert(makeComplexWrap(1,0) == complex(1), "");
1267 static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
1268
1269 }
1270
1271 namespace PR11595 {
1272 struct A { constexpr bool operator==(int x) const { return true; } };
1273 struct B { B(); A& x; };
1274 static_assert(B().x == 3, ""); // expected-error {{constant expression}} expected-note {{non-literal type 'B' cannot be used in a constant expression}}
1275
1276 constexpr bool f(int k) { // cxx11_20-error {{constexpr function never produces a constant expression}}
1277 return B().x == k; // cxx11_20-note {{non-literal type 'B' cannot be used in a constant expression}}
1278 }
1279 }
1280
1281 namespace ExprWithCleanups {
1282 struct A { A(); ~A(); int get(); };
1283 constexpr int get(bool FromA) { return FromA ? A().get() : 1; }
1284 constexpr int n = get(false);
1285 }
1286
1287 namespace Volatile {
1288
1289 volatile constexpr int n1 = 0; // expected-note {{here}}
1290 volatile const int n2 = 0; // expected-note {{here}}
1291 int n3 = 37; // expected-note {{declared here}}
1292
1293 constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1294 constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1295 constexpr int m1b = const_cast<const int&>(n1); // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
1296 constexpr int m2b = const_cast<const int&>(n2); // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
1297
1298 struct T { int n; };
1299 const T t = { 42 }; // expected-note {{declared here}}
1300
1301 constexpr int f(volatile int &&r) {
1302 return r; // expected-note {{read of volatile-qualified type 'volatile int'}}
1303 }
1304 constexpr int g(volatile int &&r) {
1305 return const_cast<int&>(r); // expected-note {{read of volatile temporary is not allowed in a constant expression}}
1306 }
1307 struct S {
1308 int j : f(0); // expected-error {{constant expression}} expected-note {{in call to 'f(0)'}}
1309 int k : g(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'g(0)'}}
1310 int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
1311 int m : t.n; // expected-warning{{width of bit-field 'm' (42 bits)}} expected-warning{{expression is not an integral constant expression}} expected-note{{read of non-constexpr variable 't' is not allowed}}
1312 };
1313
1314 }
1315
1316 namespace ExternConstexpr {
1317 extern constexpr int n = 0;
1318 extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}}
1319 void f() {
1320 extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}}
1321 constexpr int j = 0;
1322 constexpr int k; // expected-error {{constexpr variable 'k' must be initialized by a constant expression}}
1323 }
1324
1325 extern const int q;
1326 constexpr int g() { return q; } // expected-note {{outside its lifetime}}
1327 constexpr int q = g(); // expected-error {{constant expression}} expected-note {{in call}}
1328
1329 extern int r; // cxx11_20-note {{here}}
1330 constexpr int h() { return r; } // cxx11_20-error {{never produces a constant}} cxx11_20-note {{read of non-const}}
1331
1332 struct S { int n; };
1333 extern const S s;
1334 constexpr int x() { return s.n; } // expected-note {{outside its lifetime}}
1335 constexpr S s = {x()}; // expected-error {{constant expression}} expected-note {{in call}}
1336 }
1337
1338 namespace ComplexConstexpr {
1339 constexpr _Complex float test1 = {}; // expected-warning {{'_Complex' is a C99 extension}}
1340 constexpr _Complex float test2 = {1}; // expected-warning {{'_Complex' is a C99 extension}}
1341 constexpr _Complex double test3 = {1,2}; // expected-warning {{'_Complex' is a C99 extension}}
1342 constexpr _Complex int test4 = {4}; // expected-warning {{'_Complex' is a C99 extension}}
1343 constexpr _Complex int test5 = 4; // expected-warning {{'_Complex' is a C99 extension}}
1344 constexpr _Complex int test6 = {5,6}; // expected-warning {{'_Complex' is a C99 extension}}
1345 typedef _Complex float fcomplex; // expected-warning {{'_Complex' is a C99 extension}}
1346 constexpr fcomplex test7 = fcomplex();
1347
1348 constexpr const double &t2r = __real test3;
1349 constexpr const double &t2i = __imag test3;
1350 static_assert(&t2r + 1 == &t2i, "");
1351 static_assert(t2r == 1.0, "");
1352 static_assert(t2i == 2.0, "");
1353 constexpr const double *t2p = &t2r;
1354 static_assert(t2p[-1] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element -1 of array of 2 elements}}
1355 static_assert(t2p[0] == 1.0, "");
1356 static_assert(t2p[1] == 2.0, "");
1357 static_assert(t2p[2] == 0.0, ""); // expected-error {{constant expr}} expected-note {{one-past-the-end pointer}}
1358 static_assert(t2p[3] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element 3 of array of 2 elements}}
1359 constexpr _Complex float *p = 0; // expected-warning {{'_Complex' is a C99 extension}}
1360 constexpr float pr = __real *p; // expected-error {{constant expr}} expected-note {{cannot access real component of null}}
1361 constexpr float pi = __imag *p; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of null}}
1362 constexpr const _Complex double *q = &test3 + 1; // expected-warning {{'_Complex' is a C99 extension}}
1363 constexpr double qr = __real *q; // expected-error {{constant expr}} expected-note {{cannot access real component of pointer past the end}}
1364 constexpr double qi = __imag *q; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of pointer past the end}}
1365
1366 static_assert(__real test6 == 5, "");
1367 static_assert(__imag test6 == 6, "");
1368 static_assert(&__imag test6 == &__real test6 + 1, "");
1369 }
1370
1371 // _Atomic(T) is exactly like T for the purposes of constant expression
1372 // evaluation..
1373 namespace Atomic {
1374 constexpr _Atomic int n = 3; // expected-warning {{'_Atomic' is a C11 extension}}
1375
1376 struct S { _Atomic(double) d; }; // expected-warning {{'_Atomic' is a C11 extension}}
1377 constexpr S s = { 0.5 };
1378 constexpr double d1 = s.d;
1379 constexpr double d2 = n;
1380 constexpr _Atomic double d3 = n; // expected-warning {{'_Atomic' is a C11 extension}}
1381
1382 constexpr _Atomic(int) n2 = d3; // expected-warning {{'_Atomic' is a C11 extension}}
1383 static_assert(d1 == 0.5, "");
1384 static_assert(d3 == 3.0, "");
1385
1386 namespace PR16056 {
1387 struct TestVar {
1388 _Atomic(int) value; // expected-warning {{'_Atomic' is a C11 extension}}
1389 constexpr TestVar(int value) : value(value) {}
1390 };
1391 constexpr TestVar testVar{-1};
1392 static_assert(testVar.value == -1, "");
1393 }
1394
1395 namespace PR32034 {
1396 struct A {};
1397 struct B { _Atomic(A) a; }; // expected-warning {{'_Atomic' is a C11 extension}}
1398 constexpr int n = (B(), B(), 0);
1399
1400 struct C { constexpr C() {} void *self = this; };
1401 constexpr _Atomic(C) c = C(); // expected-warning {{'_Atomic' is a C11 extension}}
1402 }
1403 }
1404
1405 namespace InstantiateCaseStmt {
1406 template<int x> constexpr int f() { return x; }
1407 template<int x> int g(int c) { switch(c) { case f<x>(): return 1; } return 0; }
1408 int gg(int c) { return g<4>(c); }
1409 }
1410
1411 namespace ConvertedConstantExpr {
1412 extern int &m;
1413 extern int &n; // expected-note 2{{declared here}}
1414
1415 constexpr int k = 4;
1416 int &m = const_cast<int&>(k);
1417
1418 // If we have nothing more interesting to say, ensure we don't produce a
1419 // useless note and instead just point to the non-constant subexpression.
1420 enum class E {
1421 em = m,
1422 en = n, // expected-error {{not a constant expression}} expected-note {{initializer of 'n' is unknown}}
1423 eo = (m + // expected-error {{not a constant expression}}
1424 n // expected-note {{initializer of 'n' is unknown}}
1425 ),
1426 eq = reinterpret_cast<long>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}}
1427 };
1428 }
1429
1430 namespace IndirectField {
1431 struct S {
1432 struct { // expected-warning {{GNU extension}}
1433 union { // expected-warning {{declared in an anonymous struct}}
1434 struct { // expected-warning {{GNU extension}} expected-warning {{declared in an anonymous union}}
1435 int a;
1436 int b;
1437 };
1438 int c;
1439 };
1440 int d;
1441 };
1442 union {
1443 int e;
1444 int f;
1445 };
1446 constexpr S(int a, int b, int d, int e) : a(a), b(b), d(d), e(e) {}
1447 constexpr S(int c, int d, int f) : c(c), d(d), f(f) {}
1448 };
1449
1450 constexpr S s1(1, 2, 3, 4);
1451 constexpr S s2(5, 6, 7);
1452
1453 // FIXME: The diagnostics here do a very poor job of explaining which unnamed
1454 // member is active and which is requested.
1455 static_assert(s1.a == 1, "");
1456 static_assert(s1.b == 2, "");
1457 static_assert(s1.c == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1458 static_assert(s1.d == 3, "");
1459 static_assert(s1.e == 4, "");
1460 static_assert(s1.f == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1461
1462 static_assert(s2.a == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1463 static_assert(s2.b == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1464 static_assert(s2.c == 5, "");
1465 static_assert(s2.d == 6, "");
1466 static_assert(s2.e == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1467 static_assert(s2.f == 7, "");
1468 }
1469
1470 // DR1405: don't allow reading mutable members in constant expressions.
1471 namespace MutableMembers {
1472 struct MM {
1473 mutable int n; // expected-note 3{{declared here}}
1474 } constexpr mm = { 4 };
1475 constexpr int mmn = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1476 int x = (mm.n = 1, 3);
1477 constexpr int mmn2 = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1478
1479 // Here's one reason why allowing this would be a disaster...
1480 template<int n> struct Id { int k = n; };
1481 int f() {
1482 constexpr MM m = { 0 };
1483 ++m.n;
1484 return Id<m.n>().k; // expected-error {{not a constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1485 }
1486
1487 struct A { int n; };
1488 struct B { mutable A a; }; // expected-note {{here}}
1489 struct C { B b; };
1490 constexpr C c[3] = {};
1491 constexpr int k = c[1].b.a.n; // expected-error {{constant expression}} expected-note {{mutable}}
1492
1493 struct D { int x; mutable int y; }; // expected-note {{here}}
1494 constexpr D d1 = { 1, 2 };
1495 int l = ++d1.y;
1496 constexpr D d2 = d1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1497
1498 struct E {
1499 union {
1500 int a;
1501 mutable int b; // expected-note {{here}}
1502 };
1503 };
1504 constexpr E e1 = {{1}};
1505 constexpr E e2 = e1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1506
1507 struct F {
1508 union U { };
1509 mutable U u;
1510 struct X { };
1511 mutable X x;
1512 struct Y : X { X x; U u; };
1513 mutable Y y;
1514 int n;
1515 };
1516 // This is OK; we don't actually read any mutable state here.
1517 constexpr F f1 = {};
1518 constexpr F f2 = f1;
1519
1520 struct G {
1521 struct X {};
1522 union U { X a; };
1523 mutable U u; // expected-note {{here}}
1524 };
1525 constexpr G g1 = {};
1526 constexpr G g2 = g1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1527 constexpr G::U gu1 = {};
1528 constexpr G::U gu2 = gu1;
1529
1530 union H {
1531 mutable G::X gx; // expected-note {{here}}
1532 };
1533 constexpr H h1 = {};
1534 constexpr H h2 = h1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1535 }
1536
1537 namespace Fold {
1538
1539 // This macro forces its argument to be constant-folded, even if it's not
1540 // otherwise a constant expression.
1541 #define fold(x) (__builtin_constant_p(x) ? (x) : (x))
1542
1543 constexpr int n = (long)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}}
1544 constexpr int m = fold((long)(char*)123); // ok
1545 static_assert(m == 123, "");
1546
1547 #undef fold
1548
1549 }
1550
1551 namespace DR1454 {
1552
1553 constexpr const int &f(const int &n) { return n; }
1554 constexpr int k1 = f(0); // ok
1555
1556 struct Wrap {
1557 const int &value;
1558 };
1559 constexpr const Wrap &g(const Wrap &w) { return w; }
1560 constexpr int k2 = g({0}).value; // ok
1561
1562 // The temporary here has static storage duration, so we can bind a constexpr
1563 // reference to it.
1564 constexpr const int &i = 1;
1565 constexpr const int j = i;
1566 static_assert(j == 1, "");
1567
1568 // The temporary here is not const, so it can't be read outside the expression
1569 // in which it was created (per the C++14 rules, which we use to avoid a C++11
1570 // defect).
1571 constexpr int &&k = 1; // expected-note {{temporary created here}}
1572 constexpr const int l = k; // expected-error {{constant expression}} expected-note {{read of temporary}}
1573
1574 void f() {
1575 // The temporary here has automatic storage duration, so we can't bind a
1576 // constexpr reference to it.
1577 constexpr const int &i = 1; // expected-error {{constant expression}} expected-note 2{{temporary}}
1578 }
1579
1580 }
1581
1582 namespace RecursiveOpaqueExpr {
1583 template<typename Iter>
1584 constexpr auto LastNonzero(Iter p, Iter q) -> decltype(+*p) {
1585 return p != q ? (LastNonzero(p+1, q) ?: *p) : 0; // expected-warning {{GNU}}
1586 }
1587
1588 constexpr int arr1[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 0 };
1589 static_assert(LastNonzero(begin(arr1), end(arr1)) == 4, "");
1590
1591 constexpr int arr2[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 5 };
1592 static_assert(LastNonzero(begin(arr2), end(arr2)) == 5, "");
1593
1594 constexpr int arr3[] = {
1595 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1596 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1597 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1598 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1599 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1600 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1601 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1602 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1603 static_assert(LastNonzero(begin(arr3), end(arr3)) == 2, "");
1604 }
1605
1606 namespace VLASizeof {
1607
1608 void f(int k) { // expected-note {{here}}
1609 int arr[k]; // expected-warning {{Clang extension}} expected-note {{function parameter 'k'}}
1610 constexpr int n = 1 +
1611 sizeof(arr) // expected-error {{constant expression}}
1612 * 3;
1613 }
1614 }
1615
1616 namespace CompoundLiteral {
1617 // Matching GCC, file-scope array compound literals initialized by constants
1618 // are lifetime-extended.
1619 constexpr int *p = (int*)(int[1]){3}; // expected-warning {{C99}}
1620 static_assert(*p == 3, ""); // expected-error {{static assertion expression is not an integral constant expression}}
1621 // expected-note@-1 {{subexpression not valid}}
1622 // expected-note@-3 {{declared here}}
1623 static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}}
1624 // expected-error@-1 {{static assertion expression is not an integral constant expression}}
1625 // expected-note@-2 {{subexpression not valid}}
1626 // expected-note@-3 {{declared here}}
1627
1628 // Other kinds are not.
1629 struct X { int a[2]; };
1630 constexpr int *n = (X){1, 2}.a; // expected-warning {{C99}} expected-warning {{temporary}}
1631 // expected-error@-1 {{constant expression}}
1632 // expected-note@-2 {{pointer to subobject of temporary}}
1633 // expected-note@-3 {{temporary created here}}
1634
1635 void f() {
1636 static constexpr int *p = (int*)(int[1]){3}; // expected-warning {{C99}} expected-warning {{temporary}}
1637 // expected-error@-1 {{constant expression}}
1638 // expected-note@-2 {{pointer to subobject of temporary}}
1639 // expected-note@-3 {{temporary created here}}
1640 static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}}
1641 }
1642 }
1643
1644 namespace Vector {
1645 typedef int __attribute__((vector_size(16))) VI4;
1646 constexpr VI4 f(int n) {
1647 return VI4 { n * 3, n + 4, n - 5, n / 6 };
1648 }
1649 constexpr auto v1 = f(10);
1650
1651 typedef double __attribute__((vector_size(32))) VD4;
1652 constexpr VD4 g(int n) {
1653 return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 }; // expected-warning {{C99}}
1654 }
1655 constexpr auto v2 = g(4);
1656 }
1657
1658 // PR12626, redux
1659 namespace InvalidClasses {
1660 void test0() {
1661 struct X; // expected-note {{forward declaration}}
1662 struct Y { bool b; X x; }; // expected-error {{field has incomplete type}}
1663 Y y;
1664 auto& b = y.b;
1665 }
1666 }
1667
1668 namespace NamespaceAlias {
1669 constexpr int f() {
1670 namespace NS = NamespaceAlias; // cxx11-warning {{use of this statement in a constexpr function is a C++14 extension}}
1671 return &NS::f != nullptr;
1672 }
1673 }
1674
1675 // Constructors can be implicitly constexpr, even for a non-literal type.
1676 namespace ImplicitConstexpr {
1677 struct Q { Q() = default; Q(const Q&) = default; Q(Q&&) = default; ~Q(); }; // expected-note 3{{here}}
1678 struct R { constexpr R() noexcept; constexpr R(const R&) noexcept; constexpr R(R&&) noexcept; ~R() noexcept; };
1679 struct S { R r; }; // expected-note 3{{here}}
1680 struct T { T(const T&) noexcept; T(T &&) noexcept; ~T() noexcept; };
1681 struct U { T t; }; // cxx11_20-note 3{{here}}
1682 static_assert(!__is_literal_type(Q), "");
1683 static_assert(!__is_literal_type(R), "");
1684 static_assert(!__is_literal_type(S), "");
1685 static_assert(!__is_literal_type(T), "");
1686 static_assert(!__is_literal_type(U), "");
1687 struct Test {
1688 friend Q::Q() noexcept; // expected-error {{follows constexpr}}
1689 friend Q::Q(Q&&) noexcept; // expected-error {{follows constexpr}}
1690 friend Q::Q(const Q&) noexcept; // expected-error {{follows constexpr}}
1691 friend S::S() noexcept; // expected-error {{follows constexpr}}
1692 friend S::S(S&&) noexcept; // expected-error {{follows constexpr}}
1693 friend S::S(const S&) noexcept; // expected-error {{follows constexpr}}
1694 friend constexpr U::U() noexcept; // cxx11_20-error {{follows non-constexpr}}
1695 friend constexpr U::U(U&&) noexcept; // cxx11_20-error {{follows non-constexpr}}
1696 friend constexpr U::U(const U&) noexcept; // cxx11_20-error {{follows non-constexpr}}
1697 };
1698 }
1699
1700 // Indirectly test that an implicit lvalue to xvalue conversion performed for
1701 // an NRVO move operation isn't implemented as CK_LValueToRValue.
1702 namespace PR12826 {
1703 struct Foo {};
1704 constexpr Foo id(Foo x) { return x; }
1705 constexpr Foo res(id(Foo()));
1706 }
1707
1708 namespace PR13273 {
1709 struct U {
1710 int t;
1711 U() = default;
1712 };
1713
1714 struct S : U {
1715 S() = default;
1716 };
1717
1718 // S's default constructor isn't constexpr, because U's default constructor
1719 // doesn't initialize 't', but it's trivial, so value-initialization doesn't
1720 // actually call it.
1721 static_assert(S{}.t == 0, "");
1722 }
1723
1724 namespace PR12670 {
1725 struct S {
1726 constexpr S(int a0) : m(a0) {}
1727 constexpr S() : m(6) {}
1728 int m;
1729 };
1730 constexpr S x[3] = { {4}, 5 };
1731 static_assert(x[0].m == 4, "");
1732 static_assert(x[1].m == 5, "");
1733 static_assert(x[2].m == 6, "");
1734 }
1735
1736 // Indirectly test that an implicit lvalue-to-rvalue conversion is performed
1737 // when a conditional operator has one argument of type void and where the other
1738 // is a glvalue of class type.
1739 namespace ConditionalLValToRVal {
1740 struct A {
1741 constexpr A(int a) : v(a) {}
1742 int v;
1743 };
1744
1745 constexpr A f(const A &a) {
1746 return a.v == 0 ? throw a : a;
1747 }
1748
1749 constexpr A a(4);
1750 static_assert(f(a).v == 4, "");
1751 }
1752
1753 namespace TLS {
1754 __thread int n;
1755 int m;
1756
1757 constexpr bool b = &n == &n;
1758
1759 constexpr int *p = &n; // expected-error{{constexpr variable 'p' must be initialized by a constant expression}}
1760
1761 constexpr int *f() { return &n; }
1762 constexpr int *q = f(); // expected-error{{constexpr variable 'q' must be initialized by a constant expression}}
1763 constexpr bool c = f() == f();
1764
1765 constexpr int *g() { return &m; }
1766 constexpr int *r = g();
1767 }
1768
1769 namespace Void {
1770 constexpr void f() { return; } // cxx11-error{{constexpr function's return type 'void' is not a literal type}}
1771
1772 void assert_failed(const char *msg, const char *file, int line); // expected-note {{declared here}}
1773 #define ASSERT(expr) ((expr) ? static_cast<void>(0) : assert_failed(#expr, __FILE__, __LINE__))
1774 template<typename T, size_t S>
1775 constexpr T get(T (&a)[S], size_t k) {
1776 return ASSERT(k > 0 && k < S), a[k]; // expected-note{{non-constexpr function 'assert_failed'}}
1777 }
1778 #undef ASSERT
1779 template int get(int (&a)[4], size_t);
1780 constexpr int arr[] = { 4, 1, 2, 3, 4 };
1781 static_assert(get(arr, 1) == 1, "");
1782 static_assert(get(arr, 4) == 4, "");
1783 static_assert(get(arr, 0) == 4, ""); // expected-error{{not an integral constant expression}} \
1784 // expected-note{{in call to 'get<const int, 5UL>(arr, 0)'}}
1785 }
1786
1787 namespace std { struct type_info; }
1788
1789 namespace TypeId {
1790 struct A { virtual ~A(); };
1791 A f();
1792 A &g(); // cxx20_23-note {{declared here}}
1793 constexpr auto &x = typeid(f());
1794 constexpr auto &y = typeid(g()); // expected-error{{constant expression}}
1795 // cxx11-note@-1 {{typeid applied to expression of polymorphic type 'A' is not allowed in a constant expression}}
1796 // expected-warning@-2 {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}
1797 // cxx20_23-note@-3 {{non-constexpr function 'g' cannot be used in a constant expression}}
1798 }
1799
1800 namespace PR14203 {
1801 struct duration {
1802 constexpr duration() {}
1803 constexpr operator int() const { return 0; }
1804 };
1805 // These are valid per P0859R0 (moved as DR).
1806 template<typename T> void f() {
1807 constexpr duration d = duration();
1808 }
1809 int n = sizeof(short{duration(duration())});
1810 }
1811
1812 namespace ArrayEltInit {
1813 struct A {
1814 constexpr A() : p(&p) {}
1815 void *p;
1816 };
1817 constexpr A a[10];
1818 static_assert(a[0].p == &a[0].p, "");
1819 static_assert(a[9].p == &a[9].p, "");
1820 static_assert(a[0].p != &a[9].p, "");
1821 static_assert(a[9].p != &a[0].p, "");
1822
1823 constexpr A b[10] = {};
1824 static_assert(b[0].p == &b[0].p, "");
1825 static_assert(b[9].p == &b[9].p, "");
1826 static_assert(b[0].p != &b[9].p, "");
1827 static_assert(b[9].p != &b[0].p, "");
1828 }
1829
1830 namespace PR15884 {
1831 struct S {};
1832 constexpr S f() { return {}; }
1833 constexpr S *p = &f();
1834 // expected-error@-1 {{taking the address of a temporary}}
1835 // expected-error@-2 {{constexpr variable 'p' must be initialized by a constant expression}}
1836 // expected-note@-3 {{pointer to temporary is not a constant expression}}
1837 // expected-note@-4 {{temporary created here}}
1838 }
1839
1840 namespace AfterError {
1841 constexpr int error() {
1842 return foobar; // expected-error {{undeclared identifier}}
1843 }
1844 constexpr int k = error(); // expected-error {{constexpr variable 'k' must be initialized by a constant expression}}
1845 }
1846
1847 namespace std {
1848 typedef decltype(sizeof(int)) size_t;
1849
1850 template <class _E>
1851 class initializer_list
1852 {
1853 const _E* __begin_;
1854 size_t __size_;
1855
1856 constexpr initializer_list(const _E* __b, size_t __s)
1857 : __begin_(__b),
1858 __size_(__s)
1859 {}
1860
1861 public:
1862 typedef _E value_type;
1863 typedef const _E& reference;
1864 typedef const _E& const_reference;
1865 typedef size_t size_type;
1866
1867 typedef const _E* iterator;
1868 typedef const _E* const_iterator;
1869
1870 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
1871
1872 constexpr size_t size() const {return __size_;}
1873 constexpr const _E* begin() const {return __begin_;}
1874 constexpr const _E* end() const {return __begin_ + __size_;}
1875 };
1876 }
1877
1878 namespace InitializerList {
1879 constexpr int sum(const int *b, const int *e) {
1880 return b != e ? *b + sum(b+1, e) : 0;
1881 }
1882 constexpr int sum(std::initializer_list<int> ints) {
1883 return sum(ints.begin(), ints.end());
1884 }
1885 static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
1886
1887 static_assert(*std::initializer_list<int>{1, 2, 3}.begin() == 1, "");
1888 static_assert(std::initializer_list<int>{1, 2, 3}.begin()[2] == 3, "");
1889
1890 namespace DR2126 {
1891 constexpr std::initializer_list<float> il = {1.0, 2.0, 3.0};
1892 static_assert(il.begin()[1] == 2.0, "");
1893 }
1894 }
1895
1896 namespace StmtExpr {
1897 struct A { int k; };
1898 void f() {
1899 static_assert(({ const int x = 5; x * 3; }) == 15, ""); // expected-warning {{extension}}
1900 constexpr auto a = ({ A(); }); // expected-warning {{extension}}
1901 }
1902 constexpr int g(int k) {
1903 return ({ // expected-warning {{extension}}
1904 const int x = k;
1905 x * x;
1906 });
1907 }
1908 static_assert(g(123) == 15129, "");
1909 constexpr int h() { // cxx11_20-error {{never produces a constant}}
1910 return ({ // expected-warning {{extension}}
1911 return 0; // cxx11_20-note {{not supported}}
1912 1;
1913 });
1914 }
1915 }
1916
1917 namespace VirtualFromBase {
1918 struct S1 {
1919 virtual int f() const;
1920 };
1921 struct S2 {
1922 virtual int f();
1923 };
1924 template <typename T> struct X : T {
1925 constexpr X() {}
1926 double d = 0.0;
1927 constexpr int f() { return sizeof(T); } // cxx11-warning {{will not be implicitly 'const' in C++14}}
1928 };
1929
1930 // Virtual f(), not OK.
1931 constexpr X<X<S1>> xxs1;
1932 constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
1933 static_assert(p->f() == sizeof(X<S1>), "");
1934 // cxx11-error@-1 {{not an integral constant expression}}
1935 // cxx11-note@-2 {{call to virtual function}}
1936 // cxx20_23-error@-3 {{static assertion failed}}
1937 // cxx20_23-note@-4 {{8 == 16}}
1938
1939 // Non-virtual f(), OK.
1940 constexpr X<X<S2>> xxs2;
1941 constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
1942 static_assert(q->f() == sizeof(S2), ""); // cxx20_23-error {{static assertion failed}} \
1943 // cxx20_23-note {{16 == 8}}
1944 }
1945
1946 namespace ConstexprConstructorRecovery {
1947 class X {
1948 public:
1949 enum E : short {
1950 headers = 0x1,
1951 middlefile = 0x2,
1952 choices = 0x4
1953 };
1954 constexpr X() noexcept {};
1955 protected:
1956 E val{0}; // cxx11-error {{cannot initialize a member subobject of type 'E' with an rvalue of type 'int'}} cxx11-note {{here}}
1957 };
1958 // FIXME: We should avoid issuing this follow-on diagnostic.
1959 constexpr X x{}; // cxx11-error {{constant expression}} cxx11-note {{not initialized}}
1960 }
1961
1962 namespace Lifetime {
1963 void f() {
1964 constexpr int &n = n; // expected-error {{constant expression}} expected-note {{use of reference outside its lifetime}} expected-warning {{not yet bound to a value}}
1965 constexpr int m = m; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
1966 }
1967
1968 constexpr int &get(int &&n) { return n; }
1969 // cxx23-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
1970 constexpr int &&get_rv(int &&n) { return static_cast<int&&>(n); }
1971 struct S {
1972 int &&r;
1973 int &s;
1974 int t;
1975 constexpr S() : r(get_rv(0)), s(get(0)), t(r) {} // cxx11_20-note {{read of object outside its lifetime}}
1976 constexpr S(int) : r(get_rv(0)), s(get(0)), t(s) {} // cxx11_20-note {{read of object outside its lifetime}}
1977 };
1978 constexpr int k1 = S().t; // expected-error {{constant expression}} cxx11_20-note {{in call}}
1979 constexpr int k2 = S(0).t; // expected-error {{constant expression}} cxx11_20-note {{in call}}
1980
1981 struct Q {
1982 int n = 0;
1983 constexpr int f() const { return 0; }
1984 };
1985 constexpr Q *out_of_lifetime(Q q) { return &q; } // expected-warning {{address of stack}}
1986 constexpr int k3 = out_of_lifetime({})->n; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
1987 constexpr int k4 = out_of_lifetime({})->f(); // expected-error {{constant expression}} expected-note {{member call on object outside its lifetime}}
1988
1989 constexpr int null = ((Q*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced null pointer}}
1990
1991 Q q;
1992 Q qa[3];
1993 constexpr int pte0 = (&q)[0].f(); // ok
1994 constexpr int pte1 = (&q)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
1995 constexpr int pte2 = qa[2].f(); // ok
1996 constexpr int pte3 = qa[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
1997
1998 constexpr Q cq;
1999 constexpr Q cqa[3];
2000 constexpr int cpte0 = (&cq)[0].f(); // ok
2001 constexpr int cpte1 = (&cq)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
2002 constexpr int cpte2 = cqa[2].f(); // ok
2003 constexpr int cpte3 = cqa[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
2004
2005 // FIXME: There's no way if we can tell if the first call here is valid; it
2006 // depends on the active union member. Should we reject for that reason?
2007 union U {
2008 int n;
2009 Q q;
2010 };
2011 U u1 = {0};
2012 constexpr U u2 = {0};
2013 constexpr int union_member1 = u1.q.f();
2014 constexpr int union_member2 = u2.q.f(); // expected-error {{constant expression}} expected-note {{member call on member 'q' of union with active member 'n'}}
2015
2016 struct R { // expected-note {{field init}}
2017 struct Inner { constexpr int f() const { return 0; } };
2018 int a = b.f(); // expected-warning {{uninitialized}} expected-note 2{{member call on object outside its lifetime}}
2019 Inner b;
2020 };
2021 constexpr R r; // expected-error {{constant expression}} expected-note {{in call}} expected-note {{implicit default constructor for 'Lifetime::R' first required here}}
2022 void rf() {
2023 constexpr R r; // expected-error {{constant expression}} expected-note {{in call}}
2024 }
2025 }
2026
2027 namespace Bitfields {
2028 struct A {
2029 bool b : 1;
2030 unsigned u : 5;
2031 int n : 5;
2032 bool b2 : 3;
2033 unsigned u2 : 74; // expected-warning {{exceeds the width of its type}}
2034 int n2 : 81; // expected-warning {{exceeds the width of its type}}
2035 };
2036
2037 constexpr A a = { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}}
2038 static_assert(a.b == 0 && a.u == 1 && a.n == -1 && a.b2 == 0 &&
2039 a.u2 + 1 == 0 && a.n2 == 0x7fffffff,
2040 "bad truncation of bitfield values");
2041
2042 struct B {
2043 int n : 3;
2044 constexpr B(int k) : n(k) {}
2045 };
2046 static_assert(B(3).n == 3, "");
2047 static_assert(B(4).n == -4, "");
2048 static_assert(B(7).n == -1, "");
2049 static_assert(B(8).n == 0, "");
2050 static_assert(B(-1).n == -1, "");
2051 static_assert(B(-8889).n == -1, "");
2052
2053 namespace PR16755 {
2054 struct X {
2055 int x : 1;
2056 constexpr static int f(int x) {
2057 return X{x}.x;
2058 }
2059 };
2060 static_assert(X::f(3) == -1, "3 should truncate to -1");
2061 static_assert(X::f(1) == -1, "1 should truncate to -1");
2062 }
2063
2064 struct HasUnnamedBitfield {
2065 unsigned a;
2066 unsigned : 20;
2067 unsigned b;
2068
2069 constexpr HasUnnamedBitfield() : a(), b() {}
2070 constexpr HasUnnamedBitfield(unsigned a, unsigned b) : a(a), b(b) {}
2071 };
2072
2073 void testUnnamedBitfield() {
2074 const HasUnnamedBitfield zero{};
2075 int a = 1 / zero.b; // expected-warning {{division by zero is undefined}}
2076 const HasUnnamedBitfield oneZero{1, 0};
2077 int b = 1 / oneZero.b; // expected-warning {{division by zero is undefined}}
2078 }
2079
2080 union UnionWithUnnamedBitfield {
2081 int : 3;
2082 int n;
2083 };
2084 static_assert(UnionWithUnnamedBitfield().n == 0, "");
2085 static_assert(UnionWithUnnamedBitfield{}.n == 0, "");
2086 static_assert(UnionWithUnnamedBitfield{1}.n == 1, "");
2087 }
2088
2089 namespace ZeroSizeTypes {
2090 constexpr int (*p1)[0] = 0, (*p2)[0] = 0;
2091 constexpr int k = p2 - p1;
2092 // expected-error@-1 {{constexpr variable 'k' must be initialized by a constant expression}}
2093 // expected-note@-2 {{subtraction of pointers to type 'int[0]' of zero size}}
2094
2095 int arr[5][0];
2096 constexpr int f() { // cxx11_20-error {{never produces a constant expression}}
2097 return &arr[3] - &arr[0]; // cxx11_20-note {{subtraction of pointers to type 'int[0]' of zero size}}
2098 }
2099 }
2100
2101 namespace BadDefaultInit {
2102 template<int N> struct X { static const int n = N; };
2103
2104 struct A { // expected-error {{default member initializer for 'k' needed within definition of enclosing class}}
2105 int k = // expected-note {{default member initializer declared here}}
2106 X<A().k>::n; // expected-note {{in evaluation of exception specification for 'BadDefaultInit::A::A' needed here}}
2107 };
2108
2109 struct B {
2110 constexpr B(
2111 int k = X<B().k>::n) : // expected-error {{default argument to function 'B' that is declared later}} expected-note {{here}}
2112 k(k) {}
2113 int k;
2114 };
2115 }
2116
2117 namespace NeverConstantTwoWays {
2118 // If we see something non-constant but foldable followed by something
2119 // non-constant and not foldable, we want the first diagnostic, not the
2120 // second.
2121 constexpr int f(int n) { // cxx11_20-error {{never produces a constant expression}}
2122 return (int *)(long)&n == &n ? // cxx11_20-note {{reinterpret_cast}}
2123 1 / 0 : // expected-warning {{division by zero}}
2124 0;
2125 }
2126
2127 constexpr int n = // expected-error {{must be initialized by a constant expression}}
2128 (int *)(long)&n == &n ? // expected-note {{reinterpret_cast}}
2129 1 / 0 :
2130 0;
2131 }
2132
2133 namespace PR17800 {
2134 struct A {
2135 constexpr int operator()() const { return 0; }
2136 };
2137 template <typename ...T> constexpr int sink(T ...) {
2138 return 0;
2139 }
2140 template <int ...N> constexpr int run() {
2141 return sink(A()() + N ...);
2142 }
2143 constexpr int k = run<1, 2, 3>();
2144 }
2145
2146 namespace BuiltinStrlen {
2147 constexpr const char *a = "foo\0quux";
2148 constexpr char b[] = "foo\0quux";
2149 constexpr int f() { return 'u'; }
2150 constexpr char c[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 };
2151
2152 static_assert(__builtin_strlen("foo") == 3, "");
2153 static_assert(__builtin_strlen("foo\0quux") == 3, "");
2154 static_assert(__builtin_strlen("foo\0quux" + 4) == 4, "");
2155
2156 constexpr bool check(const char *p) {
2157 return __builtin_strlen(p) == 3 &&
2158 __builtin_strlen(p + 1) == 2 &&
2159 __builtin_strlen(p + 2) == 1 &&
2160 __builtin_strlen(p + 3) == 0 &&
2161 __builtin_strlen(p + 4) == 4 &&
2162 __builtin_strlen(p + 5) == 3 &&
2163 __builtin_strlen(p + 6) == 2 &&
2164 __builtin_strlen(p + 7) == 1 &&
2165 __builtin_strlen(p + 8) == 0;
2166 }
2167
2168 static_assert(check(a), "");
2169 static_assert(check(b), "");
2170 static_assert(check(c), "");
2171
2172 constexpr int over1 = __builtin_strlen(a + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2173 constexpr int over2 = __builtin_strlen(b + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2174 constexpr int over3 = __builtin_strlen(c + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2175
2176 constexpr int under1 = __builtin_strlen(a - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2177 constexpr int under2 = __builtin_strlen(b - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2178 constexpr int under3 = __builtin_strlen(c - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2179
2180 // FIXME: The diagnostic here could be better.
2181 constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator.
2182 constexpr int bad = __builtin_strlen(d); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2183 }
2184
2185 namespace PR19010 {
2186 struct Empty {};
2187 struct Empty2 : Empty {};
2188 struct Test : Empty2 {
2189 constexpr Test() {}
2190 Empty2 array[2];
2191 };
2192 void test() { constexpr Test t; }
2193 }
2194
2195 void PR21327(int a, int b) {
2196 static_assert(&a + 1 != &b, ""); // expected-error {{constant expression}}
2197 // expected-note@-1 {{comparison against pointer '&a + 1' that points past the end of a complete object has unspecified value}}
2198 }
2199
2200 namespace EmptyClass {
2201 struct E1 {} e1;
2202 union E2 {} e2; // expected-note {{here}}
2203 struct E3 : E1 {} e3;
2204
2205 // The defaulted copy constructor for an empty class does not read any
2206 // members. The defaulted copy constructor for an empty union reads the
2207 // object representation.
2208 constexpr E1 e1b(e1);
2209 constexpr E2 e2b(e2); // expected-error {{constant expression}} expected-note{{read of non-const}} expected-note {{in call}}
2210 constexpr E3 e3b(e3);
2211 }
2212
2213 namespace PR21786 {
2214 extern void (*start[])();
2215 extern void (*end[])();
2216 static_assert(&start != &end, ""); // expected-error {{constant expression}}
2217 // expected-note@-1 {{comparison of pointers '&start' and '&end' to unrelated zero-sized objects}}
2218 static_assert(&start != nullptr, "");
2219
2220 struct Foo;
2221 struct Bar {
2222 static const Foo x;
2223 static const Foo y;
2224 };
2225 static_assert(&Bar::x != nullptr, "");
2226 static_assert(&Bar::x != &Bar::y, "");
2227 }
2228
2229 namespace PR21859 {
2230 constexpr int Fun() { return; } // expected-error {{non-void constexpr function 'Fun' should return a value}}
2231 constexpr int Var = Fun();
2232
2233 template <typename T> constexpr int FunT1() { return; } // expected-error {{non-void constexpr function 'FunT1' should return a value}}
2234 template <typename T> constexpr int FunT2() { return 0; }
2235 template <> constexpr int FunT2<double>() { return 0; }
2236 template <> constexpr int FunT2<int>() { return; } // expected-error {{non-void constexpr function 'FunT2<int>' should return a value}}
2237 }
2238
2239 struct InvalidRedef {
2240 int f; // expected-note{{previous definition is here}}
2241 constexpr int f(void); // expected-error{{redefinition of 'f'}} cxx11-warning{{will not be implicitly 'const'}}
2242 };
2243
2244 namespace PR17938 {
2245 template <typename T> constexpr T const &f(T const &x) { return x; }
2246
2247 struct X {};
2248 struct Y : X {};
2249 struct Z : Y { constexpr Z() {} };
2250
2251 static constexpr auto z = f(Z());
2252 }
2253
2254 namespace PR24597 {
2255 struct A {
2256 int x, *p;
2257 constexpr A() : x(0), p(&x) {}
2258 constexpr A(const A &a) : x(a.x), p(&x) {}
2259 };
2260 constexpr A f() { return A(); }
2261 constexpr A g() { return f(); }
2262 constexpr int a = *f().p;
2263 constexpr int b = *g().p;
2264 }
2265
2266 namespace IncompleteClass {
2267 struct XX {
2268 static constexpr int f(XX*) { return 1; } // expected-note {{here}}
2269 friend constexpr int g(XX*) { return 2; } // expected-note {{here}}
2270
2271 static constexpr int i = f(static_cast<XX*>(nullptr)); // expected-error {{constexpr variable 'i' must be initialized by a constant expression}} expected-note {{undefined function 'f' cannot be used in a constant expression}}
2272 static constexpr int j = g(static_cast<XX*>(nullptr)); // expected-error {{constexpr variable 'j' must be initialized by a constant expression}} expected-note {{undefined function 'g' cannot be used in a constant expression}}
2273 };
2274 }
2275
2276 namespace InheritedCtor {
2277 struct A { constexpr A(int) {} };
2278
2279 struct B : A { int n; using A::A; }; // expected-note {{here}}
2280 constexpr B b(0); // expected-error {{constant expression}} cxx11_20-note {{derived class}}\
2281 // cxx23-note {{not initialized}}
2282
2283 struct C : A { using A::A; struct { union { int n, m = 0; }; union { int a = 0; }; int k = 0; }; struct {}; union {}; }; // expected-warning 6{{}}
2284 constexpr C c(0);
2285
2286 struct D : A {
2287 using A::A; // cxx11-note {{here}}
2288 struct { // expected-warning {{extension}}
2289 union { // expected-warning {{extension}}
2290 int n;
2291 };
2292 };
2293 };
2294 constexpr D d(0); // cxx11-error {{constant expression}} cxx11-note {{derived class}}
2295
2296 struct E : virtual A { using A::A; }; // expected-note {{here}}
2297 // cxx20_23-note@-1 {{struct with virtual base class is not a literal type}}
2298 // We wrap a function around this to avoid implicit zero-initialization
2299 // happening first; the zero-initialization step would produce the same
2300 // error and defeat the point of this test.
2301 void f() {
2302 constexpr E e(0); // cxx11-error {{constant expression}} cxx11-note {{derived class}}
2303 // cxx20_23-error@-1 {{constexpr variable cannot have non-literal type}}
2304 }
2305 // FIXME: This produces a note with no source location.
2306 //constexpr E e(0);
2307
2308 struct W { constexpr W(int n) : w(n) {} int w; };
2309 struct X : W { using W::W; int x = 2; };
2310 struct Y : X { using X::X; int y = 3; };
2311 struct Z : Y { using Y::Y; int z = 4; };
2312 constexpr Z z(1);
2313 static_assert(z.w == 1 && z.x == 2 && z.y == 3 && z.z == 4, "");
2314 }
2315
2316
2317 namespace PR28366 {
2318 namespace ns1 {
2319
2320 void f(char c) { //expected-note{{declared here}}
2321 //cxx11_20-note@-1{{declared here}}
2322 struct X {
2323 static constexpr char f() { // cxx11_20-error {{never produces a constant expression}}
2324 return c; //expected-error{{reference to local}} cxx11_20-note{{function parameter}}
2325 }
2326 };
2327 int I = X::f();
2328 }
2329
2330 void g() {
2331 const int c = 'c';
2332 static const int d = 'd';
2333 struct X {
2334 static constexpr int f() {
2335 return c + d;
2336 }
2337 };
2338 static_assert(X::f() == 'c' + 'd',"");
2339 }
2340
2341
2342 } // end ns1
2343
2344 } //end ns PR28366
2345
2346 namespace PointerArithmeticOverflow {
2347 int n;
2348 int a[1];
2349 constexpr int *b = &n + 1 + (long)-1;
2350 constexpr int *c = &n + 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}}
2351 constexpr int *d = &n + 1 - (unsigned long)1;
2352 constexpr int *e = a + 1 + (long)-1;
2353 constexpr int *f = a + 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}}
2354 constexpr int *g = a + 1 - (unsigned long)1;
2355
2356 constexpr int *p = (&n + 1) + (unsigned __int128)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}}
2357 constexpr int *q = (&n + 1) - (unsigned __int128)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element -3402}}
2358 constexpr int *r = &(&n + 1)[(unsigned __int128)-1]; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}}
2359 }
2360
2361 namespace PR40430 {
2362 struct S {
2363 char c[10] = "asdf";
2364 constexpr char foo() const { return c[3]; }
2365 };
2366 static_assert(S().foo() == 'f', "");
2367 }
2368
2369 namespace PR41854 {
2370 struct e { operator int(); };
2371 struct f { e c; };
2372 int a;
2373 f &d = reinterpret_cast<f&>(a);
2374 unsigned b = d.c;
2375 }
2376
2377 namespace array_size {
2378 template<int N> struct array {
2379 static constexpr int size() { return N; }
2380 };
2381 template<typename T> void f1(T t) {
2382 constexpr int k = t.size();
2383 }
2384 template<typename T> void f2(const T &t) { // expected-note 2{{declared here}}
2385 constexpr int k = t.size(); // expected-error 2{{constant}} expected-note 2{{function parameter 't' with unknown value cannot be used in a constant expression}}
2386 }
2387 template<typename T> void f3(const T &t) {
2388 constexpr int k = T::size();
2389 }
2390 void g(array<3> a) {
2391 f1(a);
2392 f2(a); // expected-note {{instantiation of}}
2393 f3(a);
2394 }
2395
2396 template<int N> struct array_nonstatic {
2397 constexpr int size() const { return N; }
2398 };
2399 void h(array_nonstatic<3> a) {
2400 f1(a);
2401 f2(a); // expected-note {{instantiation of}}
2402 }
2403 }
2404
2405 namespace flexible_array {
2406 struct A { int x; char arr[]; }; // expected-warning {{C99}} expected-note {{here}}
2407 constexpr A a = {1};
2408 static_assert(a.x == 1, "");
2409 static_assert(&a.arr != nullptr, "");
2410 static_assert(a.arr[0], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2411 static_assert(a.arr[1], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2412
2413 constexpr A b[] = {{1}, {2}, {3}}; // expected-warning {{flexible array member}}
2414 static_assert(b[0].x == 1, "");
2415 static_assert(b[1].x == 2, "");
2416 static_assert(b[2].x == 3, "");
2417 static_assert(b[2].arr[0], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2418
2419 // Flexible array initialization is currently not supported by constant
2420 // evaluation. Make sure we emit an error message, for now.
2421 constexpr A c = {1, 2, 3}; // expected-error {{constexpr variable 'c' must be initialized by a constant expression}}
2422 // expected-note@-1 {{flexible array initialization is not yet supported}}
2423 // expected-warning@-2 {{flexible array initialization is a GNU extension}}
2424 }
2425
2426 void local_constexpr_var() {
2427 constexpr int a = 0; // expected-note {{address of non-static constexpr variable 'a' may differ on each invocation of the enclosing function; add 'static' to give it a constant address}}
2428 constexpr const int *p = &a; // expected-error {{constant expression}} expected-note {{pointer to 'a' is not a constant expression}}
2429 }
2430
2431 namespace GH50055 {
2432 // Enums without fixed underlying type
2433 enum E1 {e11=-4, e12=4};
2434 enum E2 {e21=0, e22=4};
2435 enum E3 {e31=-4, e32=1024};
2436 enum E4 {e41=0};
2437 // Empty but as-if it had a single enumerator with value 0
2438 enum EEmpty {};
2439
2440 // Enum with fixed underlying type because the underlying type is explicitly specified
2441 enum EFixed : int {efixed1=-4, efixed2=4};
2442 // Enum with fixed underlying type because it is scoped
2443 enum class EScoped {escoped1=-4, escoped2=4};
2444
2445 enum EMaxInt {emaxint1=-1, emaxint2=__INT_MAX__};
2446
2447 enum NumberType {};
2448
2449 E2 testDefaultArgForParam(E2 e2Param = (E2)-1) { // ok, not a constant expression context
2450 E2 e2LocalInit = e2Param; // ok, not a constant expression context
2451 return e2LocalInit;
2452 }
2453
2454 #include <enum-constexpr-conversion-system-header.h>
2455
2456 void testValueInRangeOfEnumerationValues() {
2457 constexpr E1 x1 = static_cast<E1>(-8);
2458 constexpr E1 x2 = static_cast<E1>(8);
2459 // expected-error@-1 {{integer value 8 is outside the valid range of values [-8, 7] for the enumeration type 'E1'}}
2460 E1 x2b = static_cast<E1>(8); // ok, not a constant expression context
2461
2462 constexpr E2 x3 = static_cast<E2>(-8);
2463 // expected-error@-1 {{integer value -8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
2464 constexpr E2 x4 = static_cast<E2>(0);
2465 constexpr E2 x5 = static_cast<E2>(8);
2466 // expected-error@-1 {{integer value 8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
2467
2468 constexpr E3 x6 = static_cast<E3>(-2048);
2469 constexpr E3 x7 = static_cast<E3>(-8);
2470 constexpr E3 x8 = static_cast<E3>(0);
2471 constexpr E3 x9 = static_cast<E3>(8);
2472 constexpr E3 x10 = static_cast<E3>(2048);
2473 // expected-error@-1 {{integer value 2048 is outside the valid range of values [-2048, 2047] for the enumeration type 'E3'}}
2474
2475 constexpr E4 x11 = static_cast<E4>(0);
2476 constexpr E4 x12 = static_cast<E4>(1);
2477 constexpr E4 x13 = static_cast<E4>(2);
2478 // expected-error@-1 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'E4'}}
2479
2480 constexpr EEmpty x14 = static_cast<EEmpty>(0);
2481 constexpr EEmpty x15 = static_cast<EEmpty>(1);
2482 constexpr EEmpty x16 = static_cast<EEmpty>(2);
2483 // expected-error@-1 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'EEmpty'}}
2484
2485 constexpr EFixed x17 = static_cast<EFixed>(100);
2486 constexpr EScoped x18 = static_cast<EScoped>(100);
2487
2488 constexpr EMaxInt x19 = static_cast<EMaxInt>(__INT_MAX__-1);
2489 constexpr EMaxInt x20 = static_cast<EMaxInt>((long)__INT_MAX__+1);
2490 // expected-error@-1 {{integer value 2147483648 is outside the valid range of values [-2147483648, 2147483647] for the enumeration type 'EMaxInt'}}
2491
2492 const NumberType neg_one = (NumberType) ((NumberType) 0 - (NumberType) 1); // ok, not a constant expression context
2493
2494 CONSTEXPR_CAST_TO_SYSTEM_ENUM_OUTSIDE_OF_RANGE;
2495 // expected-error@-1 {{integer value 123 is outside the valid range of values [0, 1] for the enumeration type 'SystemEnum'}}
2496 }
2497
2498 template<class T, unsigned size> struct Bitfield {
2499 static constexpr T max = static_cast<T>((1 << size) - 1); // #enum
2500 };
2501
2502 void testValueInRangeOfEnumerationValuesViaTemplate() {
2503 Bitfield<E2, 3> good;
2504 Bitfield<E2, 4> bad; // cxx11-error@#enum {{integer value 15 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
2505 }
2506
2507 enum SortOrder {
2508 AscendingOrder,
2509 DescendingOrder
2510 };
2511
2512 class A {
2513 static void f(SortOrder order);
2514 };
2515
2516 void A::f(SortOrder order) {
2517 if (order == SortOrder(-1)) // ok, not a constant expression context
2518 return;
2519 }
2520 }
2521
2522 GH50055::E2 GlobalInitNotCE1 = (GH50055::E2)-1; // ok, not a constant expression context
2523 GH50055::E2 GlobalInitNotCE2 = GH50055::testDefaultArgForParam(); // ok, not a constant expression context
2524 constexpr GH50055::E2 GlobalInitCE = (GH50055::E2)-1;
2525 // expected-error@-1 {{integer value -1 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}