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_assert 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, "");
1600 static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}}
1601
1602 // Other kinds are not.
1603 struct X { int a[2]; };
1604 constexpr int *n = (X){1, 2}.a; // expected-warning {{C99}} expected-warning {{temporary}}
1605 // expected-error@-1 {{constant expression}}
1606 // expected-note@-2 {{pointer to subobject of temporary}}
1607 // expected-note@-3 {{temporary created here}}
1608
f()1609 void f() {
1610 static constexpr int *p = (int*)(int[1]){3}; // expected-warning {{C99}} expected-warning {{temporary}}
1611 // expected-error@-1 {{constant expression}}
1612 // expected-note@-2 {{pointer to subobject of temporary}}
1613 // expected-note@-3 {{temporary created here}}
1614 static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}}
1615 }
1616 }
1617
1618 namespace Vector {
1619 typedef int __attribute__((vector_size(16))) VI4;
f(int n)1620 constexpr VI4 f(int n) {
1621 return VI4 { n * 3, n + 4, n - 5, n / 6 };
1622 }
1623 constexpr auto v1 = f(10);
1624
1625 typedef double __attribute__((vector_size(32))) VD4;
g(int n)1626 constexpr VD4 g(int n) {
1627 return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 }; // expected-warning {{C99}}
1628 }
1629 constexpr auto v2 = g(4);
1630 }
1631
1632 // PR12626, redux
1633 namespace InvalidClasses {
test0()1634 void test0() {
1635 struct X; // expected-note {{forward declaration}}
1636 struct Y { bool b; X x; }; // expected-error {{field has incomplete type}}
1637 Y y;
1638 auto& b = y.b;
1639 }
1640 }
1641
1642 namespace NamespaceAlias {
f()1643 constexpr int f() {
1644 namespace NS = NamespaceAlias; // cxx11-warning {{use of this statement in a constexpr function is a C++14 extension}}
1645 return &NS::f != nullptr;
1646 }
1647 }
1648
1649 // Constructors can be implicitly constexpr, even for a non-literal type.
1650 namespace ImplicitConstexpr {
1651 struct Q { Q() = default; Q(const Q&) = default; Q(Q&&) = default; ~Q(); }; // expected-note 3{{here}}
1652 struct R { constexpr R() noexcept; constexpr R(const R&) noexcept; constexpr R(R&&) noexcept; ~R() noexcept; };
1653 struct S { R r; }; // expected-note 3{{here}}
1654 struct T { T(const T&) noexcept; T(T &&) noexcept; ~T() noexcept; };
1655 struct U { T t; }; // expected-note 3{{here}}
1656 static_assert(!__is_literal_type(Q), "");
1657 static_assert(!__is_literal_type(R), "");
1658 static_assert(!__is_literal_type(S), "");
1659 static_assert(!__is_literal_type(T), "");
1660 static_assert(!__is_literal_type(U), "");
1661 struct Test {
1662 friend Q::Q() noexcept; // expected-error {{follows constexpr}}
1663 friend Q::Q(Q&&) noexcept; // expected-error {{follows constexpr}}
1664 friend Q::Q(const Q&) noexcept; // expected-error {{follows constexpr}}
1665 friend S::S() noexcept; // expected-error {{follows constexpr}}
1666 friend S::S(S&&) noexcept; // expected-error {{follows constexpr}}
1667 friend S::S(const S&) noexcept; // expected-error {{follows constexpr}}
1668 friend constexpr U::U() noexcept; // expected-error {{follows non-constexpr}}
1669 friend constexpr U::U(U&&) noexcept; // expected-error {{follows non-constexpr}}
1670 friend constexpr U::U(const U&) noexcept; // expected-error {{follows non-constexpr}}
1671 };
1672 }
1673
1674 // Indirectly test that an implicit lvalue to xvalue conversion performed for
1675 // an NRVO move operation isn't implemented as CK_LValueToRValue.
1676 namespace PR12826 {
1677 struct Foo {};
id(Foo x)1678 constexpr Foo id(Foo x) { return x; }
1679 constexpr Foo res(id(Foo()));
1680 }
1681
1682 namespace PR13273 {
1683 struct U {
1684 int t;
1685 U() = default;
1686 };
1687
1688 struct S : U {
1689 S() = default;
1690 };
1691
1692 // S's default constructor isn't constexpr, because U's default constructor
1693 // doesn't initialize 't', but it's trivial, so value-initialization doesn't
1694 // actually call it.
1695 static_assert(S{}.t == 0, "");
1696 }
1697
1698 namespace PR12670 {
1699 struct S {
SPR12670::S1700 constexpr S(int a0) : m(a0) {}
SPR12670::S1701 constexpr S() : m(6) {}
1702 int m;
1703 };
1704 constexpr S x[3] = { {4}, 5 };
1705 static_assert(x[0].m == 4, "");
1706 static_assert(x[1].m == 5, "");
1707 static_assert(x[2].m == 6, "");
1708 }
1709
1710 // Indirectly test that an implicit lvalue-to-rvalue conversion is performed
1711 // when a conditional operator has one argument of type void and where the other
1712 // is a glvalue of class type.
1713 namespace ConditionalLValToRVal {
1714 struct A {
AConditionalLValToRVal::A1715 constexpr A(int a) : v(a) {}
1716 int v;
1717 };
1718
f(const A & a)1719 constexpr A f(const A &a) {
1720 return a.v == 0 ? throw a : a;
1721 }
1722
1723 constexpr A a(4);
1724 static_assert(f(a).v == 4, "");
1725 }
1726
1727 namespace TLS {
1728 __thread int n;
1729 int m;
1730
1731 constexpr bool b = &n == &n;
1732
1733 constexpr int *p = &n; // expected-error{{constexpr variable 'p' must be initialized by a constant expression}}
1734
f()1735 constexpr int *f() { return &n; }
1736 constexpr int *q = f(); // expected-error{{constexpr variable 'q' must be initialized by a constant expression}}
1737 constexpr bool c = f() == f();
1738
g()1739 constexpr int *g() { return &m; }
1740 constexpr int *r = g();
1741 }
1742
1743 namespace Void {
f()1744 constexpr void f() { return; } // cxx11-error{{constexpr function's return type 'void' is not a literal type}}
1745
1746 void assert_failed(const char *msg, const char *file, int line); // expected-note {{declared here}}
1747 #define ASSERT(expr) ((expr) ? static_cast<void>(0) : assert_failed(#expr, __FILE__, __LINE__))
1748 template<typename T, size_t S>
get(T (& a)[S],size_t k)1749 constexpr T get(T (&a)[S], size_t k) {
1750 return ASSERT(k > 0 && k < S), a[k]; // expected-note{{non-constexpr function 'assert_failed'}}
1751 }
1752 #undef ASSERT
1753 template int get(int (&a)[4], size_t);
1754 constexpr int arr[] = { 4, 1, 2, 3, 4 };
1755 static_assert(get(arr, 1) == 1, "");
1756 static_assert(get(arr, 4) == 4, "");
1757 static_assert(get(arr, 0) == 4, ""); // expected-error{{not an integral constant expression}} \
1758 // expected-note{{in call to 'get(arr, 0)'}}
1759 }
1760
1761 namespace std { struct type_info; }
1762
1763 namespace TypeId {
1764 struct A { virtual ~A(); };
1765 A f();
1766 A &g(); // cxx20_2b-note {{declared here}}
1767 constexpr auto &x = typeid(f());
1768 constexpr auto &y = typeid(g()); // expected-error{{constant expression}}
1769 // cxx11-note@-1 {{typeid applied to expression of polymorphic type 'TypeId::A' is not allowed in a constant expression}}
1770 // expected-warning@-2 {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}
1771 // cxx20_2b-note@-3 {{non-constexpr function 'g' cannot be used in a constant expression}}
1772 }
1773
1774 namespace PR14203 {
1775 struct duration {
durationPR14203::duration1776 constexpr duration() {}
operator intPR14203::duration1777 constexpr operator int() const { return 0; }
1778 };
1779 // These are valid per P0859R0 (moved as DR).
f()1780 template<typename T> void f() {
1781 constexpr duration d = duration();
1782 }
1783 int n = sizeof(short{duration(duration())});
1784 }
1785
1786 namespace ArrayEltInit {
1787 struct A {
AArrayEltInit::A1788 constexpr A() : p(&p) {}
1789 void *p;
1790 };
1791 constexpr A a[10];
1792 static_assert(a[0].p == &a[0].p, "");
1793 static_assert(a[9].p == &a[9].p, "");
1794 static_assert(a[0].p != &a[9].p, "");
1795 static_assert(a[9].p != &a[0].p, "");
1796
1797 constexpr A b[10] = {};
1798 static_assert(b[0].p == &b[0].p, "");
1799 static_assert(b[9].p == &b[9].p, "");
1800 static_assert(b[0].p != &b[9].p, "");
1801 static_assert(b[9].p != &b[0].p, "");
1802 }
1803
1804 namespace PR15884 {
1805 struct S {};
f()1806 constexpr S f() { return {}; }
1807 constexpr S *p = &f();
1808 // expected-error@-1 {{taking the address of a temporary}}
1809 // expected-error@-2 {{constexpr variable 'p' must be initialized by a constant expression}}
1810 // expected-note@-3 {{pointer to temporary is not a constant expression}}
1811 // expected-note@-4 {{temporary created here}}
1812 }
1813
1814 namespace AfterError {
error()1815 constexpr int error() {
1816 return foobar; // expected-error {{undeclared identifier}}
1817 }
1818 constexpr int k = error(); // expected-error {{constexpr variable 'k' must be initialized by a constant expression}}
1819 }
1820
1821 namespace std {
1822 typedef decltype(sizeof(int)) size_t;
1823
1824 template <class _E>
1825 class initializer_list
1826 {
1827 const _E* __begin_;
1828 size_t __size_;
1829
initializer_list(const _E * __b,size_t __s)1830 constexpr initializer_list(const _E* __b, size_t __s)
1831 : __begin_(__b),
1832 __size_(__s)
1833 {}
1834
1835 public:
1836 typedef _E value_type;
1837 typedef const _E& reference;
1838 typedef const _E& const_reference;
1839 typedef size_t size_type;
1840
1841 typedef const _E* iterator;
1842 typedef const _E* const_iterator;
1843
initializer_list()1844 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
1845
size() const1846 constexpr size_t size() const {return __size_;}
begin() const1847 constexpr const _E* begin() const {return __begin_;}
end() const1848 constexpr const _E* end() const {return __begin_ + __size_;}
1849 };
1850 }
1851
1852 namespace InitializerList {
sum(const int * b,const int * e)1853 constexpr int sum(const int *b, const int *e) {
1854 return b != e ? *b + sum(b+1, e) : 0;
1855 }
sum(std::initializer_list<int> ints)1856 constexpr int sum(std::initializer_list<int> ints) {
1857 return sum(ints.begin(), ints.end());
1858 }
1859 static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
1860
1861 static_assert(*std::initializer_list<int>{1, 2, 3}.begin() == 1, "");
1862 static_assert(std::initializer_list<int>{1, 2, 3}.begin()[2] == 3, "");
1863
1864 namespace DR2126 {
1865 constexpr std::initializer_list<float> il = {1.0, 2.0, 3.0};
1866 static_assert(il.begin()[1] == 2.0, "");
1867 }
1868 }
1869
1870 namespace StmtExpr {
1871 struct A { int k; };
f()1872 void f() {
1873 static_assert(({ const int x = 5; x * 3; }) == 15, ""); // expected-warning {{extension}}
1874 constexpr auto a = ({ A(); }); // expected-warning {{extension}}
1875 }
g(int k)1876 constexpr int g(int k) {
1877 return ({ // expected-warning {{extension}}
1878 const int x = k;
1879 x * x;
1880 });
1881 }
1882 static_assert(g(123) == 15129, "");
h()1883 constexpr int h() { // expected-error {{never produces a constant}}
1884 return ({ // expected-warning {{extension}}
1885 return 0; // expected-note {{not supported}}
1886 1;
1887 });
1888 }
1889 }
1890
1891 namespace VirtualFromBase {
1892 struct S1 {
1893 virtual int f() const;
1894 };
1895 struct S2 {
1896 virtual int f();
1897 };
1898 template <typename T> struct X : T {
XVirtualFromBase::X1899 constexpr X() {}
1900 double d = 0.0;
fVirtualFromBase::X1901 constexpr int f() { return sizeof(T); } // cxx11-warning {{will not be implicitly 'const' in C++14}}
1902 };
1903
1904 // Virtual f(), not OK.
1905 constexpr X<X<S1>> xxs1;
1906 constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1);
1907 static_assert(p->f() == sizeof(X<S1>), "");
1908 // cxx11-error@-1 {{not an integral constant expression}}
1909 // cxx11-note@-2 {{call to virtual function}}
1910 // cxx20_2b-error@-3 {{static_assert failed}}
1911
1912 // Non-virtual f(), OK.
1913 constexpr X<X<S2>> xxs2;
1914 constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2);
1915 static_assert(q->f() == sizeof(S2), ""); // cxx20_2b-error {{static_assert failed}}
1916 }
1917
1918 namespace ConstexprConstructorRecovery {
1919 class X {
1920 public:
1921 enum E : short {
1922 headers = 0x1,
1923 middlefile = 0x2,
1924 choices = 0x4
1925 };
X()1926 constexpr X() noexcept {};
1927 protected:
1928 E val{0}; // cxx11-error {{cannot initialize a member subobject of type 'ConstexprConstructorRecovery::X::E' with an rvalue of type 'int'}} cxx11-note {{here}}
1929 };
1930 // FIXME: We should avoid issuing this follow-on diagnostic.
1931 constexpr X x{}; // cxx11-error {{constant expression}} cxx11-note {{not initialized}}
1932 }
1933
1934 namespace Lifetime {
f()1935 void f() {
1936 constexpr int &n = n; // expected-error {{constant expression}} expected-note {{use of reference outside its lifetime}} expected-warning {{not yet bound to a value}}
1937 constexpr int m = m; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
1938 }
1939
get(int && n)1940 constexpr int &get(int &&n) { return n; }
1941 // cxx2b-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
1942 // cxx2b-error@-2 {{no return statement in constexpr function}} See PR40598
get_rv(int && n)1943 constexpr int &&get_rv(int &&n) { return static_cast<int&&>(n); }
1944 struct S {
1945 int &&r;
1946 int &s;
1947 int t;
SLifetime::S1948 constexpr S() : r(get_rv(0)), s(get(0)), t(r) {} // expected-note {{read of object outside its lifetime}}
SLifetime::S1949 constexpr S(int) : r(get_rv(0)), s(get(0)), t(s) {}
1950 // cxx2b-warning@-1 {{reference 's' is not yet bound to a value when used here}}
1951 // cxx2b-note@-2 {{read of uninitialized object is not allowed in a constant expression}}
1952 // cxx11_20-note@-3 {{read of object outside its lifetime}}
1953 };
1954 constexpr int k1 = S().t; // expected-error {{constant expression}} expected-note {{in call}}
1955 constexpr int k2 = S(0).t; // expected-error {{constant expression}} expected-note {{in call}}
1956
1957 struct Q {
1958 int n = 0;
fLifetime::Q1959 constexpr int f() const { return 0; }
1960 };
out_of_lifetime(Q q)1961 constexpr Q *out_of_lifetime(Q q) { return &q; } // expected-warning {{address of stack}}
1962 constexpr int k3 = out_of_lifetime({})->n; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
1963 constexpr int k4 = out_of_lifetime({})->f(); // expected-error {{constant expression}} expected-note {{member call on object outside its lifetime}}
1964
1965 constexpr int null = ((Q*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced null pointer}}
1966
1967 Q q;
1968 Q qa[3];
1969 constexpr int pte0 = (&q)[0].f(); // ok
1970 constexpr int pte1 = (&q)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
1971 constexpr int pte2 = qa[2].f(); // ok
1972 constexpr int pte3 = qa[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
1973
1974 constexpr Q cq;
1975 constexpr Q cqa[3];
1976 constexpr int cpte0 = (&cq)[0].f(); // ok
1977 constexpr int cpte1 = (&cq)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
1978 constexpr int cpte2 = cqa[2].f(); // ok
1979 constexpr int cpte3 = cqa[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
1980
1981 // FIXME: There's no way if we can tell if the first call here is valid; it
1982 // depends on the active union member. Should we reject for that reason?
1983 union U {
1984 int n;
1985 Q q;
1986 };
1987 U u1 = {0};
1988 constexpr U u2 = {0};
1989 constexpr int union_member1 = u1.q.f();
1990 constexpr int union_member2 = u2.q.f(); // expected-error {{constant expression}} expected-note {{member call on member 'q' of union with active member 'n'}}
1991
1992 struct R { // expected-note {{field init}}
fLifetime::R::Inner1993 struct Inner { constexpr int f() const { return 0; } };
1994 int a = b.f(); // expected-warning {{uninitialized}} expected-note 2{{member call on object outside its lifetime}}
1995 Inner b;
1996 };
1997 constexpr R r; // expected-error {{constant expression}} expected-note {{in call}} expected-note {{implicit default constructor for 'Lifetime::R' first required here}}
rf()1998 void rf() {
1999 constexpr R r; // expected-error {{constant expression}} expected-note {{in call}}
2000 }
2001 }
2002
2003 namespace Bitfields {
2004 struct A {
2005 bool b : 1;
2006 unsigned u : 5;
2007 int n : 5;
2008 bool b2 : 3;
2009 unsigned u2 : 74; // expected-warning {{exceeds the width of its type}}
2010 int n2 : 81; // expected-warning {{exceeds the width of its type}}
2011 };
2012
2013 constexpr A a = { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}}
2014 static_assert(a.b == 0 && a.u == 1 && a.n == -1 && a.b2 == 0 &&
2015 a.u2 + 1 == 0 && a.n2 == 0x7fffffff,
2016 "bad truncation of bitfield values");
2017
2018 struct B {
2019 int n : 3;
BBitfields::B2020 constexpr B(int k) : n(k) {}
2021 };
2022 static_assert(B(3).n == 3, "");
2023 static_assert(B(4).n == -4, "");
2024 static_assert(B(7).n == -1, "");
2025 static_assert(B(8).n == 0, "");
2026 static_assert(B(-1).n == -1, "");
2027 static_assert(B(-8889).n == -1, "");
2028
2029 namespace PR16755 {
2030 struct X {
2031 int x : 1;
fBitfields::PR16755::X2032 constexpr static int f(int x) {
2033 return X{x}.x;
2034 }
2035 };
2036 static_assert(X::f(3) == -1, "3 should truncate to -1");
2037 }
2038
2039 struct HasUnnamedBitfield {
2040 unsigned a;
2041 unsigned : 20;
2042 unsigned b;
2043
HasUnnamedBitfieldBitfields::HasUnnamedBitfield2044 constexpr HasUnnamedBitfield() : a(), b() {}
HasUnnamedBitfieldBitfields::HasUnnamedBitfield2045 constexpr HasUnnamedBitfield(unsigned a, unsigned b) : a(a), b(b) {}
2046 };
2047
testUnnamedBitfield()2048 void testUnnamedBitfield() {
2049 const HasUnnamedBitfield zero{};
2050 int a = 1 / zero.b; // expected-warning {{division by zero is undefined}}
2051 const HasUnnamedBitfield oneZero{1, 0};
2052 int b = 1 / oneZero.b; // expected-warning {{division by zero is undefined}}
2053 }
2054
2055 union UnionWithUnnamedBitfield {
2056 int : 3;
2057 int n;
2058 };
2059 static_assert(UnionWithUnnamedBitfield().n == 0, "");
2060 static_assert(UnionWithUnnamedBitfield{}.n == 0, "");
2061 static_assert(UnionWithUnnamedBitfield{1}.n == 1, "");
2062 }
2063
2064 namespace ZeroSizeTypes {
2065 constexpr int (*p1)[0] = 0, (*p2)[0] = 0;
2066 constexpr int k = p2 - p1;
2067 // expected-error@-1 {{constexpr variable 'k' must be initialized by a constant expression}}
2068 // expected-note@-2 {{subtraction of pointers to type 'int [0]' of zero size}}
2069
2070 int arr[5][0];
f()2071 constexpr int f() { // expected-error {{never produces a constant expression}}
2072 return &arr[3] - &arr[0]; // expected-note {{subtraction of pointers to type 'int [0]' of zero size}}
2073 }
2074 }
2075
2076 namespace BadDefaultInit {
2077 template<int N> struct X { static const int n = N; };
2078
2079 struct A { // expected-error {{default member initializer for 'k' needed within definition of enclosing class}}
2080 int k = // expected-note {{default member initializer declared here}}
2081 X<A().k>::n; // expected-note {{in evaluation of exception specification for 'BadDefaultInit::A::A' needed here}}
2082 };
2083
2084 struct B {
BBadDefaultInit::B2085 constexpr B(
2086 int k = X<B().k>::n) : // expected-error {{default argument to function 'B' that is declared later}} expected-note {{here}}
2087 k(k) {}
2088 int k;
2089 };
2090 }
2091
2092 namespace NeverConstantTwoWays {
2093 // If we see something non-constant but foldable followed by something
2094 // non-constant and not foldable, we want the first diagnostic, not the
2095 // second.
f(int n)2096 constexpr int f(int n) { // expected-error {{never produces a constant expression}}
2097 return (int *)(long)&n == &n ? // expected-note {{reinterpret_cast}}
2098 1 / 0 : // expected-warning {{division by zero}}
2099 0;
2100 }
2101
2102 constexpr int n = // expected-error {{must be initialized by a constant expression}}
2103 (int *)(long)&n == &n ? // expected-note {{reinterpret_cast}}
2104 1 / 0 :
2105 0;
2106 }
2107
2108 namespace PR17800 {
2109 struct A {
operator ()PR17800::A2110 constexpr int operator()() const { return 0; }
2111 };
sink(T...)2112 template <typename ...T> constexpr int sink(T ...) {
2113 return 0;
2114 }
run()2115 template <int ...N> constexpr int run() {
2116 return sink(A()() + N ...);
2117 }
2118 constexpr int k = run<1, 2, 3>();
2119 }
2120
2121 namespace BuiltinStrlen {
2122 constexpr const char *a = "foo\0quux";
2123 constexpr char b[] = "foo\0quux";
f()2124 constexpr int f() { return 'u'; }
2125 constexpr char c[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 };
2126
2127 static_assert(__builtin_strlen("foo") == 3, "");
2128 static_assert(__builtin_strlen("foo\0quux") == 3, "");
2129 static_assert(__builtin_strlen("foo\0quux" + 4) == 4, "");
2130
check(const char * p)2131 constexpr bool check(const char *p) {
2132 return __builtin_strlen(p) == 3 &&
2133 __builtin_strlen(p + 1) == 2 &&
2134 __builtin_strlen(p + 2) == 1 &&
2135 __builtin_strlen(p + 3) == 0 &&
2136 __builtin_strlen(p + 4) == 4 &&
2137 __builtin_strlen(p + 5) == 3 &&
2138 __builtin_strlen(p + 6) == 2 &&
2139 __builtin_strlen(p + 7) == 1 &&
2140 __builtin_strlen(p + 8) == 0;
2141 }
2142
2143 static_assert(check(a), "");
2144 static_assert(check(b), "");
2145 static_assert(check(c), "");
2146
2147 constexpr int over1 = __builtin_strlen(a + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2148 constexpr int over2 = __builtin_strlen(b + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2149 constexpr int over3 = __builtin_strlen(c + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2150
2151 constexpr int under1 = __builtin_strlen(a - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2152 constexpr int under2 = __builtin_strlen(b - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2153 constexpr int under3 = __builtin_strlen(c - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2154
2155 // FIXME: The diagnostic here could be better.
2156 constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator.
2157 constexpr int bad = __builtin_strlen(d); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2158 }
2159
2160 namespace PR19010 {
2161 struct Empty {};
2162 struct Empty2 : Empty {};
2163 struct Test : Empty2 {
TestPR19010::Test2164 constexpr Test() {}
2165 Empty2 array[2];
2166 };
test()2167 void test() { constexpr Test t; }
2168 }
2169
PR21327(int a,int b)2170 void PR21327(int a, int b) {
2171 static_assert(&a + 1 != &b, ""); // expected-error {{constant expression}}
2172 }
2173
2174 namespace EmptyClass {
2175 struct E1 {} e1;
2176 union E2 {} e2; // expected-note {{here}}
2177 struct E3 : E1 {} e3;
2178
2179 // The defaulted copy constructor for an empty class does not read any
2180 // members. The defaulted copy constructor for an empty union reads the
2181 // object representation.
2182 constexpr E1 e1b(e1);
2183 constexpr E2 e2b(e2); // expected-error {{constant expression}} expected-note{{read of non-const}} expected-note {{in call}}
2184 constexpr E3 e3b(e3);
2185 }
2186
2187 namespace PR21786 {
2188 extern void (*start[])();
2189 extern void (*end[])();
2190 static_assert(&start != &end, ""); // expected-error {{constant expression}}
2191 static_assert(&start != nullptr, "");
2192
2193 struct Foo;
2194 struct Bar {
2195 static const Foo x;
2196 static const Foo y;
2197 };
2198 static_assert(&Bar::x != nullptr, "");
2199 static_assert(&Bar::x != &Bar::y, "");
2200 }
2201
2202 namespace PR21859 {
Fun()2203 constexpr int Fun() { return; } // expected-error {{non-void constexpr function 'Fun' should return a value}}
2204 constexpr int Var = Fun();
2205
FunT1()2206 template <typename T> constexpr int FunT1() { return; } // expected-error {{non-void constexpr function 'FunT1' should return a value}}
FunT2()2207 template <typename T> constexpr int FunT2() { return 0; }
FunT2()2208 template <> constexpr int FunT2<double>() { return 0; }
FunT2()2209 template <> constexpr int FunT2<int>() { return; } // expected-error {{non-void constexpr function 'FunT2<int>' should return a value}}
2210 }
2211
2212 struct InvalidRedef {
2213 int f; // expected-note{{previous definition is here}}
2214 constexpr int f(void); // expected-error{{redefinition of 'f'}} cxx11-warning{{will not be implicitly 'const'}}
2215 };
2216
2217 namespace PR17938 {
f(T const & x)2218 template <typename T> constexpr T const &f(T const &x) { return x; }
2219
2220 struct X {};
2221 struct Y : X {};
ZPR17938::Z2222 struct Z : Y { constexpr Z() {} };
2223
2224 static constexpr auto z = f(Z());
2225 }
2226
2227 namespace PR24597 {
2228 struct A {
2229 int x, *p;
APR24597::A2230 constexpr A() : x(0), p(&x) {}
APR24597::A2231 constexpr A(const A &a) : x(a.x), p(&x) {}
2232 };
f()2233 constexpr A f() { return A(); }
g()2234 constexpr A g() { return f(); }
2235 constexpr int a = *f().p;
2236 constexpr int b = *g().p;
2237 }
2238
2239 namespace IncompleteClass {
2240 struct XX {
fIncompleteClass::XX2241 static constexpr int f(XX*) { return 1; } // expected-note {{here}}
g(XX *)2242 friend constexpr int g(XX*) { return 2; } // expected-note {{here}}
2243
2244 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}}
2245 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}}
2246 };
2247 }
2248
2249 namespace InheritedCtor {
AInheritedCtor::A2250 struct A { constexpr A(int) {} };
2251
2252 struct B : A { int n; using A::A; }; // expected-note {{here}}
2253 constexpr B b(0); // expected-error {{constant expression}} expected-note {{derived class}}
2254
2255 struct C : A { using A::A; struct { union { int n, m = 0; }; union { int a = 0; }; int k = 0; }; struct {}; union {}; }; // expected-warning 6{{}}
2256 constexpr C c(0);
2257
2258 struct D : A {
2259 using A::A; // cxx11-note {{here}}
2260 struct { // expected-warning {{extension}}
2261 union { // expected-warning {{extension}}
2262 int n;
2263 };
2264 };
2265 };
2266 constexpr D d(0); // cxx11-error {{constant expression}} cxx11-note {{derived class}}
2267
2268 struct E : virtual A { using A::A; }; // expected-note {{here}}
2269 // cxx20_2b-note@-1 {{struct with virtual base class is not a literal type}}
2270 // We wrap a function around this to avoid implicit zero-initialization
2271 // happening first; the zero-initialization step would produce the same
2272 // error and defeat the point of this test.
f()2273 void f() {
2274 constexpr E e(0); // cxx11-error {{constant expression}} cxx11-note {{derived class}}
2275 // cxx20_2b-error@-1 {{constexpr variable cannot have non-literal type}}
2276 }
2277 // FIXME: This produces a note with no source location.
2278 //constexpr E e(0);
2279
WInheritedCtor::W2280 struct W { constexpr W(int n) : w(n) {} int w; };
2281 struct X : W { using W::W; int x = 2; };
2282 struct Y : X { using X::X; int y = 3; };
2283 struct Z : Y { using Y::Y; int z = 4; };
2284 constexpr Z z(1);
2285 static_assert(z.w == 1 && z.x == 2 && z.y == 3 && z.z == 4, "");
2286 }
2287
2288
2289 namespace PR28366 {
2290 namespace ns1 {
2291
f(char c)2292 void f(char c) { //expected-note2{{declared here}}
2293 struct X {
2294 static constexpr char f() { //expected-error{{never produces a constant expression}}
2295 return c; //expected-error{{reference to local}} expected-note{{function parameter}}
2296 }
2297 };
2298 int I = X::f();
2299 }
2300
g()2301 void g() {
2302 const int c = 'c';
2303 static const int d = 'd';
2304 struct X {
2305 static constexpr int f() {
2306 return c + d;
2307 }
2308 };
2309 static_assert(X::f() == 'c' + 'd',"");
2310 }
2311
2312
2313 } // end ns1
2314
2315 } //end ns PR28366
2316
2317 namespace PointerArithmeticOverflow {
2318 int n;
2319 int a[1];
2320 constexpr int *b = &n + 1 + (long)-1;
2321 constexpr int *c = &n + 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}}
2322 constexpr int *d = &n + 1 - (unsigned long)1;
2323 constexpr int *e = a + 1 + (long)-1;
2324 constexpr int *f = a + 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}}
2325 constexpr int *g = a + 1 - (unsigned long)1;
2326
2327 constexpr int *p = (&n + 1) + (unsigned __int128)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}}
2328 constexpr int *q = (&n + 1) - (unsigned __int128)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element -3402}}
2329 constexpr int *r = &(&n + 1)[(unsigned __int128)-1]; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}}
2330 }
2331
2332 namespace PR40430 {
2333 struct S {
2334 char c[10] = "asdf";
fooPR40430::S2335 constexpr char foo() const { return c[3]; }
2336 };
2337 static_assert(S().foo() == 'f', "");
2338 }
2339
2340 namespace PR41854 {
2341 struct e { operator int(); };
2342 struct f { e c; };
2343 int a;
2344 f &d = reinterpret_cast<f&>(a);
2345 unsigned b = d.c;
2346 }
2347
2348 namespace array_size {
2349 template<int N> struct array {
sizearray_size::array2350 static constexpr int size() { return N; }
2351 };
f1(T t)2352 template<typename T> void f1(T t) {
2353 constexpr int k = t.size();
2354 }
f2(const T & t)2355 template<typename T> void f2(const T &t) { // expected-note 2{{declared here}}
2356 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}}
2357 }
f3(const T & t)2358 template<typename T> void f3(const T &t) {
2359 constexpr int k = T::size();
2360 }
g(array<3> a)2361 void g(array<3> a) {
2362 f1(a);
2363 f2(a); // expected-note {{instantiation of}}
2364 f3(a);
2365 }
2366
2367 template<int N> struct array_nonstatic {
sizearray_size::array_nonstatic2368 constexpr int size() const { return N; }
2369 };
h(array_nonstatic<3> a)2370 void h(array_nonstatic<3> a) {
2371 f1(a);
2372 f2(a); // expected-note {{instantiation of}}
2373 }
2374 }
2375
2376 namespace flexible_array {
2377 struct A { int x; char arr[]; }; // expected-warning {{C99}} expected-note {{here}}
2378 constexpr A a = {1};
2379 static_assert(a.x == 1, "");
2380 static_assert(&a.arr != nullptr, "");
2381 static_assert(a.arr[0], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2382 static_assert(a.arr[1], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2383
2384 constexpr A b[] = {{1}, {2}, {3}}; // expected-warning {{flexible array member}}
2385 static_assert(b[0].x == 1, "");
2386 static_assert(b[1].x == 2, "");
2387 static_assert(b[2].x == 3, "");
2388 static_assert(b[2].arr[0], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2389
2390 // If we ever start to accept this, we'll need to ensure we can
2391 // constant-evaluate it properly.
2392 constexpr A c = {1, 2, 3}; // expected-error {{initialization of flexible array member}}
2393 }
2394
local_constexpr_var()2395 void local_constexpr_var() {
2396 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}}
2397 constexpr const int *p = &a; // expected-error {{constant expression}} expected-note {{pointer to 'a' is not a constant expression}}
2398 }
2399