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