1 // RUN: %clang_cc1 -triple i686-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -fsyntax-only -fcxx-exceptions -verify -std=c++11 -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
2 
3 namespace StaticAssertFoldTest {
4 
5 int x;
6 static_assert(++x, "test"); // expected-error {{not an integral constant expression}}
7 static_assert(false, "test"); // expected-error {{test}}
8 
9 }
10 
11 typedef decltype(sizeof(char)) size_t;
12 
id(const T & t)13 template<typename T> constexpr T id(const T &t) { return t; }
min(const T & a,const T & b)14 template<typename T> constexpr T min(const T &a, const T &b) {
15   return a < b ? a : b;
16 }
max(const T & a,const T & b)17 template<typename T> constexpr T max(const T &a, const T &b) {
18   return a < b ? b : a;
19 }
begin(T (& xs)[N])20 template<typename T, size_t N> constexpr T *begin(T (&xs)[N]) { return xs; }
end(T (& xs)[N])21 template<typename T, size_t N> constexpr T *end(T (&xs)[N]) { return xs + N; }
22 
23 struct MemberZero {
zeroMemberZero24   constexpr int zero() const { return 0; }
25 };
26 
27 namespace DerivedToVBaseCast {
28 
29   struct U { int n; };
30   struct V : U { int n; };
31   struct A : virtual V { int n; };
32   struct Aa { int n; };
33   struct B : virtual A, Aa {};
34   struct C : virtual A, Aa {};
35   struct D : B, C {};
36 
37   D d;
38   constexpr B *p = &d;
39   constexpr C *q = &d;
40 
41   static_assert((void*)p != (void*)q, "");
42   static_assert((A*)p == (A*)q, "");
43   static_assert((Aa*)p != (Aa*)q, "");
44 
45   constexpr B &pp = d;
46   constexpr C &qq = d;
47   static_assert((void*)&pp != (void*)&qq, "");
48   static_assert(&(A&)pp == &(A&)qq, "");
49   static_assert(&(Aa&)pp != &(Aa&)qq, "");
50 
51   constexpr V *v = p;
52   constexpr V *w = q;
53   constexpr V *x = (A*)p;
54   static_assert(v == w, "");
55   static_assert(v == x, "");
56 
57   static_assert((U*)&d == p, "");
58   static_assert((U*)&d == q, "");
59   static_assert((U*)&d == v, "");
60   static_assert((U*)&d == w, "");
61   static_assert((U*)&d == x, "");
62 
63   struct X {};
64   struct Y1 : virtual X {};
65   struct Y2 : X {};
66   struct Z : Y1, Y2 {};
67   Z z;
68   static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
69 }
70 
71 namespace ConstCast {
72 
73 constexpr int n1 = 0;
74 constexpr int n2 = const_cast<int&>(n1);
75 constexpr int *n3 = const_cast<int*>(&n1);
76 constexpr int n4 = *const_cast<int*>(&n1);
77 constexpr const int * const *n5 = const_cast<const int* const*>(&n3);
78 constexpr int **n6 = const_cast<int**>(&n3);
79 constexpr int n7 = **n5;
80 constexpr int n8 = **n6;
81 
82 // const_cast from prvalue to xvalue.
83 struct A { int n; };
84 constexpr int n9 = (const_cast<A&&>(A{123})).n;
85 static_assert(n9 == 123, "");
86 
87 }
88 
89 namespace TemplateArgumentConversion {
90   template<int n> struct IntParam {};
91 
92   using IntParam0 = IntParam<0>;
93   using IntParam0 = IntParam<id(0)>;
94   using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}}
95 }
96 
97 namespace CaseStatements {
98   int x;
f(int n)99   void f(int n) {
100     switch (n) {
101     case MemberZero().zero: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}}
102     case id(0): // expected-error {{duplicate case value '0'}}
103       return;
104     case __builtin_constant_p(true) ? (__SIZE_TYPE__)&x : 0:; // expected-error {{constant}}
105     }
106   }
107 }
108 
109 extern int &Recurse1;
110 int &Recurse2 = Recurse1; // expected-note {{declared here}}
111 int &Recurse1 = Recurse2;
112 constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
113 
114 extern const int RecurseA;
115 const int RecurseB = RecurseA; // expected-note {{declared here}}
116 const int RecurseA = 10;
117 constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
118 
119 namespace MemberEnum {
120   struct WithMemberEnum {
121     enum E { A = 42 };
122   } wme;
123 
124   static_assert(wme.A == 42, "");
125 }
126 
127 namespace DefaultArguments {
128 
129 const int z = int();
Sum(int a=0,const int & b=0,const int * c=& z,char d=0)130 constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
131   return a + b + *c + d;
132 }
133 const int four = 4;
134 constexpr int eight = 8;
135 constexpr const int twentyseven = 27;
136 static_assert(Sum() == 0, "");
137 static_assert(Sum(1) == 1, "");
138 static_assert(Sum(1, four) == 5, "");
139 static_assert(Sum(1, eight, &twentyseven) == 36, "");
140 static_assert(Sum(1, 2, &four, eight) == 15, "");
141 
142 }
143 
144 namespace Ellipsis {
145 
146 // Note, values passed through an ellipsis can't actually be used.
F(int a,...)147 constexpr int F(int a, ...) { return a; }
148 static_assert(F(0) == 0, "");
149 static_assert(F(1, 0) == 1, "");
150 static_assert(F(2, "test") == 2, "");
151 static_assert(F(3, &F) == 3, "");
152 int k = 0; // expected-note {{here}}
153 static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
154 
155 }
156 
157 namespace Recursion {
fib(int n)158   constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
159   static_assert(fib(11) == 89, "");
160 
gcd_inner(int a,int b)161   constexpr int gcd_inner(int a, int b) {
162     return b == 0 ? a : gcd_inner(b, a % b);
163   }
gcd(int a,int b)164   constexpr int gcd(int a, int b) {
165     return gcd_inner(max(a, b), min(a, b));
166   }
167 
168   static_assert(gcd(1749237, 5628959) == 7, "");
169 }
170 
171 namespace FunctionCast {
172   // When folding, we allow functions to be cast to different types. Such
173   // cast functions cannot be called, even if they're constexpr.
f()174   constexpr int f() { return 1; }
175   typedef double (*DoubleFn)();
176   typedef int (*IntFn)();
177   int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}}
178   int b[(int)IntFn(f)()];    // ok
179 }
180 
181 namespace StaticMemberFunction {
182   struct S {
183     static constexpr int k = 42;
fStaticMemberFunction::S184     static constexpr int f(int n) { return n * k + 2; }
185   } s;
186 
187   constexpr int n = s.f(19);
188   static_assert(S::f(19) == 800, "");
189   static_assert(s.f(19) == 800, "");
190   static_assert(n == 800, "");
191 
192   constexpr int (*sf1)(int) = &S::f;
193   constexpr int (*sf2)(int) = &s.f;
194   constexpr const int *sk = &s.k;
195 }
196 
197 namespace ParameterScopes {
198 
199   const int k = 42;
ObscureTheTruth(const int & a)200   constexpr const int &ObscureTheTruth(const int &a) { return a; }
MaybeReturnJunk(bool b,const int a)201   constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}}
202     return ObscureTheTruth(b ? a : k);
203   }
204   static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
205   constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}}
206 
MaybeReturnNonstaticRef(bool b,const int a)207   constexpr const int MaybeReturnNonstaticRef(bool b, const int a) {
208     return ObscureTheTruth(b ? a : k);
209   }
210   static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
211   constexpr int b = MaybeReturnNonstaticRef(true, 0); // ok
212 
InternalReturnJunk(int n)213   constexpr int InternalReturnJunk(int n) {
214     return MaybeReturnJunk(true, n); // expected-note {{read of variable whose lifetime has ended}}
215   }
216   constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
217 
LToR(int & n)218   constexpr int LToR(int &n) { return n; }
GrabCallersArgument(bool which,int a,int b)219   constexpr int GrabCallersArgument(bool which, int a, int b) {
220     return LToR(which ? b : a);
221   }
222   static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
223   static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
224 
225 }
226 
227 namespace Pointers {
228 
f(int n,const int * a,const int * b,const int * c)229   constexpr int f(int n, const int *a, const int *b, const int *c) {
230     return n == 0 ? 0 : *a + f(n-1, b, c, a);
231   }
232 
233   const int x = 1, y = 10, z = 100;
234   static_assert(f(23, &x, &y, &z) == 788, "");
235 
g(int n,int a,int b,int c)236   constexpr int g(int n, int a, int b, int c) {
237     return f(n, &a, &b, &c);
238   }
239   static_assert(g(23, x, y, z) == 788, "");
240 
241 }
242 
243 namespace FunctionPointers {
244 
Double(int n)245   constexpr int Double(int n) { return 2 * n; }
Triple(int n)246   constexpr int Triple(int n) { return 3 * n; }
Twice(int (* F)(int),int n)247   constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
Quadruple(int n)248   constexpr int Quadruple(int n) { return Twice(Double, n); }
Select(int n)249   constexpr auto Select(int n) -> int (*)(int) {
250     return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
251   }
Apply(int (* F)(int),int n)252   constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}}
253 
254   static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
255 
256   constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}}
257 
258 }
259 
260 namespace PointerComparison {
261 
262 int x, y;
263 static_assert(&x == &y, "false"); // expected-error {{false}}
264 static_assert(&x != &y, "");
265 constexpr bool g1 = &x == &y;
266 constexpr bool g2 = &x != &y;
267 constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
268 constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
269 constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
270 constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
271 
272 struct S { int x, y; } s;
273 static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
274 static_assert(&s.x != &s.y, "");
275 static_assert(&s.x <= &s.y, "");
276 static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
277 static_assert(&s.x < &s.y, "");
278 static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
279 
280 static_assert(0 == &y, "false"); // expected-error {{false}}
281 static_assert(0 != &y, "");
282 constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
283 constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
284 constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
285 constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
286 
287 static_assert(&x == 0, "false"); // expected-error {{false}}
288 static_assert(&x != 0, "");
289 constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
290 constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
291 constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
292 constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
293 
294 static_assert(&x == &x, "");
295 static_assert(&x != &x, "false"); // expected-error {{false}}
296 static_assert(&x <= &x, "");
297 static_assert(&x >= &x, "");
298 static_assert(&x < &x, "false"); // expected-error {{false}}
299 static_assert(&x > &x, "false"); // expected-error {{false}}
300 
301 constexpr S* sptr = &s;
302 constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
303 
304 struct U {};
305 struct Str {
306   int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
307     expected-warning {{not an integral constant expression}} \
308     expected-note {{dynamic_cast is not allowed in a constant expression}}
309   int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
310     expected-warning {{not an integral constant expression}} \
311     expected-note {{reinterpret_cast is not allowed in a constant expression}}
312   int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
313     expected-warning {{not an integral constant expression}} \
314     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
315   int d : (S*)(42) == (S*)(42); // \
316     expected-warning {{not an integral constant expression}} \
317     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
318   int e : (Str*)(sptr) == (Str*)(sptr); // \
319     expected-warning {{not an integral constant expression}} \
320     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
321   int f : &(U&)(*sptr) == &(U&)(*sptr); // \
322     expected-warning {{not an integral constant expression}} \
323     expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
324   int g : (S*)(void*)(sptr) == sptr; // \
325     expected-warning {{not an integral constant expression}} \
326     expected-note {{cast from 'void *' is not allowed in a constant expression}}
327 };
328 
329 extern char externalvar[];
330 constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
331 constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
332 static_assert(0 != "foo", "");
333 
334 }
335 
336 namespace MaterializeTemporary {
337 
f(const int & r)338 constexpr int f(const int &r) { return r; }
339 constexpr int n = f(1);
340 
same(const int & a,const int & b)341 constexpr bool same(const int &a, const int &b) { return &a == &b; }
sameTemporary(const int & n)342 constexpr bool sameTemporary(const int &n) { return same(n, n); }
343 
344 static_assert(n, "");
345 static_assert(!same(4, 4), "");
346 static_assert(same(n, n), "");
347 static_assert(sameTemporary(9), "");
348 
349 struct A { int &&r; };
350 struct B { A &&a1; A &&a2; };
351 
352 constexpr B b1 { { 1 }, { 2 } }; // expected-note {{temporary created here}}
353 static_assert(&b1.a1 != &b1.a2, "");
354 static_assert(&b1.a1.r != &b1.a2.r, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
355 
356 constexpr B &&b2 { { 3 }, { 4 } }; // expected-note {{temporary created here}}
357 static_assert(&b1 != &b2, "");
358 static_assert(&b1.a1 != &b2.a1, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
359 
360 constexpr thread_local B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
foo()361 void foo() {
362   constexpr static B b1 { { 1 }, { 2 } }; // ok
363   constexpr thread_local B b2 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
364   constexpr B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
365 }
366 
367 constexpr B &&b4 = ((1, 2), 3, 4, B { {10}, {{20}} }); // expected-warning 4{{unused}}
368 static_assert(&b4 != &b2, "");
369 
370 // Proposed DR: copy-elision doesn't trigger lifetime extension.
371 constexpr B b5 = B{ {0}, {0} }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
372 
373 namespace NestedNonStatic {
374   // Proposed DR: for a reference constant expression to refer to a static
375   // storage duration temporary, that temporary must itself be initialized
376   // by a constant expression (a core constant expression is not enough).
377   struct A { int &&r; };
378   struct B { A &&a; };
379   constexpr B a = { A{0} }; // ok
380   constexpr B b = { A(A{0}) }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
381 }
382 
383 namespace FakeInitList {
384   struct init_list_3_ints { const int (&x)[3]; };
385   struct init_list_2_init_list_3_ints { const init_list_3_ints (&x)[2]; };
386   constexpr init_list_2_init_list_3_ints ils = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } };
387 }
388 
389 }
390 
strcmp_ce(const char * p,const char * q)391 constexpr int strcmp_ce(const char *p, const char *q) {
392   return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
393 }
394 
395 namespace StringLiteral {
396 
397 template<typename Char>
MangleChars(const Char * p)398 constexpr int MangleChars(const Char *p) {
399   return *p + 3 * (*p ? MangleChars(p+1) : 0);
400 }
401 
402 static_assert(MangleChars("constexpr!") == 1768383, "");
403 static_assert(MangleChars(u8"constexpr!") == 1768383, "");
404 static_assert(MangleChars(L"constexpr!") == 1768383, "");
405 static_assert(MangleChars(u"constexpr!") == 1768383, "");
406 static_assert(MangleChars(U"constexpr!") == 1768383, "");
407 
408 constexpr char c0 = "nought index"[0];
409 constexpr char c1 = "nice index"[10];
410 constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}} expected-note {{read of dereferenced one-past-the-end pointer}}
411 constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} expected-note {{cannot refer to element -1 of array of 15 elements}}
412 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}}
413 
414 constexpr const char *p = "test" + 2;
415 static_assert(*p == 's', "");
416 
max_iter(const char * a,const char * b)417 constexpr const char *max_iter(const char *a, const char *b) {
418   return *a < *b ? b : a;
419 }
max_element(const char * a,const char * b)420 constexpr const char *max_element(const char *a, const char *b) {
421   return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
422 }
423 
424 constexpr char str[] = "the quick brown fox jumped over the lazy dog";
425 constexpr const char *max = max_element(begin(str), end(str));
426 static_assert(*max == 'z', "");
427 static_assert(max == str + 38, "");
428 
429 static_assert(strcmp_ce("hello world", "hello world") == 0, "");
430 static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
431 static_assert(strcmp_ce("constexpr", "test") < 0, "");
432 static_assert(strcmp_ce("", " ") < 0, "");
433 
434 struct S {
435   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}}
436 };
437 
438 struct T {
439   char c[6];
TStringLiteral::T440   constexpr T() : c{"foo"} {}
441 };
442 constexpr T t;
443 
444 static_assert(t.c[0] == 'f', "");
445 static_assert(t.c[1] == 'o', "");
446 static_assert(t.c[2] == 'o', "");
447 static_assert(t.c[3] == 0, "");
448 static_assert(t.c[4] == 0, "");
449 static_assert(t.c[5] == 0, "");
450 static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
451 
452 struct U {
453   wchar_t chars[6];
454   int n;
455 } constexpr u = { { L"test" }, 0 };
456 static_assert(u.chars[2] == L's', "");
457 
458 struct V {
459   char c[4];
VStringLiteral::V460   constexpr V() : c("hi!") {}
461 };
462 static_assert(V().c[1] == "i"[0], "");
463 
464 namespace Parens {
465   constexpr unsigned char a[] = ("foo"), b[] = {"foo"}, c[] = {("foo")},
466                           d[4] = ("foo"), e[5] = {"foo"}, f[6] = {("foo")};
467   static_assert(a[0] == 'f', "");
468   static_assert(b[1] == 'o', "");
469   static_assert(c[2] == 'o', "");
470   static_assert(d[0] == 'f', "");
471   static_assert(e[1] == 'o', "");
472   static_assert(f[2] == 'o', "");
473   static_assert(f[5] == 0, "");
474   static_assert(f[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
475 }
476 
477 }
478 
479 namespace Array {
480 
481 template<typename Iter>
Sum(Iter begin,Iter end)482 constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) {
483   return begin == end ? 0 : *begin + Sum(begin+1, end);
484 }
485 
486 constexpr int xs[] = { 1, 2, 3, 4, 5 };
487 constexpr int ys[] = { 5, 4, 3, 2, 1 };
488 constexpr int sum_xs = Sum(begin(xs), end(xs));
489 static_assert(sum_xs == 15, "");
490 
ZipFoldR(int (* F)(int x,int y,int c),int n,const int * xs,const int * ys,int c)491 constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
492                        const int *xs, const int *ys, int c) {
493   return n ? F(
494                *xs, // expected-note {{read of dereferenced one-past-the-end pointer}}
495                *ys,
496                ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
497       expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
498       expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
499            : c;
500 }
MulAdd(int x,int y,int c)501 constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
502 constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
503 static_assert(InnerProduct == 35, "");
504 
SubMul(int x,int y,int c)505 constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
506 constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
507 static_assert(DiffProd == 8, "");
508 static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
509       expected-error {{constant expression}} \
510       expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
511 
512 constexpr const int *p = xs + 3;
513 constexpr int xs4 = p[1]; // ok
514 constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
515 constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}}
516 constexpr int xs0 = p[-3]; // ok
517 constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
518 
519 constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
520 static_assert(zs[0][0][0][0] == 1, "");
521 static_assert(zs[1][1][1][1] == 16, "");
522 static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
523 static_assert((&zs[0][0][0][2])[-1] == 2, "");
524 static_assert(**(**(zs + 1) + 1) == 11, "");
525 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}}
526 static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
527 constexpr int err_zs_1_2_0_0 = zs[1][2][0][0]; // expected-error {{constant expression}} expected-note {{cannot access array element of pointer past the end}}
528 
fail(const int & p)529 constexpr int fail(const int &p) {
530   return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
531 }
532 static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \
533 expected-error {{static_assert expression is not an integral constant expression}} \
534 expected-note {{in call to 'fail(zs[1][0][1][0])'}}
535 
536 constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}}
SumNonzero(const int * p)537 constexpr int SumNonzero(const int *p) {
538   return *p + (*p ? SumNonzero(p+1) : 0);
539 }
CountZero(const int * p,const int * q)540 constexpr int CountZero(const int *p, const int *q) {
541   return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
542 }
543 static_assert(SumNonzero(arr) == 6, "");
544 static_assert(CountZero(arr, arr + 40) == 36, "");
545 
546 struct ArrayElem {
ArrayElemArray::ArrayElem547   constexpr ArrayElem() : n(0) {}
548   int n;
fArray::ArrayElem549   constexpr int f() const { return n; }
550 };
551 struct ArrayRVal {
ArrayRValArray::ArrayRVal552   constexpr ArrayRVal() {}
553   ArrayElem elems[10];
554 };
555 static_assert(ArrayRVal().elems[3].f() == 0, "");
556 
557 constexpr int selfref[2][2][2] = {
558   selfref[1][1][1] + 1, selfref[0][0][0] + 1,
559   selfref[1][0][1] + 1, selfref[0][1][0] + 1,
560   selfref[1][0][0] + 1, selfref[0][1][1] + 1 };
561 static_assert(selfref[0][0][0] == 1, "");
562 static_assert(selfref[0][0][1] == 2, "");
563 static_assert(selfref[0][1][0] == 1, "");
564 static_assert(selfref[0][1][1] == 2, "");
565 static_assert(selfref[1][0][0] == 1, "");
566 static_assert(selfref[1][0][1] == 3, "");
567 static_assert(selfref[1][1][0] == 0, "");
568 static_assert(selfref[1][1][1] == 0, "");
569 
570 struct TrivialDefCtor { int n; };
571 typedef TrivialDefCtor TDCArray[2][2];
572 static_assert(TDCArray{}[1][1].n == 0, "");
573 
574 struct NonAggregateTDC : TrivialDefCtor {};
575 typedef NonAggregateTDC NATDCArray[2][2];
576 static_assert(NATDCArray{}[1][1].n == 0, "");
577 
578 }
579 
580 namespace DependentValues {
581 
582 struct I { int n; typedef I V[10]; };
583 I::V x, y;
584 int g();
585 template<bool B, typename T> struct S : T {
586   int k;
fDependentValues::S587   void f() {
588     I::V &cells = B ? x : y;
589     I &i = cells[k];
590     switch (i.n) {}
591 
592     // FIXME: We should be able to diagnose this.
593     constexpr int n = g();
594 
595     constexpr int m = this->g(); // ok, could be constexpr
596   }
597 };
598 
599 }
600 
601 namespace Class {
602 
AClass::A603 struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
fn(const A & a)604 constexpr int fn(const A &a) { return a.k; }
605 static_assert(fn(A(4,5)) == 9, "");
606 
607 struct B { int n; int m; } constexpr b = { 0, b.n };
608 struct C {
CClass::C609   constexpr C(C *this_) : m(42), n(this_->m) {} // ok
610   int m, n;
611 };
612 struct D {
613   C c;
DClass::D614   constexpr D() : c(&c) {}
615 };
616 static_assert(D().c.n == 42, "");
617 
618 struct E {
EClass::E619   constexpr E() : p(&p) {}
620   void *p;
621 };
622 constexpr const E &e1 = E();
623 // This is a constant expression if we elide the copy constructor call, and
624 // is not a constant expression if we don't! But we do, so it is.
625 constexpr E e2 = E();
626 static_assert(e2.p == &e2.p, "");
627 constexpr E e3;
628 static_assert(e3.p == &e3.p, "");
629 
630 extern const class F f;
631 struct F {
FClass::F632   constexpr F() : p(&f.p) {}
633   const void *p;
634 };
635 constexpr F f;
636 
637 struct G {
638   struct T {
TClass::G::T639     constexpr T(T *p) : u1(), u2(p) {}
640     union U1 {
U1()641       constexpr U1() {}
642       int a, b = 42;
643     } u1;
644     union U2 {
U2(T * p)645       constexpr U2(T *p) : c(p->u1.b) {}
646       int c, d;
647     } u2;
648   } t;
GClass::G649   constexpr G() : t(&t) {}
650 } constexpr g;
651 
652 static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
653 static_assert(g.t.u1.b == 42, "");
654 static_assert(g.t.u2.c == 42, "");
655 static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
656 
657 struct S {
658   int a, b;
659   const S *p;
660   double d;
661   const char *q;
662 
SClass::S663   constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
664 };
665 
666 S global(43, &global);
667 
668 static_assert(S(15, &global).b == 15, "");
669 
CheckS(const S & s)670 constexpr bool CheckS(const S &s) {
671   return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
672 }
673 static_assert(CheckS(S(27, &global)), "");
674 
675 struct Arr {
676   char arr[3];
ArrClass::Arr677   constexpr Arr() : arr{'x', 'y', 'z'} {}
678 };
hash(Arr && a)679 constexpr int hash(Arr &&a) {
680   return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
681 }
682 constexpr int k = hash(Arr());
683 static_assert(k == 0x007a7978, "");
684 
685 
686 struct AggregateInit {
687   const char &c;
688   int n;
689   double d;
690   int arr[5];
691   void *p;
692 };
693 
694 constexpr AggregateInit agg1 = { "hello"[0] };
695 
696 static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
697 static_assert(agg1.n == 0, "");
698 static_assert(agg1.d == 0.0, "");
699 static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
700 static_assert(agg1.arr[0] == 0, "");
701 static_assert(agg1.arr[4] == 0, "");
702 static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
703 static_assert(agg1.p == nullptr, "");
704 
705 static constexpr const unsigned char uc[] = { "foo" };
706 static_assert(uc[0] == 'f', "");
707 static_assert(uc[3] == 0, "");
708 
709 namespace SimpleDerivedClass {
710 
711 struct B {
BClass::SimpleDerivedClass::B712   constexpr B(int n) : a(n) {}
713   int a;
714 };
715 struct D : B {
DClass::SimpleDerivedClass::D716   constexpr D(int n) : B(n) {}
717 };
718 constexpr D d(3);
719 static_assert(d.a == 3, "");
720 
721 }
722 
BottomClass::Bottom723 struct Bottom { constexpr Bottom() {} };
724 struct Base : Bottom {
BaseClass::Base725   constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
726   int a;
727   const char *b;
728 };
729 struct Base2 : Bottom {
Base2Class::Base2730   constexpr Base2(const int &r) : r(r) {}
731   int q = 123;
732   const int &r;
733 };
734 struct Derived : Base, Base2 {
DerivedClass::Derived735   constexpr Derived() : Base(76), Base2(a) {}
736   int c = r + b[1];
737 };
738 
operator ==(const Base & a,const Base & b)739 constexpr bool operator==(const Base &a, const Base &b) {
740   return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
741 }
742 
743 constexpr Base base;
744 constexpr Base base2(76);
745 constexpr Derived derived;
746 static_assert(derived.a == 76, "");
747 static_assert(derived.b[2] == 's', "");
748 static_assert(derived.c == 76 + 'e', "");
749 static_assert(derived.q == 123, "");
750 static_assert(derived.r == 76, "");
751 static_assert(&derived.r == &derived.a, "");
752 
753 static_assert(!(derived == base), "");
754 static_assert(derived == base2, "");
755 
756 constexpr Bottom &bot1 = (Base&)derived;
757 constexpr Bottom &bot2 = (Base2&)derived;
758 static_assert(&bot1 != &bot2, "");
759 
760 constexpr Bottom *pb1 = (Base*)&derived;
761 constexpr Bottom *pb2 = (Base2*)&derived;
762 static_assert(&pb1 != &pb2, "");
763 static_assert(pb1 == &bot1, "");
764 static_assert(pb2 == &bot2, "");
765 
766 constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
767 constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
768 constexpr Base2 &ok2 = (Base2&)bot2;
769 static_assert(&ok2 == &derived, "");
770 
771 constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
772 constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
773 constexpr Base2 *pok2 = (Base2*)pb2;
774 static_assert(pok2 == &derived, "");
775 static_assert(&ok2 == pok2, "");
776 static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
777 static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
778 
779 // Core issue 903: we do not perform constant evaluation when checking for a
780 // null pointer in C++11. Just check for an integer literal with value 0.
781 constexpr Base *nullB = 42 - 6 * 7; // expected-error {{cannot initialize a variable of type 'Class::Base *const' with an rvalue of type 'int'}}
782 constexpr Base *nullB1 = 0;
783 static_assert((Bottom*)nullB == 0, "");
784 static_assert((Derived*)nullB == 0, "");
785 static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
786 Base *nullB2 = '\0'; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'char'}}
787 Base *nullB3 = (0);
788 Base *nullB4 = false; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'bool'}}
789 Base *nullB5 = ((0ULL));
790 Base *nullB6 = 0.; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'double'}}
791 enum Null { kNull };
792 Base *nullB7 = kNull; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'Class::Null'}}
793 static_assert(nullB1 == (1 - 1), ""); // expected-error {{comparison between pointer and integer}}
794 
795 
796 
797 namespace ConversionOperators {
798 
799 struct T {
TClass::ConversionOperators::T800   constexpr T(int n) : k(5*n - 3) {}
operator intClass::ConversionOperators::T801   constexpr operator int() const { return k; }
802   int k;
803 };
804 
805 struct S {
SClass::ConversionOperators::S806   constexpr S(int n) : k(2*n + 1) {}
operator intClass::ConversionOperators::S807   constexpr operator int() const { return k; }
operator TClass::ConversionOperators::S808   constexpr operator T() const { return T(k); }
809   int k;
810 };
811 
check(T a,T b)812 constexpr bool check(T a, T b) { return a == b.k; }
813 
814 static_assert(S(5) == 11, "");
815 static_assert(check(S(5), 11), "");
816 
817 namespace PR14171 {
818 
819 struct X {
820   constexpr (operator int)() const { return 0; }
821 };
822 static_assert(X() == 0, "");
823 
824 }
825 
826 }
827 
828 struct This {
fClass::This829   constexpr int f() const { return 0; }
gClass::This830   static constexpr int g() { return 0; }
hClass::This831   void h() {
832     constexpr int x = f(); // expected-error {{must be initialized by a constant}}
833     // expected-note@-1 {{implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
834     constexpr int y = this->f(); // expected-error {{must be initialized by a constant}}
835     // expected-note-re@-1 {{{{^}}use of 'this' pointer}}
836     constexpr int z = g();
837     static_assert(z == 0, "");
838   }
839 };
840 
841 }
842 
843 namespace Temporaries {
844 
845 struct S {
STemporaries::S846   constexpr S() {}
847   constexpr int f() const;
848   constexpr int g() const;
849 };
850 struct T : S {
TTemporaries::T851   constexpr T(int n) : S(), n(n) {}
852   int n;
853 };
f() const854 constexpr int S::f() const {
855   return static_cast<const T*>(this)->n; // expected-note {{cannot cast}}
856 }
g() const857 constexpr int S::g() const {
858   // FIXME: Better diagnostic for this.
859   return this->*(int(S::*))&T::n; // expected-note {{subexpression}}
860 }
861 // The T temporary is implicitly cast to an S subobject, but we can recover the
862 // T full-object via a base-to-derived cast, or a derived-to-base-casted member
863 // pointer.
864 static_assert(S().f(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->f()'}}
865 static_assert(S().g(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->g()'}}
866 static_assert(T(3).f() == 3, "");
867 static_assert(T(4).g() == 4, "");
868 
f(const S & s)869 constexpr int f(const S &s) {
870   return static_cast<const T&>(s).n;
871 }
872 constexpr int n = f(T(5));
873 static_assert(f(T(5)) == 5, "");
874 
b(int n)875 constexpr bool b(int n) { return &n; }
876 static_assert(b(0), "");
877 
878 struct NonLiteral {
879   NonLiteral();
880   int f();
881 };
882 constexpr int k = NonLiteral().f(); // expected-error {{constant expression}} expected-note {{non-literal type 'Temporaries::NonLiteral'}}
883 
884 }
885 
886 namespace Union {
887 
888 union U {
889   int a;
890   int b;
891 };
892 
893 constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{C99 feature}}
894 static_assert(u[0].a == 0, "");
895 static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
896 static_assert(u[1].b == 1, "");
897 static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
898 static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}}
899 static_assert((&(u[1]) + 1 + 1)->b == 3, "");
900 
901 constexpr U v = {};
902 static_assert(v.a == 0, "");
903 
904 union Empty {};
905 constexpr Empty e = {};
906 
907 // Make sure we handle trivial copy constructors for unions.
908 constexpr U x = {42};
909 constexpr U y = x;
910 static_assert(y.a == 42, "");
911 static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}}
912 
913 }
914 
915 namespace MemberPointer {
916   struct A {
AMemberPointer::A917     constexpr A(int n) : n(n) {}
918     int n;
fMemberPointer::A919     constexpr int f() const { return n + 3; }
920   };
921   constexpr A a(7);
922   static_assert(A(5).*&A::n == 5, "");
923   static_assert((&a)->*&A::n == 7, "");
924   static_assert((A(8).*&A::f)() == 11, "");
925   static_assert(((&a)->*&A::f)() == 10, "");
926 
927   struct B : A {
BMemberPointer::B928     constexpr B(int n, int m) : A(n), m(m) {}
929     int m;
gMemberPointer::B930     constexpr int g() const { return n + m + 1; }
931   };
932   constexpr B b(9, 13);
933   static_assert(B(4, 11).*&A::n == 4, "");
934   static_assert(B(4, 11).*&B::m == 11, "");
935   static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
936   static_assert((&b)->*&A::n == 9, "");
937   static_assert((&b)->*&B::m == 13, "");
938   static_assert((&b)->*(int(A::*))&B::m == 13, "");
939   static_assert((B(4, 11).*&A::f)() == 7, "");
940   static_assert((B(4, 11).*&B::g)() == 16, "");
941   static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
942   static_assert(((&b)->*&A::f)() == 12, "");
943   static_assert(((&b)->*&B::g)() == 23, "");
944   static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
945 
946   struct S {
SMemberPointer::S947     constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
948       m(m), n(n), pf(pf), pn(pn) {}
SMemberPointer::S949     constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
950 
fMemberPointer::S951     constexpr int f() const { return this->*pn; }
952     virtual int g() const;
953 
954     int m, n;
955     int (S::*pf)() const;
956     int S::*pn;
957   };
958 
959   constexpr int S::*pm = &S::m;
960   constexpr int S::*pn = &S::n;
961   constexpr int (S::*pf)() const = &S::f;
962   constexpr int (S::*pg)() const = &S::g;
963 
964   constexpr S s(2, 5, &S::f, &S::m);
965 
966   static_assert((s.*&S::f)() == 2, "");
967   static_assert((s.*s.pf)() == 2, "");
968 
969   static_assert(pf == &S::f, "");
970   static_assert(pf == s.*&S::pf, "");
971   static_assert(pm == &S::m, "");
972   static_assert(pm != pn, "");
973   static_assert(s.pn != pn, "");
974   static_assert(s.pn == pm, "");
975   static_assert(pg != nullptr, "");
976   static_assert(pf != nullptr, "");
977   static_assert((int S::*)nullptr == nullptr, "");
978   static_assert(pg == pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
979   static_assert(pf != pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
980 
981   template<int n> struct T : T<n-1> {};
982   template<> struct T<0> { int n; };
983   template<> struct T<30> : T<29> { int m; };
984 
985   T<17> t17;
986   T<30> t30;
987 
988   constexpr int (T<10>::*deepn) = &T<0>::n;
989   static_assert(&(t17.*deepn) == &t17.n, "");
990   static_assert(deepn == &T<2>::n, "");
991 
992   constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
993   constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
994   static_assert(&(t30.*deepm) == &t30.m, "");
995   static_assert(deepm == &T<50>::m, "");
996   static_assert(deepm != deepn, "");
997 
998   constexpr T<5> *p17_5 = &t17;
999   constexpr T<13> *p17_13 = (T<13>*)p17_5;
1000   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>'}}
1001   static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
1002   static_assert(&(p17_13->*deepn) == &t17.n, "");
1003   constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
1004 
1005   constexpr T<5> *p30_5 = &t30;
1006   constexpr T<23> *p30_23 = (T<23>*)p30_5;
1007   constexpr T<13> *p30_13 = p30_23;
1008   static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
1009   static_assert(&(p30_13->*deepn) == &t30.n, "");
1010   static_assert(&(p30_23->*deepn) == &t30.n, "");
1011   static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
1012   static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
1013   static_assert(&(p30_23->*deepm) == &t30.m, "");
1014 
1015   struct Base { int n; };
1016   template<int N> struct Mid : Base {};
1017   struct Derived : Mid<0>, Mid<1> {};
1018   static_assert(&Mid<0>::n == &Mid<1>::n, "");
1019   static_assert((int Derived::*)(int Mid<0>::*)&Mid<0>::n !=
1020                 (int Derived::*)(int Mid<1>::*)&Mid<1>::n, "");
1021   static_assert(&Mid<0>::n == (int Mid<0>::*)&Base::n, "");
1022 }
1023 
1024 namespace ArrayBaseDerived {
1025 
1026   struct Base {
BaseArrayBaseDerived::Base1027     constexpr Base() {}
1028     int n = 0;
1029   };
1030   struct Derived : Base {
DerivedArrayBaseDerived::Derived1031     constexpr Derived() {}
fArrayBaseDerived::Derived1032     constexpr const int *f() const { return &n; }
1033   };
1034 
1035   constexpr Derived a[10];
1036   constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
1037   constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
1038   static_assert(pb3 == pd3, "");
1039 
1040   // pb3 does not point to an array element.
1041   constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
1042   constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}}
1043   constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}}
1044   constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}}
1045   constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}}
1046   constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}}
1047   constexpr Base *pb3a = pb4 - 1;
1048 
1049   // pb4 does not point to a Derived.
1050   constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}}
1051   constexpr Derived *pd3a = (Derived*)pb3a;
1052   constexpr int pd3n = pd3a->n;
1053 
1054   // pd3a still points to the Derived array.
1055   constexpr Derived *pd6 = pd3a + 3;
1056   static_assert(pd6 == &a[6], "");
1057   constexpr Derived *pd9 = pd6 + 3;
1058   constexpr Derived *pd10 = pd6 + 4;
1059   constexpr int pd9n = pd9->n; // ok
1060   constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}}
1061   constexpr int pd0n = pd10[-10].n;
1062   constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}}
1063 
1064   constexpr Base *pb9 = pd9;
1065   constexpr const int *(Base::*pfb)() const =
1066       static_cast<const int *(Base::*)() const>(&Derived::f);
1067   static_assert((pb9->*pfb)() == &a[9].n, "");
1068 }
1069 
1070 namespace Complex {
1071 
1072 class complex {
1073   int re, im;
1074 public:
complex(int re=0,int im=0)1075   constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
complex(const complex & o)1076   constexpr complex(const complex &o) : re(o.re), im(o.im) {}
operator -() const1077   constexpr complex operator-() const { return complex(-re, -im); }
operator +(const complex & l,const complex & r)1078   friend constexpr complex operator+(const complex &l, const complex &r) {
1079     return complex(l.re + r.re, l.im + r.im);
1080   }
operator -(const complex & l,const complex & r)1081   friend constexpr complex operator-(const complex &l, const complex &r) {
1082     return l + -r;
1083   }
operator *(const complex & l,const complex & r)1084   friend constexpr complex operator*(const complex &l, const complex &r) {
1085     return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
1086   }
operator ==(const complex & l,const complex & r)1087   friend constexpr bool operator==(const complex &l, const complex &r) {
1088     return l.re == r.re && l.im == r.im;
1089   }
operator !=(const complex & r) const1090   constexpr bool operator!=(const complex &r) const {
1091     return re != r.re || im != r.im;
1092   }
real() const1093   constexpr int real() const { return re; }
imag() const1094   constexpr int imag() const { return im; }
1095 };
1096 
1097 constexpr complex i = complex(0, 1);
1098 constexpr complex k = (3 + 4*i) * (6 - 4*i);
1099 static_assert(complex(1,0).real() == 1, "");
1100 static_assert(complex(1,0).imag() == 0, "");
1101 static_assert(((complex)1).imag() == 0, "");
1102 static_assert(k.real() == 34, "");
1103 static_assert(k.imag() == 12, "");
1104 static_assert(k - 34 == 12*i, "");
1105 static_assert((complex)1 == complex(1), "");
1106 static_assert((complex)1 != complex(0, 1), "");
1107 static_assert(complex(1) == complex(1), "");
1108 static_assert(complex(1) != complex(0, 1), "");
makeComplex(int re,int im)1109 constexpr complex makeComplex(int re, int im) { return complex(re, im); }
1110 static_assert(makeComplex(1,0) == complex(1), "");
1111 static_assert(makeComplex(1,0) != complex(0, 1), "");
1112 
1113 class complex_wrap : public complex {
1114 public:
complex_wrap(int re,int im=0)1115   constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
complex_wrap(const complex_wrap & o)1116   constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
1117 };
1118 
1119 static_assert((complex_wrap)1 == complex(1), "");
1120 static_assert((complex)1 != complex_wrap(0, 1), "");
1121 static_assert(complex(1) == complex_wrap(1), "");
1122 static_assert(complex_wrap(1) != complex(0, 1), "");
makeComplexWrap(int re,int im)1123 constexpr complex_wrap makeComplexWrap(int re, int im) {
1124   return complex_wrap(re, im);
1125 }
1126 static_assert(makeComplexWrap(1,0) == complex(1), "");
1127 static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
1128 
1129 }
1130 
1131 namespace PR11595 {
operator ==PR11595::A1132   struct A { constexpr bool operator==(int x) const { return true; } };
1133   struct B { B(); A& x; };
1134   static_assert(B().x == 3, "");  // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
1135 
f(int k)1136   constexpr bool f(int k) { // expected-error {{constexpr function never produces a constant expression}}
1137     return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
1138   }
1139 }
1140 
1141 namespace ExprWithCleanups {
1142   struct A { A(); ~A(); int get(); };
get(bool FromA)1143   constexpr int get(bool FromA) { return FromA ? A().get() : 1; }
1144   constexpr int n = get(false);
1145 }
1146 
1147 namespace Volatile {
1148 
1149 volatile constexpr int n1 = 0; // expected-note {{here}}
1150 volatile const int n2 = 0; // expected-note {{here}}
1151 int n3 = 37; // expected-note {{declared here}}
1152 
1153 constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1154 constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1155 constexpr int m1b = const_cast<const int&>(n1); // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
1156 constexpr int m2b = const_cast<const int&>(n2); // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
1157 
1158 struct T { int n; };
1159 const T t = { 42 }; // expected-note {{declared here}}
1160 
f(volatile int && r)1161 constexpr int f(volatile int &&r) {
1162   return r; // expected-note {{read of volatile-qualified type 'volatile int'}}
1163 }
g(volatile int && r)1164 constexpr int g(volatile int &&r) {
1165   return const_cast<int&>(r); // expected-note {{read of volatile temporary is not allowed in a constant expression}}
1166 }
1167 struct S {
1168   int j : f(0); // expected-error {{constant expression}} expected-note {{in call to 'f(0)'}}
1169   int k : g(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'g(0)'}}
1170   int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
1171   int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}}
1172 };
1173 
1174 }
1175 
1176 namespace ExternConstexpr {
1177   extern constexpr int n = 0;
1178   extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}}
f()1179   void f() {
1180     extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}}
1181     constexpr int j = 0;
1182     constexpr int k; // expected-error {{default initialization of an object of const type}} expected-note{{add an explicit initializer to initialize 'k'}}
1183   }
1184 }
1185 
1186 namespace ComplexConstexpr {
1187   constexpr _Complex float test1 = {};
1188   constexpr _Complex float test2 = {1};
1189   constexpr _Complex double test3 = {1,2};
1190   constexpr _Complex int test4 = {4};
1191   constexpr _Complex int test5 = 4;
1192   constexpr _Complex int test6 = {5,6};
1193   typedef _Complex float fcomplex;
1194   constexpr fcomplex test7 = fcomplex();
1195 
1196   constexpr const double &t2r = __real test3;
1197   constexpr const double &t2i = __imag test3;
1198   static_assert(&t2r + 1 == &t2i, "");
1199   static_assert(t2r == 1.0, "");
1200   static_assert(t2i == 2.0, "");
1201   constexpr const double *t2p = &t2r;
1202   static_assert(t2p[-1] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element -1 of array of 2 elements}}
1203   static_assert(t2p[0] == 1.0, "");
1204   static_assert(t2p[1] == 2.0, "");
1205   static_assert(t2p[2] == 0.0, ""); // expected-error {{constant expr}} expected-note {{one-past-the-end pointer}}
1206   static_assert(t2p[3] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element 3 of array of 2 elements}}
1207   constexpr _Complex float *p = 0;
1208   constexpr float pr = __real *p; // expected-error {{constant expr}} expected-note {{cannot access real component of null}}
1209   constexpr float pi = __imag *p; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of null}}
1210   constexpr const _Complex double *q = &test3 + 1;
1211   constexpr double qr = __real *q; // expected-error {{constant expr}} expected-note {{cannot access real component of pointer past the end}}
1212   constexpr double qi = __imag *q; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of pointer past the end}}
1213 
1214   static_assert(__real test6 == 5, "");
1215   static_assert(__imag test6 == 6, "");
1216   static_assert(&__imag test6 == &__real test6 + 1, "");
1217 }
1218 
1219 // _Atomic(T) is exactly like T for the purposes of constant expression
1220 // evaluation..
1221 namespace Atomic {
1222   constexpr _Atomic int n = 3;
1223 
1224   struct S { _Atomic(double) d; };
1225   constexpr S s = { 0.5 };
1226   constexpr double d1 = s.d;
1227   constexpr double d2 = n;
1228   constexpr _Atomic double d3 = n;
1229 
1230   constexpr _Atomic(int) n2 = d3;
1231   static_assert(d1 == 0.5, "");
1232   static_assert(d3 == 3.0, "");
1233 
1234   namespace PR16056 {
1235     struct TestVar {
1236       _Atomic(int) value;
TestVarAtomic::PR16056::TestVar1237       constexpr TestVar(int value) : value(value) {}
1238     };
1239     constexpr TestVar testVar{-1};
1240     static_assert(testVar.value == -1, "");
1241   }
1242 }
1243 
1244 namespace InstantiateCaseStmt {
f()1245   template<int x> constexpr int f() { return x; }
g(int c)1246   template<int x> int g(int c) { switch(c) { case f<x>(): return 1; } return 0; }
gg(int c)1247   int gg(int c) { return g<4>(c); }
1248 }
1249 
1250 namespace ConvertedConstantExpr {
1251   extern int &m;
1252   extern int &n;
1253 
1254   constexpr int k = 4;
1255   int &m = const_cast<int&>(k);
1256 
1257   // If we have nothing more interesting to say, ensure we don't produce a
1258   // useless note and instead just point to the non-constant subexpression.
1259   enum class E {
1260     em = m,
1261     en = n, // expected-error {{not a constant expression}}
1262     eo = (m +
1263           n // expected-error {{not a constant expression}}
1264           ),
1265     eq = reinterpret_cast<int>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}}
1266   };
1267 }
1268 
1269 namespace IndirectField {
1270   struct S {
1271     struct { // expected-warning {{GNU extension}}
1272       union { // expected-warning {{declared in an anonymous struct}}
1273         struct { // expected-warning {{GNU extension}} expected-warning {{declared in an anonymous union}}
1274           int a;
1275           int b;
1276         };
1277         int c;
1278       };
1279       int d;
1280     };
1281     union {
1282       int e;
1283       int f;
1284     };
SIndirectField::S1285     constexpr S(int a, int b, int d, int e) : a(a), b(b), d(d), e(e) {}
SIndirectField::S1286     constexpr S(int c, int d, int f) : c(c), d(d), f(f) {}
1287   };
1288 
1289   constexpr S s1(1, 2, 3, 4);
1290   constexpr S s2(5, 6, 7);
1291 
1292   // FIXME: The diagnostics here do a very poor job of explaining which unnamed
1293   // member is active and which is requested.
1294   static_assert(s1.a == 1, "");
1295   static_assert(s1.b == 2, "");
1296   static_assert(s1.c == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1297   static_assert(s1.d == 3, "");
1298   static_assert(s1.e == 4, "");
1299   static_assert(s1.f == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1300 
1301   static_assert(s2.a == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1302   static_assert(s2.b == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1303   static_assert(s2.c == 5, "");
1304   static_assert(s2.d == 6, "");
1305   static_assert(s2.e == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1306   static_assert(s2.f == 7, "");
1307 }
1308 
1309 // DR1405: don't allow reading mutable members in constant expressions.
1310 namespace MutableMembers {
1311   struct MM {
1312     mutable int n; // expected-note 3{{declared here}}
1313   } constexpr mm = { 4 };
1314   constexpr int mmn = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1315   int x = (mm.n = 1, 3);
1316   constexpr int mmn2 = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1317 
1318   // Here's one reason why allowing this would be a disaster...
1319   template<int n> struct Id { int k = n; };
f()1320   int f() {
1321     constexpr MM m = { 0 };
1322     ++m.n;
1323     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}}
1324   }
1325 
1326   struct A { int n; };
1327   struct B { mutable A a; }; // expected-note {{here}}
1328   struct C { B b; };
1329   constexpr C c[3] = {};
1330   constexpr int k = c[1].b.a.n; // expected-error {{constant expression}} expected-note {{mutable}}
1331 
1332   struct D { int x; mutable int y; }; // expected-note {{here}}
1333   constexpr D d1 = { 1, 2 };
1334   int l = ++d1.y;
1335   constexpr D d2 = d1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1336 
1337   struct E {
1338     union {
1339       int a;
1340       mutable int b; // expected-note {{here}}
1341     };
1342   };
1343   constexpr E e1 = {{1}};
1344   constexpr E e2 = e1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1345 
1346   struct F {
1347     union U { };
1348     mutable U u;
1349     struct X { };
1350     mutable X x;
1351     struct Y : X { X x; U u; };
1352     mutable Y y;
1353     int n;
1354   };
1355   // This is OK; we don't actually read any mutable state here.
1356   constexpr F f1 = {};
1357   constexpr F f2 = f1;
1358 
1359   struct G {
1360     struct X {};
1361     union U { X a; };
1362     mutable U u; // expected-note {{here}}
1363   };
1364   constexpr G g1 = {};
1365   constexpr G g2 = g1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1366   constexpr G::U gu1 = {};
1367   constexpr G::U gu2 = gu1;
1368 
1369   union H {
1370     mutable G::X gx; // expected-note {{here}}
1371   };
1372   constexpr H h1 = {};
1373   constexpr H h2 = h1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1374 }
1375 
1376 namespace Fold {
1377 
1378   // This macro forces its argument to be constant-folded, even if it's not
1379   // otherwise a constant expression.
1380   #define fold(x) (__builtin_constant_p(x) ? (x) : (x))
1381 
1382   constexpr int n = (int)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}}
1383   constexpr int m = fold((int)(char*)123); // ok
1384   static_assert(m == 123, "");
1385 
1386   #undef fold
1387 
1388 }
1389 
1390 namespace DR1454 {
1391 
f(const int & n)1392 constexpr const int &f(const int &n) { return n; }
1393 constexpr int k1 = f(0); // ok
1394 
1395 struct Wrap {
1396   const int &value;
1397 };
g(const Wrap & w)1398 constexpr const Wrap &g(const Wrap &w) { return w; }
1399 constexpr int k2 = g({0}).value; // ok
1400 
1401 // The temporary here has static storage duration, so we can bind a constexpr
1402 // reference to it.
1403 constexpr const int &i = 1;
1404 constexpr const int j = i;
1405 static_assert(j == 1, "");
1406 
1407 // The temporary here is not const, so it can't be read outside the expression
1408 // in which it was created (per the C++14 rules, which we use to avoid a C++11
1409 // defect).
1410 constexpr int &&k = 1; // expected-note {{temporary created here}}
1411 constexpr const int l = k; // expected-error {{constant expression}} expected-note {{read of temporary}}
1412 
f()1413 void f() {
1414   // The temporary here has automatic storage duration, so we can't bind a
1415   // constexpr reference to it.
1416   constexpr const int &i = 1; // expected-error {{constant expression}} expected-note 2{{temporary}}
1417 }
1418 
1419 }
1420 
1421 namespace RecursiveOpaqueExpr {
1422   template<typename Iter>
LastNonzero(Iter p,Iter q)1423   constexpr auto LastNonzero(Iter p, Iter q) -> decltype(+*p) {
1424     return p != q ? (LastNonzero(p+1, q) ?: *p) : 0; // expected-warning {{GNU}}
1425   }
1426 
1427   constexpr int arr1[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 0 };
1428   static_assert(LastNonzero(begin(arr1), end(arr1)) == 4, "");
1429 
1430   constexpr int arr2[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 5 };
1431   static_assert(LastNonzero(begin(arr2), end(arr2)) == 5, "");
1432 
1433   constexpr int arr3[] = {
1434     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,
1435     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,
1436     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,
1437     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,
1438     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,
1439     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,
1440     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,
1441     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 };
1442   static_assert(LastNonzero(begin(arr3), end(arr3)) == 2, "");
1443 }
1444 
1445 namespace VLASizeof {
1446 
f(int k)1447   void f(int k) {
1448     int arr[k]; // expected-warning {{C99}}
1449     constexpr int n = 1 +
1450         sizeof(arr) // expected-error {{constant expression}}
1451         * 3;
1452   }
1453 }
1454 
1455 namespace CompoundLiteral {
1456   // FIXME:
1457   // We don't model the semantics of this correctly: the compound literal is
1458   // represented as a prvalue in the AST, but actually behaves like an lvalue.
1459   // We treat the compound literal as a temporary and refuse to produce a
1460   // pointer to it. This is OK: we're not required to treat this as a constant
1461   // in C++, and in C we model compound literals as lvalues.
1462   constexpr int *p = (int*)(int[1]){0}; // expected-warning {{C99}} expected-error {{constant expression}} expected-note 2{{temporary}}
1463 }
1464 
1465 namespace Vector {
1466   typedef int __attribute__((vector_size(16))) VI4;
f(int n)1467   constexpr VI4 f(int n) {
1468     return VI4 { n * 3, n + 4, n - 5, n / 6 };
1469   }
1470   constexpr auto v1 = f(10);
1471 
1472   typedef double __attribute__((vector_size(32))) VD4;
g(int n)1473   constexpr VD4 g(int n) {
1474     return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 }; // expected-warning {{C99}}
1475   }
1476   constexpr auto v2 = g(4);
1477 }
1478 
1479 // PR12626, redux
1480 namespace InvalidClasses {
test0()1481   void test0() {
1482     struct X; // expected-note {{forward declaration}}
1483     struct Y { bool b; X x; }; // expected-error {{field has incomplete type}}
1484     Y y;
1485     auto& b = y.b;
1486   }
1487 }
1488 
1489 namespace NamespaceAlias {
f()1490   constexpr int f() {
1491     namespace NS = NamespaceAlias; // expected-warning {{use of this statement in a constexpr function is a C++14 extension}}
1492     return &NS::f != nullptr;
1493   }
1494 }
1495 
1496 // Constructors can be implicitly constexpr, even for a non-literal type.
1497 namespace ImplicitConstexpr {
1498   struct Q { Q() = default; Q(const Q&) = default; Q(Q&&) = default; ~Q(); }; // expected-note 3{{here}}
1499   struct R { constexpr R() noexcept; constexpr R(const R&) noexcept; constexpr R(R&&) noexcept; ~R() noexcept; };
1500   struct S { R r; }; // expected-note 3{{here}}
1501   struct T { T(const T&) noexcept; T(T &&) noexcept; ~T() noexcept; };
1502   struct U { T t; }; // expected-note 3{{here}}
1503   static_assert(!__is_literal_type(Q), "");
1504   static_assert(!__is_literal_type(R), "");
1505   static_assert(!__is_literal_type(S), "");
1506   static_assert(!__is_literal_type(T), "");
1507   static_assert(!__is_literal_type(U), "");
1508   struct Test {
1509     friend Q::Q() noexcept; // expected-error {{follows constexpr}}
1510     friend Q::Q(Q&&) noexcept; // expected-error {{follows constexpr}}
1511     friend Q::Q(const Q&) noexcept; // expected-error {{follows constexpr}}
1512     friend S::S() noexcept; // expected-error {{follows constexpr}}
1513     friend S::S(S&&) noexcept; // expected-error {{follows constexpr}}
1514     friend S::S(const S&) noexcept; // expected-error {{follows constexpr}}
1515     friend constexpr U::U() noexcept; // expected-error {{follows non-constexpr}}
1516     friend constexpr U::U(U&&) noexcept; // expected-error {{follows non-constexpr}}
1517     friend constexpr U::U(const U&) noexcept; // expected-error {{follows non-constexpr}}
1518   };
1519 }
1520 
1521 // Indirectly test that an implicit lvalue to xvalue conversion performed for
1522 // an NRVO move operation isn't implemented as CK_LValueToRValue.
1523 namespace PR12826 {
1524   struct Foo {};
id(Foo x)1525   constexpr Foo id(Foo x) { return x; }
1526   constexpr Foo res(id(Foo()));
1527 }
1528 
1529 namespace PR13273 {
1530   struct U {
1531     int t;
1532     U() = default;
1533   };
1534 
1535   struct S : U {
1536     S() = default;
1537   };
1538 
1539   // S's default constructor isn't constexpr, because U's default constructor
1540   // doesn't initialize 't', but it's trivial, so value-initialization doesn't
1541   // actually call it.
1542   static_assert(S{}.t == 0, "");
1543 }
1544 
1545 namespace PR12670 {
1546   struct S {
SPR12670::S1547     constexpr S(int a0) : m(a0) {}
SPR12670::S1548     constexpr S() : m(6) {}
1549     int m;
1550   };
1551   constexpr S x[3] = { {4}, 5 };
1552   static_assert(x[0].m == 4, "");
1553   static_assert(x[1].m == 5, "");
1554   static_assert(x[2].m == 6, "");
1555 }
1556 
1557 // Indirectly test that an implicit lvalue-to-rvalue conversion is performed
1558 // when a conditional operator has one argument of type void and where the other
1559 // is a glvalue of class type.
1560 namespace ConditionalLValToRVal {
1561   struct A {
AConditionalLValToRVal::A1562     constexpr A(int a) : v(a) {}
1563     int v;
1564   };
1565 
f(const A & a)1566   constexpr A f(const A &a) {
1567     return a.v == 0 ? throw a : a;
1568   }
1569 
1570   constexpr A a(4);
1571   static_assert(f(a).v == 4, "");
1572 }
1573 
1574 namespace TLS {
1575   __thread int n;
1576   int m;
1577 
1578   constexpr bool b = &n == &n;
1579 
1580   constexpr int *p = &n; // expected-error{{constexpr variable 'p' must be initialized by a constant expression}}
1581 
f()1582   constexpr int *f() { return &n; }
1583   constexpr int *q = f(); // expected-error{{constexpr variable 'q' must be initialized by a constant expression}}
1584   constexpr bool c = f() == f();
1585 
g()1586   constexpr int *g() { return &m; }
1587   constexpr int *r = g();
1588 }
1589 
1590 namespace Void {
f()1591   constexpr void f() { return; } // expected-error{{constexpr function's return type 'void' is not a literal type}}
1592 
1593   void assert_failed(const char *msg, const char *file, int line); // expected-note {{declared here}}
1594 #define ASSERT(expr) ((expr) ? static_cast<void>(0) : assert_failed(#expr, __FILE__, __LINE__))
1595   template<typename T, size_t S>
get(T (& a)[S],size_t k)1596   constexpr T get(T (&a)[S], size_t k) {
1597     return ASSERT(k > 0 && k < S), a[k]; // expected-note{{non-constexpr function 'assert_failed'}}
1598   }
1599 #undef ASSERT
1600   template int get(int (&a)[4], size_t);
1601   constexpr int arr[] = { 4, 1, 2, 3, 4 };
1602   static_assert(get(arr, 1) == 1, "");
1603   static_assert(get(arr, 4) == 4, "");
1604   static_assert(get(arr, 0) == 4, ""); // expected-error{{not an integral constant expression}} \
1605   // expected-note{{in call to 'get(arr, 0)'}}
1606 }
1607 
1608 namespace std { struct type_info; }
1609 
1610 namespace TypeId {
1611   struct A { virtual ~A(); };
1612   A f();
1613   A &g();
1614   constexpr auto &x = typeid(f());
1615   constexpr auto &y = typeid(g()); // expected-error{{constant expression}} \
1616   // expected-note{{typeid applied to expression of polymorphic type 'TypeId::A' is not allowed in a constant expression}} \
1617   // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}
1618 }
1619 
1620 namespace PR14203 {
1621   struct duration {
durationPR14203::duration1622     constexpr duration() {}
operator intPR14203::duration1623     constexpr operator int() const { return 0; }
1624   };
f()1625   template<typename T> void f() {
1626     // If we want to evaluate this at the point of the template definition, we
1627     // need to trigger the implicit definition of the move constructor at that
1628     // point.
1629     // FIXME: C++ does not permit us to implicitly define it at the appropriate
1630     // times, since it is only allowed to be implicitly defined when it is
1631     // odr-used.
1632     constexpr duration d = duration();
1633   }
1634   // FIXME: It's unclear whether this is valid. On the one hand, we're not
1635   // allowed to generate a move constructor. On the other hand, if we did,
1636   // this would be a constant expression. For now, we generate a move
1637   // constructor here.
1638   int n = sizeof(short{duration(duration())});
1639 }
1640 
1641 namespace ArrayEltInit {
1642   struct A {
AArrayEltInit::A1643     constexpr A() : p(&p) {}
1644     void *p;
1645   };
1646   constexpr A a[10];
1647   static_assert(a[0].p == &a[0].p, "");
1648   static_assert(a[9].p == &a[9].p, "");
1649   static_assert(a[0].p != &a[9].p, "");
1650   static_assert(a[9].p != &a[0].p, "");
1651 
1652   constexpr A b[10] = {};
1653   static_assert(b[0].p == &b[0].p, "");
1654   static_assert(b[9].p == &b[9].p, "");
1655   static_assert(b[0].p != &b[9].p, "");
1656   static_assert(b[9].p != &b[0].p, "");
1657 }
1658 
1659 namespace PR15884 {
1660   struct S {};
f()1661   constexpr S f() { return {}; }
1662   constexpr S *p = &f();
1663   // expected-error@-1 {{taking the address of a temporary}}
1664   // expected-error@-2 {{constexpr variable 'p' must be initialized by a constant expression}}
1665   // expected-note@-3 {{pointer to temporary is not a constant expression}}
1666   // expected-note@-4 {{temporary created here}}
1667 }
1668 
1669 namespace AfterError {
1670   // FIXME: Suppress the 'no return statements' diagnostic if the body is invalid.
error()1671   constexpr int error() { // expected-error {{no return statement}}
1672     return foobar; // expected-error {{undeclared identifier}}
1673   }
1674   constexpr int k = error(); // expected-error {{must be initialized by a constant expression}}
1675 }
1676 
1677 namespace std {
1678   typedef decltype(sizeof(int)) size_t;
1679 
1680   template <class _E>
1681   class initializer_list
1682   {
1683     const _E* __begin_;
1684     size_t    __size_;
1685 
initializer_list(const _E * __b,size_t __s)1686     constexpr initializer_list(const _E* __b, size_t __s)
1687       : __begin_(__b),
1688         __size_(__s)
1689     {}
1690 
1691   public:
1692     typedef _E        value_type;
1693     typedef const _E& reference;
1694     typedef const _E& const_reference;
1695     typedef size_t    size_type;
1696 
1697     typedef const _E* iterator;
1698     typedef const _E* const_iterator;
1699 
initializer_list()1700     constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
1701 
size() const1702     constexpr size_t    size()  const {return __size_;}
begin() const1703     constexpr const _E* begin() const {return __begin_;}
end() const1704     constexpr const _E* end()   const {return __begin_ + __size_;}
1705   };
1706 }
1707 
1708 namespace InitializerList {
sum(const int * b,const int * e)1709   constexpr int sum(const int *b, const int *e) {
1710     return b != e ? *b + sum(b+1, e) : 0;
1711   }
sum(std::initializer_list<int> ints)1712   constexpr int sum(std::initializer_list<int> ints) {
1713     return sum(ints.begin(), ints.end());
1714   }
1715   static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
1716 
1717   static_assert(*std::initializer_list<int>{1, 2, 3}.begin() == 1, "");
1718   static_assert(std::initializer_list<int>{1, 2, 3}.begin()[2] == 3, "");
1719 }
1720 
1721 namespace StmtExpr {
1722   struct A { int k; };
f()1723   void f() {
1724     static_assert(({ const int x = 5; x * 3; }) == 15, ""); // expected-warning {{extension}}
1725     constexpr auto a = ({ A(); }); // expected-warning {{extension}}
1726   }
g(int k)1727   constexpr int g(int k) {
1728     return ({ // expected-warning {{extension}}
1729       const int x = k;
1730       x * x;
1731     });
1732   }
1733   static_assert(g(123) == 15129, "");
h()1734   constexpr int h() { // expected-error {{never produces a constant}}
1735     return ({ // expected-warning {{extension}}
1736       return 0; // expected-note {{not supported}}
1737       1;
1738     });
1739   }
1740 }
1741 
1742 namespace VirtualFromBase {
1743   struct S1 {
1744     virtual int f() const;
1745   };
1746   struct S2 {
1747     virtual int f();
1748   };
1749   template <typename T> struct X : T {
XVirtualFromBase::X1750     constexpr X() {}
1751     double d = 0.0;
fVirtualFromBase::X1752     constexpr int f() { return sizeof(T); } // expected-warning {{will not be implicitly 'const' in C++14}}
1753   };
1754 
1755   // Virtual f(), not OK.
1756   constexpr X<X<S1>> xxs1;
1757   constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
1758   static_assert(p->f() == sizeof(X<S1>), ""); // expected-error {{constant expression}} expected-note {{virtual function call}}
1759 
1760   // Non-virtual f(), OK.
1761   constexpr X<X<S2>> xxs2;
1762   constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
1763   static_assert(q->f() == sizeof(S2), "");
1764 }
1765 
1766 namespace ConstexprConstructorRecovery {
1767   class X {
1768   public:
1769       enum E : short {
1770           headers = 0x1,
1771           middlefile = 0x2,
1772           choices = 0x4
1773       };
X()1774       constexpr X() noexcept {};
1775   protected:
1776       E val{0}; // expected-error {{cannot initialize a member subobject of type 'ConstexprConstructorRecovery::X::E' with an rvalue of type 'int'}}
1777   };
1778   constexpr X x{};
1779 }
1780 
1781 namespace Lifetime {
f()1782   void f() {
1783     constexpr int &n = n; // expected-error {{constant expression}} expected-note {{use of reference outside its lifetime}} expected-warning {{not yet bound to a value}}
1784     constexpr int m = m; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
1785   }
1786 
get(int && n)1787   constexpr int &get(int &&n) { return n; }
1788   struct S {
1789     int &&r; // expected-note 2{{declared here}}
1790     int &s;
1791     int t;
SLifetime::S1792     constexpr S() : r(0), s(get(0)), t(r) {} // expected-warning {{temporary}}
SLifetime::S1793     constexpr S(int) : r(0), s(get(0)), t(s) {} // expected-warning {{temporary}} expected-note {{read of object outside its lifetime}}
1794   };
1795   constexpr int k1 = S().t; // ok, int is lifetime-extended to end of constructor
1796   constexpr int k2 = S(0).t; // expected-error {{constant expression}} expected-note {{in call}}
1797 }
1798 
1799 namespace Bitfields {
1800   struct A {
1801     bool b : 1;
1802     unsigned u : 5;
1803     int n : 5;
1804     bool b2 : 3;
1805     unsigned u2 : 74; // expected-warning {{exceeds the size of its type}}
1806     int n2 : 81; // expected-warning {{exceeds the size of its type}}
1807   };
1808 
1809   constexpr A a = { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}}
1810   static_assert(a.b == 0 && a.u == 1 && a.n == -1 && a.b2 == 0 &&
1811                 a.u2 + 1 == 0 && a.n2 == 0x7fffffff,
1812                 "bad truncation of bitfield values");
1813 
1814   struct B {
1815     int n : 3;
BBitfields::B1816     constexpr B(int k) : n(k) {}
1817   };
1818   static_assert(B(3).n == 3, "");
1819   static_assert(B(4).n == -4, "");
1820   static_assert(B(7).n == -1, "");
1821   static_assert(B(8).n == 0, "");
1822   static_assert(B(-1).n == -1, "");
1823   static_assert(B(-8889).n == -1, "");
1824 
1825   namespace PR16755 {
1826     struct X {
1827       int x : 1;
fBitfields::PR16755::X1828       constexpr static int f(int x) {
1829         return X{x}.x;
1830       }
1831     };
1832     static_assert(X::f(3) == -1, "3 should truncate to -1");
1833   }
1834 }
1835 
1836 namespace ZeroSizeTypes {
1837   constexpr int (*p1)[0] = 0, (*p2)[0] = 0;
1838   constexpr int k = p2 - p1;
1839   // expected-error@-1 {{constexpr variable 'k' must be initialized by a constant expression}}
1840   // expected-note@-2 {{subtraction of pointers to type 'int [0]' of zero size}}
1841 
1842   int arr[5][0];
f()1843   constexpr int f() { // expected-error {{never produces a constant expression}}
1844     return &arr[3] - &arr[0]; // expected-note {{subtraction of pointers to type 'int [0]' of zero size}}
1845   }
1846 }
1847 
1848 namespace BadDefaultInit {
1849   template<int N> struct X { static const int n = N; };
1850 
1851   struct A {
1852     int k = // expected-error {{cannot use defaulted default constructor of 'A' within the class outside of member functions because 'k' has an initializer}}
1853         X<A().k>::n; // expected-error {{not a constant expression}} expected-note {{implicit default constructor for 'BadDefaultInit::A' first required here}}
1854   };
1855 
1856   // FIXME: The "constexpr constructor must initialize all members" diagnostic
1857   // here is bogus (we discard the k(k) initializer because the parameter 'k'
1858   // has been marked invalid).
1859   struct B { // expected-note 2{{candidate}}
BBadDefaultInit::B1860     constexpr B( // expected-error {{must initialize all members}} expected-note {{candidate}}
1861         int k = X<B().k>::n) : // expected-error {{no matching constructor}}
1862       k(k) {}
1863     int k; // expected-note {{not initialized}}
1864   };
1865 }
1866 
1867 namespace NeverConstantTwoWays {
1868   // If we see something non-constant but foldable followed by something
1869   // non-constant and not foldable, we want the first diagnostic, not the
1870   // second.
f(int n)1871   constexpr int f(int n) { // expected-error {{never produces a constant expression}}
1872     return (int *)(long)&n == &n ? // expected-note {{reinterpret_cast}}
1873         1 / 0 : // expected-warning {{division by zero}}
1874         0;
1875   }
1876 
1877   // FIXME: We should diagnose the cast to long here, not the division by zero.
1878   constexpr int n = // expected-error {{must be initialized by a constant expression}}
1879       (int *)(long)&n == &n ?
1880         1 / 0 : // expected-warning {{division by zero}} expected-note {{division by zero}}
1881         0;
1882 }
1883 
1884 namespace PR17800 {
1885   struct A {
operator ()PR17800::A1886     constexpr int operator()() const { return 0; }
1887   };
sink(T...)1888   template <typename ...T> constexpr int sink(T ...) {
1889     return 0;
1890   }
run()1891   template <int ...N> constexpr int run() {
1892     return sink(A()() + N ...);
1893   }
1894   constexpr int k = run<1, 2, 3>();
1895 }
1896 
1897 namespace BuiltinStrlen {
1898   constexpr const char *a = "foo\0quux";
1899   constexpr char b[] = "foo\0quux";
f()1900   constexpr int f() { return 'u'; }
1901   constexpr char c[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 };
1902 
1903   static_assert(__builtin_strlen("foo") == 3, "");
1904   static_assert(__builtin_strlen("foo\0quux") == 3, "");
1905   static_assert(__builtin_strlen("foo\0quux" + 4) == 4, "");
1906 
check(const char * p)1907   constexpr bool check(const char *p) {
1908     return __builtin_strlen(p) == 3 &&
1909            __builtin_strlen(p + 1) == 2 &&
1910            __builtin_strlen(p + 2) == 1 &&
1911            __builtin_strlen(p + 3) == 0 &&
1912            __builtin_strlen(p + 4) == 4 &&
1913            __builtin_strlen(p + 5) == 3 &&
1914            __builtin_strlen(p + 6) == 2 &&
1915            __builtin_strlen(p + 7) == 1 &&
1916            __builtin_strlen(p + 8) == 0;
1917   }
1918 
1919   static_assert(check(a), "");
1920   static_assert(check(b), "");
1921   static_assert(check(c), "");
1922 
1923   constexpr int over1 = __builtin_strlen(a + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1924   constexpr int over2 = __builtin_strlen(b + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1925   constexpr int over3 = __builtin_strlen(c + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1926 
1927   constexpr int under1 = __builtin_strlen(a - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
1928   constexpr int under2 = __builtin_strlen(b - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
1929   constexpr int under3 = __builtin_strlen(c - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
1930 
1931   // FIXME: The diagnostic here could be better.
1932   constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator.
1933   constexpr int bad = __builtin_strlen(d); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
1934 }
1935 
1936 namespace PR19010 {
1937   struct Empty {};
1938   struct Empty2 : Empty {};
1939   struct Test : Empty2 {
TestPR19010::Test1940     constexpr Test() {}
1941     Empty2 array[2];
1942   };
test()1943   void test() { constexpr Test t; }
1944 }
1945 
PR21327(int a,int b)1946 void PR21327(int a, int b) {
1947   static_assert(&a + 1 != &b, ""); // expected-error {{constant expression}}
1948 }
1949 
1950 namespace EmptyClass {
1951   struct E1 {} e1;
1952   union E2 {} e2; // expected-note {{here}}
1953   struct E3 : E1 {} e3;
1954 
1955   // The defaulted copy constructor for an empty class does not read any
1956   // members. The defaulted copy constructor for an empty union reads the
1957   // object representation.
1958   constexpr E1 e1b(e1);
1959   constexpr E2 e2b(e2); // expected-error {{constant expression}} expected-note{{read of non-const}} expected-note {{in call}}
1960   constexpr E3 e3b(e3);
1961 }
1962 
1963 namespace PR21786 {
1964   extern void (*start[])();
1965   extern void (*end[])();
1966   static_assert(&start != &end, ""); // expected-error {{constant expression}}
1967   static_assert(&start != nullptr, "");
1968 
1969   struct Foo;
1970   struct Bar {
1971     static const Foo x;
1972     static const Foo y;
1973   };
1974   static_assert(&Bar::x != nullptr, "");
1975   static_assert(&Bar::x != &Bar::y, "");
1976 }
1977 
1978 namespace PR21859 {
Fun()1979   constexpr int Fun() { return; } // expected-error {{non-void constexpr function 'Fun' should return a value}}
1980   constexpr int Var = Fun(); // expected-error {{constexpr variable 'Var' must be initialized by a constant expression}}
1981 }
1982 
1983 struct InvalidRedef {
1984   int f; // expected-note{{previous definition is here}}
1985   constexpr int f(void); // expected-error{{redefinition of 'f'}} expected-warning{{will not be implicitly 'const'}}
1986 };
1987