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