constant-expression-cxx11.cpp revision 7ead5c7b6fd48cf549e55b4db499c26ecf88ae75
1// RUN: %clang_cc1 -triple i686-linux -fsyntax-only -verify -std=c++11 -pedantic %s -Wno-comment
2
3namespace StaticAssertFoldTest {
4
5int x;
6static_assert(++x, "test"); // expected-error {{not an integral constant expression}}
7static_assert(false, "test"); // expected-error {{test}}
8
9}
10
11typedef decltype(sizeof(char)) size_t;
12
13template<typename T> constexpr T id(const T &t) { return t; }
14template<typename T> constexpr T min(const T &a, const T &b) {
15  return a < b ? a : b;
16}
17template<typename T> constexpr T max(const T &a, const T &b) {
18  return a < b ? b : a;
19}
20template<typename T, size_t N> constexpr T *begin(T (&xs)[N]) { return xs; }
21template<typename T, size_t N> constexpr T *end(T (&xs)[N]) { return xs + N; }
22
23struct MemberZero {
24  constexpr int zero() { return 0; }
25};
26
27namespace DerivedToVBaseCast {
28
29  struct U { int n; };
30  struct V : U { int n; };
31  struct A : virtual V { int n; };
32  struct Aa { int n; };
33  struct B : virtual A, Aa {};
34  struct C : virtual A, Aa {};
35  struct D : B, C {};
36
37  D d;
38  constexpr B *p = &d;
39  constexpr C *q = &d;
40  static_assert((void*)p != (void*)q, "");
41  static_assert((A*)p == (A*)q, "");
42  static_assert((Aa*)p != (Aa*)q, "");
43
44  constexpr B &pp = d;
45  constexpr C &qq = d;
46  static_assert((void*)&pp != (void*)&qq, "");
47  static_assert(&(A&)pp == &(A&)qq, "");
48  static_assert(&(Aa&)pp != &(Aa&)qq, "");
49
50  constexpr V *v = p;
51  constexpr V *w = q;
52  constexpr V *x = (A*)p;
53  static_assert(v == w, "");
54  static_assert(v == x, "");
55
56  static_assert((U*)&d == p, "");
57  static_assert((U*)&d == q, "");
58  static_assert((U*)&d == v, "");
59  static_assert((U*)&d == w, "");
60  static_assert((U*)&d == x, "");
61
62  struct X {};
63  struct Y1 : virtual X {};
64  struct Y2 : X {};
65  struct Z : Y1, Y2 {};
66  Z z;
67  static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
68
69}
70
71namespace ConstCast {
72
73constexpr int n1 = 0;
74constexpr int n2 = const_cast<int&>(n1);
75constexpr int *n3 = const_cast<int*>(&n1);
76constexpr int n4 = *const_cast<int*>(&n1);
77constexpr const int * const *n5 = const_cast<const int* const*>(&n3);
78constexpr int **n6 = const_cast<int**>(&n3);
79constexpr int n7 = **n5;
80constexpr int n8 = **n6;
81
82}
83
84namespace TemplateArgumentConversion {
85  template<int n> struct IntParam {};
86
87  using IntParam0 = IntParam<0>;
88  using IntParam0 = IntParam<id(0)>;
89  using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}}
90}
91
92namespace CaseStatements {
93  void f(int n) {
94    switch (n) {
95    // FIXME: Produce the 'add ()' fixit for this.
96    case MemberZero().zero: // desired-error {{did you mean to call it with no arguments?}} expected-error {{not an integer constant expression}} expected-note {{non-literal type '<bound member function type>'}}
97    case id(1):
98      return;
99    }
100  }
101}
102
103extern int &Recurse1;
104int &Recurse2 = Recurse1; // expected-note 2{{declared here}} expected-note {{initializer of 'Recurse1' is not a constant expression}}
105int &Recurse1 = Recurse2; // expected-note {{declared here}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
106constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
107
108extern const int RecurseA;
109const int RecurseB = RecurseA; // expected-note {{declared here}}
110const int RecurseA = 10;
111constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
112
113namespace MemberEnum {
114  struct WithMemberEnum {
115    enum E { A = 42 };
116  } wme;
117
118  static_assert(wme.A == 42, "");
119}
120
121namespace DefaultArguments {
122
123const int z = int();
124constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
125  return a + b + *c + d;
126}
127const int four = 4;
128constexpr int eight = 8;
129constexpr const int twentyseven = 27;
130static_assert(Sum() == 0, "");
131static_assert(Sum(1) == 1, "");
132static_assert(Sum(1, four) == 5, "");
133static_assert(Sum(1, eight, &twentyseven) == 36, "");
134static_assert(Sum(1, 2, &four, eight) == 15, "");
135
136}
137
138namespace Ellipsis {
139
140// Note, values passed through an ellipsis can't actually be used.
141constexpr int F(int a, ...) { return a; }
142static_assert(F(0) == 0, "");
143static_assert(F(1, 0) == 1, "");
144static_assert(F(2, "test") == 2, "");
145static_assert(F(3, &F) == 3, "");
146int k = 0; // expected-note {{here}}
147static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
148
149}
150
151namespace Recursion {
152  constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
153  static_assert(fib(11) == 89, "");
154
155  constexpr int gcd_inner(int a, int b) {
156    return b == 0 ? a : gcd_inner(b, a % b);
157  }
158  constexpr int gcd(int a, int b) {
159    return gcd_inner(max(a, b), min(a, b));
160  }
161
162  static_assert(gcd(1749237, 5628959) == 7, "");
163}
164
165namespace FunctionCast {
166  // When folding, we allow functions to be cast to different types. Such
167  // cast functions cannot be called, even if they're constexpr.
168  constexpr int f() { return 1; }
169  typedef double (*DoubleFn)();
170  typedef int (*IntFn)();
171  int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}}
172  int b[(int)IntFn(f)()];    // ok
173}
174
175namespace StaticMemberFunction {
176  struct S {
177    static constexpr int k = 42;
178    static constexpr int f(int n) { return n * k + 2; }
179  } s;
180
181  constexpr int n = s.f(19);
182  static_assert(S::f(19) == 800, "");
183  static_assert(s.f(19) == 800, "");
184  static_assert(n == 800, "");
185
186  constexpr int (*sf1)(int) = &S::f;
187  constexpr int (*sf2)(int) = &s.f;
188  constexpr const int *sk = &s.k;
189}
190
191namespace ParameterScopes {
192
193  const int k = 42;
194  constexpr const int &ObscureTheTruth(const int &a) { return a; } // expected-note 3{{reference to 'a' cannot be returned from a constexpr function}}
195  constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}}
196    return ObscureTheTruth(b ? a : k); // expected-note 2{{in call to 'ObscureTheTruth(a)'}}
197  }
198  static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
199  constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnJunk(1, 0)'}}
200
201  constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { // expected-note {{here}}
202    // If ObscureTheTruth returns a reference to 'a', the result is not a
203    // constant expression even though 'a' is still in scope.
204    return ObscureTheTruth(b ? a : k); // expected-note {{in call to 'ObscureTheTruth(a)'}}
205  }
206  static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
207  constexpr int b = MaybeReturnNonstaticRef(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnNonstaticRef(1, 0)'}}
208
209  constexpr int InternalReturnJunk(int n) {
210    // FIXME: We should reject this: it never produces a constant expression.
211    return MaybeReturnJunk(true, n); // expected-note {{in call to 'MaybeReturnJunk(1, 0)'}}
212  }
213  constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
214
215  constexpr int LToR(int &n) { return n; }
216  constexpr int GrabCallersArgument(bool which, int a, int b) {
217    return LToR(which ? b : a);
218  }
219  static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
220  static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
221
222}
223
224namespace Pointers {
225
226  constexpr int f(int n, const int *a, const int *b, const int *c) {
227    return n == 0 ? 0 : *a + f(n-1, b, c, a);
228  }
229
230  const int x = 1, y = 10, z = 100;
231  static_assert(f(23, &x, &y, &z) == 788, "");
232
233  constexpr int g(int n, int a, int b, int c) {
234    return f(n, &a, &b, &c);
235  }
236  static_assert(g(23, x, y, z) == 788, "");
237
238}
239
240namespace FunctionPointers {
241
242  constexpr int Double(int n) { return 2 * n; }
243  constexpr int Triple(int n) { return 3 * n; }
244  constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
245  constexpr int Quadruple(int n) { return Twice(Double, n); }
246  constexpr auto Select(int n) -> int (*)(int) {
247    return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
248  }
249  constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}}
250
251  static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
252
253  constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}}
254
255}
256
257namespace PointerComparison {
258
259int x, y;
260static_assert(&x == &y, "false"); // expected-error {{false}}
261static_assert(&x != &y, "");
262constexpr bool g1 = &x == &y;
263constexpr bool g2 = &x != &y;
264constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
265constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
266constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
267constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
268
269struct S { int x, y; } s;
270static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
271static_assert(&s.x != &s.y, "");
272static_assert(&s.x <= &s.y, "");
273static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
274static_assert(&s.x < &s.y, "");
275static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
276
277static_assert(0 == &y, "false"); // expected-error {{false}}
278static_assert(0 != &y, "");
279constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
280constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
281constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
282constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
283
284static_assert(&x == 0, "false"); // expected-error {{false}}
285static_assert(&x != 0, "");
286constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
287constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
288constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
289constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
290
291static_assert(&x == &x, "");
292static_assert(&x != &x, "false"); // expected-error {{false}}
293static_assert(&x <= &x, "");
294static_assert(&x >= &x, "");
295static_assert(&x < &x, "false"); // expected-error {{false}}
296static_assert(&x > &x, "false"); // expected-error {{false}}
297
298constexpr S* sptr = &s;
299constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
300
301struct Str {
302  // FIXME: In C++ mode, we should say 'integral' not 'integer'
303  int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
304    expected-warning {{not integer constant expression}} \
305    expected-note {{dynamic_cast is not allowed in a constant expression}}
306  int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
307    expected-warning {{not integer constant expression}} \
308    expected-note {{reinterpret_cast is not allowed in a constant expression}}
309  int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
310    expected-warning {{not integer constant expression}} \
311    expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
312  int d : (S*)(42) == (S*)(42); // \
313    expected-warning {{not integer constant expression}} \
314    expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
315  int e : (Str*)(sptr) == (Str*)(sptr); // \
316    expected-warning {{not integer constant expression}} \
317    expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
318  int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \
319    expected-warning {{not integer constant expression}} \
320    expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
321  int g : (S*)(void*)(sptr) == sptr; // \
322    expected-warning {{not integer constant expression}} \
323    expected-note {{cast from 'void *' is not allowed in a constant expression}}
324};
325
326extern char externalvar[];
327constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
328constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
329static_assert(0 != "foo", "");
330
331}
332
333namespace MaterializeTemporary {
334
335constexpr int f(const int &r) { return r; }
336constexpr int n = f(1);
337
338constexpr bool same(const int &a, const int &b) { return &a == &b; }
339constexpr bool sameTemporary(const int &n) { return same(n, n); }
340
341static_assert(n, "");
342static_assert(!same(4, 4), "");
343static_assert(same(n, n), "");
344static_assert(sameTemporary(9), "");
345
346}
347
348constexpr int strcmp_ce(const char *p, const char *q) {
349  return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
350}
351
352namespace StringLiteral {
353
354template<typename Char>
355constexpr int MangleChars(const Char *p) {
356  return *p + 3 * (*p ? MangleChars(p+1) : 0);
357}
358
359static_assert(MangleChars("constexpr!") == 1768383, "");
360static_assert(MangleChars(u"constexpr!") == 1768383, "");
361static_assert(MangleChars(U"constexpr!") == 1768383, "");
362
363constexpr char c0 = "nought index"[0];
364constexpr char c1 = "nice index"[10];
365constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}} expected-note {{read of dereferenced one-past-the-end pointer}}
366constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} expected-note {{cannot refer to element -1 of array of 15 elements}}
367constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} expected-note {{cast which performs the conversions of a reinterpret_cast}}
368
369constexpr const char *p = "test" + 2;
370static_assert(*p == 's', "");
371
372constexpr const char *max_iter(const char *a, const char *b) {
373  return *a < *b ? b : a;
374}
375constexpr const char *max_element(const char *a, const char *b) {
376  return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
377}
378
379constexpr char str[] = "the quick brown fox jumped over the lazy dog";
380constexpr const char *max = max_element(begin(str), end(str));
381static_assert(*max == 'z', "");
382static_assert(max == str + 38, "");
383
384static_assert(strcmp_ce("hello world", "hello world") == 0, "");
385static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
386static_assert(strcmp_ce("constexpr", "test") < 0, "");
387static_assert(strcmp_ce("", " ") < 0, "");
388
389struct S {
390  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}}
391};
392
393struct T {
394  char c[6];
395  constexpr T() : c{"foo"} {}
396};
397constexpr T t;
398
399static_assert(t.c[0] == 'f', "");
400static_assert(t.c[1] == 'o', "");
401static_assert(t.c[2] == 'o', "");
402static_assert(t.c[3] == 0, "");
403static_assert(t.c[4] == 0, "");
404static_assert(t.c[5] == 0, "");
405static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
406
407}
408
409namespace Array {
410
411template<typename Iter>
412constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) {
413  return begin == end ? 0 : *begin + Sum(begin+1, end);
414}
415
416constexpr int xs[] = { 1, 2, 3, 4, 5 };
417constexpr int ys[] = { 5, 4, 3, 2, 1 };
418constexpr int sum_xs = Sum(begin(xs), end(xs));
419static_assert(sum_xs == 15, "");
420
421constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
422                       const int *xs, const int *ys, int c) {
423  return n ? F(
424               *xs, // expected-note {{read of dereferenced one-past-the-end pointer}}
425               *ys,
426               ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
427      expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
428      expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
429           : c;
430}
431constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
432constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
433static_assert(InnerProduct == 35, "");
434
435constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
436constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
437static_assert(DiffProd == 8, "");
438static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
439      expected-error {{constant expression}} \
440      expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
441
442constexpr const int *p = xs + 3;
443constexpr int xs4 = p[1]; // ok
444constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
445constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}}
446constexpr int xs0 = p[-3]; // ok
447constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
448
449constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
450static_assert(zs[0][0][0][0] == 1, "");
451static_assert(zs[1][1][1][1] == 16, "");
452static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
453static_assert((&zs[0][0][0][2])[-1] == 2, "");
454static_assert(**(**(zs + 1) + 1) == 11, "");
455static_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}}
456static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
457constexpr int err_zs_1_2_0_0 = zs[1][2][0][0]; // expected-error {{constant expression}} expected-note {{cannot access array element of pointer past the end}}
458
459constexpr int fail(const int &p) {
460  return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
461}
462static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \
463expected-error {{static_assert expression is not an integral constant expression}} \
464expected-note {{in call to 'fail(zs[1][0][1][0])'}}
465
466constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}}
467constexpr int SumNonzero(const int *p) {
468  return *p + (*p ? SumNonzero(p+1) : 0);
469}
470constexpr int CountZero(const int *p, const int *q) {
471  return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
472}
473static_assert(SumNonzero(arr) == 6, "");
474static_assert(CountZero(arr, arr + 40) == 36, "");
475
476struct ArrayElem {
477  constexpr ArrayElem() : n(0) {}
478  int n;
479  constexpr int f() { return n; }
480};
481struct ArrayRVal {
482  constexpr ArrayRVal() {}
483  ArrayElem elems[10];
484};
485static_assert(ArrayRVal().elems[3].f() == 0, "");
486
487}
488
489namespace DependentValues {
490
491struct I { int n; typedef I V[10]; };
492I::V x, y;
493template<bool B> struct S {
494  int k;
495  void f() {
496    I::V &cells = B ? x : y;
497    I &i = cells[k];
498    switch (i.n) {}
499  }
500};
501
502}
503
504namespace Class {
505
506struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
507constexpr int fn(const A &a) { return a.k; }
508static_assert(fn(A(4,5)) == 9, "");
509
510struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
511struct C {
512  constexpr C(C *this_) : m(42), n(this_->m) {} // ok
513  int m, n;
514};
515struct D {
516  C c;
517  constexpr D() : c(&c) {}
518};
519static_assert(D().c.n == 42, "");
520
521struct E {
522  constexpr E() : p(&p) {} // expected-note {{pointer to temporary cannot be used to initialize a member in a constant expression}}
523  void *p;
524};
525constexpr const E &e1 = E(); // expected-error {{constant expression}} expected-note {{in call to 'E()'}} expected-note {{temporary created here}}
526// This is a constant expression if we elide the copy constructor call, and
527// is not a constant expression if we don't! But we do, so it is.
528constexpr E e2 = E();
529static_assert(e2.p == &e2.p, "");
530constexpr E e3;
531static_assert(e3.p == &e3.p, "");
532
533extern const class F f;
534struct F {
535  constexpr F() : p(&f.p) {}
536  const void *p;
537};
538constexpr F f;
539
540struct G {
541  struct T {
542    constexpr T(T *p) : u1(), u2(p) {}
543    union U1 {
544      constexpr U1() {}
545      int a, b = 42;
546    } u1;
547    union U2 {
548      constexpr U2(T *p) : c(p->u1.b) {}
549      int c, d;
550    } u2;
551  } t;
552  constexpr G() : t(&t) {}
553} constexpr g;
554
555static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
556static_assert(g.t.u1.b == 42, "");
557static_assert(g.t.u2.c == 42, "");
558static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
559
560struct S {
561  int a, b;
562  const S *p;
563  double d;
564  const char *q;
565
566  constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
567};
568
569S global(43, &global);
570
571static_assert(S(15, &global).b == 15, "");
572
573constexpr bool CheckS(const S &s) {
574  return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
575}
576static_assert(CheckS(S(27, &global)), "");
577
578struct Arr {
579  char arr[3];
580  constexpr Arr() : arr{'x', 'y', 'z'} {}
581};
582constexpr int hash(Arr &&a) {
583  return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
584}
585constexpr int k = hash(Arr());
586static_assert(k == 0x007a7978, "");
587
588
589struct AggregateInit {
590  const char &c;
591  int n;
592  double d;
593  int arr[5];
594  void *p;
595};
596
597constexpr AggregateInit agg1 = { "hello"[0] };
598
599static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
600static_assert(agg1.n == 0, "");
601static_assert(agg1.d == 0.0, "");
602static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
603static_assert(agg1.arr[0] == 0, "");
604static_assert(agg1.arr[4] == 0, "");
605static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
606static_assert(agg1.p == nullptr, "");
607
608namespace SimpleDerivedClass {
609
610struct B {
611  constexpr B(int n) : a(n) {}
612  int a;
613};
614struct D : B {
615  constexpr D(int n) : B(n) {}
616};
617constexpr D d(3);
618static_assert(d.a == 3, "");
619
620}
621
622struct Bottom { constexpr Bottom() {} };
623struct Base : Bottom {
624  constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
625  int a;
626  const char *b;
627};
628struct Base2 : Bottom {
629  constexpr Base2(const int &r) : r(r) {}
630  int q = 123;
631  const int &r;
632};
633struct Derived : Base, Base2 {
634  constexpr Derived() : Base(76), Base2(a) {}
635  int c = r + b[1];
636};
637
638constexpr bool operator==(const Base &a, const Base &b) {
639  return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
640}
641
642constexpr Base base;
643constexpr Base base2(76);
644constexpr Derived derived;
645static_assert(derived.a == 76, "");
646static_assert(derived.b[2] == 's', "");
647static_assert(derived.c == 76 + 'e', "");
648static_assert(derived.q == 123, "");
649static_assert(derived.r == 76, "");
650static_assert(&derived.r == &derived.a, "");
651
652static_assert(!(derived == base), "");
653static_assert(derived == base2, "");
654
655constexpr Bottom &bot1 = (Base&)derived;
656constexpr Bottom &bot2 = (Base2&)derived;
657static_assert(&bot1 != &bot2, "");
658
659constexpr Bottom *pb1 = (Base*)&derived;
660constexpr Bottom *pb2 = (Base2*)&derived;
661static_assert(pb1 != pb2, "");
662static_assert(pb1 == &bot1, "");
663static_assert(pb2 == &bot2, "");
664
665constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
666constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
667constexpr Base2 &ok2 = (Base2&)bot2;
668static_assert(&ok2 == &derived, "");
669
670constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
671constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
672constexpr Base2 *pok2 = (Base2*)pb2;
673static_assert(pok2 == &derived, "");
674static_assert(&ok2 == pok2, "");
675static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
676static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
677
678constexpr Base *nullB = 42 - 6 * 7;
679static_assert((Bottom*)nullB == 0, "");
680static_assert((Derived*)nullB == 0, "");
681static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
682
683}
684
685namespace Temporaries {
686
687struct S {
688  constexpr S() {}
689  constexpr int f();
690};
691struct T : S {
692  constexpr T(int n) : S(), n(n) {}
693  int n;
694};
695constexpr int S::f() {
696  // 'this' must be the postfix-expression in a class member access expression,
697  // so we can't just use
698  //   return static_cast<T*>(this)->n;
699  return this->*(int(S::*))&T::n;
700}
701// The T temporary is implicitly cast to an S subobject, but we can recover the
702// T full-object via a base-to-derived cast, or a derived-to-base-casted member
703// pointer.
704static_assert(T(3).f() == 3, "");
705
706constexpr int f(const S &s) {
707  return static_cast<const T&>(s).n;
708}
709constexpr int n = f(T(5));
710static_assert(f(T(5)) == 5, "");
711
712}
713
714namespace Union {
715
716union U {
717  int a;
718  int b;
719};
720
721constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{C99 feature}}
722static_assert(u[0].a == 0, "");
723static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
724static_assert(u[1].b == 1, "");
725static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
726static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}}
727static_assert((&(u[1]) + 1 + 1)->b == 3, "");
728
729// Make sure we handle trivial copy constructors for unions.
730constexpr U x = {42};
731constexpr U y = x;
732static_assert(y.a == 42, "");
733static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}}
734
735}
736
737namespace MemberPointer {
738  struct A {
739    constexpr A(int n) : n(n) {}
740    int n;
741    constexpr int f() { return n + 3; }
742  };
743  constexpr A a(7);
744  static_assert(A(5).*&A::n == 5, "");
745  static_assert((&a)->*&A::n == 7, "");
746  static_assert((A(8).*&A::f)() == 11, "");
747  static_assert(((&a)->*&A::f)() == 10, "");
748
749  struct B : A {
750    constexpr B(int n, int m) : A(n), m(m) {}
751    int m;
752    constexpr int g() { return n + m + 1; }
753  };
754  constexpr B b(9, 13);
755  static_assert(B(4, 11).*&A::n == 4, "");
756  static_assert(B(4, 11).*&B::m == 11, "");
757  static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
758  static_assert((&b)->*&A::n == 9, "");
759  static_assert((&b)->*&B::m == 13, "");
760  static_assert((&b)->*(int(A::*))&B::m == 13, "");
761  static_assert((B(4, 11).*&A::f)() == 7, "");
762  static_assert((B(4, 11).*&B::g)() == 16, "");
763  static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
764  static_assert(((&b)->*&A::f)() == 12, "");
765  static_assert(((&b)->*&B::g)() == 23, "");
766  static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
767
768  struct S {
769    constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
770      m(m), n(n), pf(pf), pn(pn) {}
771    constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
772
773    constexpr int f() { return this->*pn; }
774    virtual int g() const;
775
776    int m, n;
777    int (S::*pf)() const;
778    int S::*pn;
779  };
780
781  constexpr int S::*pm = &S::m;
782  constexpr int S::*pn = &S::n;
783  constexpr int (S::*pf)() const = &S::f;
784  constexpr int (S::*pg)() const = &S::g;
785
786  constexpr S s(2, 5, &S::f, &S::m);
787
788  static_assert((s.*&S::f)() == 2, "");
789  static_assert((s.*s.pf)() == 2, "");
790
791  template<int n> struct T : T<n-1> {};
792  template<> struct T<0> { int n; };
793  template<> struct T<30> : T<29> { int m; };
794
795  T<17> t17;
796  T<30> t30;
797
798  constexpr int (T<10>::*deepn) = &T<0>::n;
799  static_assert(&(t17.*deepn) == &t17.n, "");
800
801  constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
802  constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
803  static_assert(&(t30.*deepm) == &t30.m, "");
804
805  constexpr T<5> *p17_5 = &t17;
806  constexpr T<13> *p17_13 = (T<13>*)p17_5;
807  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>'}}
808  static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
809  static_assert(&(p17_13->*deepn) == &t17.n, "");
810  constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
811
812  constexpr T<5> *p30_5 = &t30;
813  constexpr T<23> *p30_23 = (T<23>*)p30_5;
814  constexpr T<13> *p30_13 = p30_23;
815  static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
816  static_assert(&(p30_13->*deepn) == &t30.n, "");
817  static_assert(&(p30_23->*deepn) == &t30.n, "");
818  static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
819  static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
820  static_assert(&(p30_23->*deepm) == &t30.m, "");
821}
822
823namespace ArrayBaseDerived {
824
825  struct Base {
826    constexpr Base() {}
827    int n = 0;
828  };
829  struct Derived : Base {
830    constexpr Derived() {}
831    constexpr const int *f() { return &n; }
832  };
833
834  constexpr Derived a[10];
835  constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
836  constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
837  static_assert(pb3 == pd3, "");
838
839  // pb3 does not point to an array element.
840  constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
841  constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}}
842  constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}}
843  constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}}
844  constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}}
845  constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}}
846  constexpr Base *pb3a = pb4 - 1;
847
848  // pb4 does not point to a Derived.
849  constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}}
850  constexpr Derived *pd3a = (Derived*)pb3a;
851  constexpr int pd3n = pd3a->n;
852
853  // pd3a still points to the Derived array.
854  constexpr Derived *pd6 = pd3a + 3;
855  static_assert(pd6 == &a[6], "");
856  constexpr Derived *pd9 = pd6 + 3;
857  constexpr Derived *pd10 = pd6 + 4;
858  constexpr int pd9n = pd9->n; // ok
859  constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}}
860  constexpr int pd0n = pd10[-10].n;
861  constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}}
862
863  constexpr Base *pb9 = pd9;
864  constexpr const int *(Base::*pfb)() const =
865      static_cast<const int *(Base::*)() const>(&Derived::f);
866  static_assert((pb9->*pfb)() == &a[9].n, "");
867}
868
869namespace Complex {
870
871class complex {
872  int re, im;
873public:
874  constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
875  constexpr complex(const complex &o) : re(o.re), im(o.im) {}
876  constexpr complex operator-() const { return complex(-re, -im); }
877  friend constexpr complex operator+(const complex &l, const complex &r) {
878    return complex(l.re + r.re, l.im + r.im);
879  }
880  friend constexpr complex operator-(const complex &l, const complex &r) {
881    return l + -r;
882  }
883  friend constexpr complex operator*(const complex &l, const complex &r) {
884    return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
885  }
886  friend constexpr bool operator==(const complex &l, const complex &r) {
887    return l.re == r.re && l.im == r.im;
888  }
889  constexpr bool operator!=(const complex &r) const {
890    return re != r.re || im != r.im;
891  }
892  constexpr int real() const { return re; }
893  constexpr int imag() const { return im; }
894};
895
896constexpr complex i = complex(0, 1);
897constexpr complex k = (3 + 4*i) * (6 - 4*i);
898static_assert(complex(1,0).real() == 1, "");
899static_assert(complex(1,0).imag() == 0, "");
900static_assert(((complex)1).imag() == 0, "");
901static_assert(k.real() == 34, "");
902static_assert(k.imag() == 12, "");
903static_assert(k - 34 == 12*i, "");
904static_assert((complex)1 == complex(1), "");
905static_assert((complex)1 != complex(0, 1), "");
906static_assert(complex(1) == complex(1), "");
907static_assert(complex(1) != complex(0, 1), "");
908constexpr complex makeComplex(int re, int im) { return complex(re, im); }
909static_assert(makeComplex(1,0) == complex(1), "");
910static_assert(makeComplex(1,0) != complex(0, 1), "");
911
912class complex_wrap : public complex {
913public:
914  constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
915  constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
916};
917
918static_assert((complex_wrap)1 == complex(1), "");
919static_assert((complex)1 != complex_wrap(0, 1), "");
920static_assert(complex(1) == complex_wrap(1), "");
921static_assert(complex_wrap(1) != complex(0, 1), "");
922constexpr complex_wrap makeComplexWrap(int re, int im) {
923  return complex_wrap(re, im);
924}
925static_assert(makeComplexWrap(1,0) == complex(1), "");
926static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
927
928}
929
930namespace PR11595 {
931  struct A { constexpr bool operator==(int x) { return true; } };
932  struct B { B(); A& x; };
933  static_assert(B().x == 3, "");  // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
934
935  constexpr bool f(int k) {
936    return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
937  }
938  constexpr int n = f(1); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'f(1)'}}
939}
940
941namespace ExprWithCleanups {
942  struct A { A(); ~A(); int get(); };
943  constexpr int get(bool FromA) { return FromA ? A().get() : 1; }
944  constexpr int n = get(false);
945}
946
947namespace Volatile {
948
949volatile constexpr int n1 = 0; // expected-note {{here}}
950volatile const int n2 = 0; // expected-note {{here}}
951int n3 = 37; // expected-note {{declared here}}
952
953constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
954constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
955
956struct T { int n; };
957const T t = { 42 }; // expected-note {{declared here}}
958
959constexpr int f(volatile int &&r) {
960  return r; // expected-note {{read of volatile temporary is not allowed in a constant expression}}
961}
962struct S {
963  int k : f(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'f(0)'}}
964  int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
965  int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}}
966};
967
968}
969
970namespace ExternConstexpr {
971  extern constexpr int n = 0;
972  extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}}
973  void f() {
974    extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}}
975    constexpr int j = 0;
976    constexpr int k; // expected-error {{default initialization of an object of const type}}
977  }
978}
979
980namespace ComplexConstexpr {
981  constexpr _Complex float test1 = {};
982  constexpr _Complex float test2 = {1};
983  constexpr _Complex double test3 = {1,2};
984  constexpr _Complex int test4 = {4};
985  constexpr _Complex int test5 = 4;
986  constexpr _Complex int test6 = {5,6};
987}
988