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