blob: d208362cbce6e449211013765ca1b9c8c6a461d6 [file] [log] [blame]
Richard Smithe97cbd72011-11-11 04:05:33 +00001// RUN: %clang_cc1 -triple i686-linux -fsyntax-only -verify -std=c++11 %s
2
3// This version of static_assert just requires a foldable value as the
4// expression, not an ICE.
5// FIXME: Once we implement the C++11 ICE rules, most uses of this here should
6// be converted to static_assert.
7#define static_assert_fold(expr, str) \
8 static_assert(__builtin_constant_p(expr), "not an integral constant expression"); \
9 static_assert(__builtin_constant_p(expr) ? expr : true, str)
10
11namespace StaticAssertFoldTest {
12
13int x;
14static_assert_fold(++x, "test"); // expected-error {{not an integral constant expression}}
15static_assert_fold(false, "test"); // expected-error {{test}}
16
17}
18
19// FIXME: support const T& parameters here.
20//template<typename T> constexpr T id(const T &t) { return t; }
21template<typename T> constexpr T id(T t) { return t; }
22// FIXME: support templates here.
23//template<typename T> constexpr T min(const T &a, const T &b) {
24// return a < b ? a : b;
25//}
26//template<typename T> constexpr T max(const T &a, const T &b) {
27// return a < b ? b : a;
28//}
29constexpr int min(const int &a, const int &b) { return a < b ? a : b; }
30constexpr int max(const int &a, const int &b) { return a < b ? b : a; }
31
32struct MemberZero {
33 constexpr int zero() { return 0; }
34};
35
36namespace DerivedToVBaseCast {
37
38 struct U { int n; };
39 struct V : U { int n; };
40 struct A : virtual V { int n; };
41 struct Aa { int n; };
42 struct B : virtual A, Aa {};
43 struct C : virtual A, Aa {};
44 struct D : B, C {};
45
46 D d;
47 constexpr B *p = &d;
48 constexpr C *q = &d;
49 static_assert_fold((void*)p != (void*)q, "");
50 static_assert_fold((A*)p == (A*)q, "");
51 static_assert_fold((Aa*)p != (Aa*)q, "");
52
53 constexpr B &pp = d;
54 constexpr C &qq = d;
55 static_assert_fold((void*)&pp != (void*)&qq, "");
56 static_assert_fold(&(A&)pp == &(A&)qq, "");
57 static_assert_fold(&(Aa&)pp != &(Aa&)qq, "");
58
59 constexpr V *v = p;
60 constexpr V *w = q;
61 constexpr V *x = (A*)p;
62 static_assert_fold(v == w, "");
63 static_assert_fold(v == x, "");
64
65 static_assert_fold((U*)&d == p, "");
66 static_assert_fold((U*)&d == q, "");
67 static_assert_fold((U*)&d == v, "");
68 static_assert_fold((U*)&d == w, "");
69 static_assert_fold((U*)&d == x, "");
70
71 struct X {};
72 struct Y1 : virtual X {};
73 struct Y2 : X {};
74 struct Z : Y1, Y2 {};
75 Z z;
76 static_assert_fold((X*)(Y1*)&z != (X*)(Y2*)&z, "");
77
78}
79
Richard Smith6804be52011-11-11 08:28:03 +000080namespace ConstCast {
81
82constexpr int n1 = 0;
83constexpr int n2 = const_cast<int&>(n1);
84constexpr int *n3 = const_cast<int*>(&n1);
85constexpr int n4 = *const_cast<int*>(&n1);
86constexpr const int * const *n5 = const_cast<const int* const*>(&n3);
87constexpr int **n6 = const_cast<int**>(&n3);
88constexpr int n7 = **n5;
89constexpr int n8 = **n6;
90
91}
92
Richard Smithe97cbd72011-11-11 04:05:33 +000093namespace TemplateArgumentConversion {
94 template<int n> struct IntParam {};
95
96 using IntParam0 = IntParam<0>;
97 // FIXME: This should be accepted once we do constexpr function invocation.
98 using IntParam0 = IntParam<id(0)>; // expected-error {{not an integral constant expression}}
99 using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} expected-error {{not an integral constant expression}}
100}
101
102namespace CaseStatements {
103 void f(int n) {
104 switch (n) {
105 // FIXME: Produce the 'add ()' fixit for this.
106 case MemberZero().zero: // desired-error {{did you mean to call it with no arguments?}} expected-error {{not an integer constant expression}}
107 // FIXME: This should be accepted once we do constexpr function invocation.
108 case id(1): // expected-error {{not an integer constant expression}}
109 return;
110 }
111 }
112}
113
114extern int &Recurse1;
115int &Recurse2 = Recurse1, &Recurse1 = Recurse2;
116constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}}
117
118namespace MemberEnum {
119 struct WithMemberEnum {
120 enum E { A = 42 };
121 } wme;
122
123 static_assert_fold(wme.A == 42, "");
124}
125
126namespace DefaultArguments {
127
128const int z = int();
129constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
130 return a + b + *c + d;
131}
132const int four = 4;
133constexpr int eight = 8;
134constexpr const int twentyseven = 27;
135static_assert_fold(Sum() == 0, "");
136static_assert_fold(Sum(1) == 1, "");
137static_assert_fold(Sum(1, four) == 5, "");
138static_assert_fold(Sum(1, eight, &twentyseven) == 36, "");
139static_assert_fold(Sum(1, 2, &four, eight) == 15, "");
140
141}
142
143namespace Ellipsis {
144
145// Note, values passed through an ellipsis can't actually be used.
146constexpr int F(int a, ...) { return a; }
147static_assert_fold(F(0) == 0, "");
148static_assert_fold(F(1, 0) == 1, "");
149static_assert_fold(F(2, "test") == 2, "");
150static_assert_fold(F(3, &F) == 3, "");
151int k = 0;
152static_assert_fold(F(4, k) == 3, ""); // expected-error {{constant expression}}
153
154}
155
156namespace Recursion {
157 constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
158 static_assert_fold(fib(11) == 89, "");
159
160 constexpr int gcd_inner(int a, int b) {
161 return b == 0 ? a : gcd_inner(b, a % b);
162 }
163 constexpr int gcd(int a, int b) {
164 return gcd_inner(max(a, b), min(a, b));
165 }
166
167 static_assert_fold(gcd(1749237, 5628959) == 7, "");
168}
169
170namespace FunctionCast {
171 // When folding, we allow functions to be cast to different types. Such
172 // cast functions cannot be called, even if they're constexpr.
173 constexpr int f() { return 1; }
174 typedef double (*DoubleFn)();
175 typedef int (*IntFn)();
176 int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}}
177 int b[(int)IntFn(f)()]; // ok
178}
179
180namespace StaticMemberFunction {
181 struct S {
182 static constexpr int k = 42;
183 static constexpr int f(int n) { return n * k + 2; }
184 } s;
185
186 constexpr int n = s.f(19);
187 static_assert_fold(S::f(19) == 800, "");
188 static_assert_fold(s.f(19) == 800, "");
189 static_assert_fold(n == 800, "");
Richard Smithce40ad62011-11-12 22:28:03 +0000190
191 constexpr int (*sf1)(int) = &S::f;
192 constexpr int (*sf2)(int) = &s.f;
193 constexpr const int *sk = &s.k;
Richard Smithe97cbd72011-11-11 04:05:33 +0000194}
195
196namespace ParameterScopes {
197
198 const int k = 42;
199 constexpr const int &ObscureTheTruth(const int &a) { return a; }
200 constexpr const int &MaybeReturnJunk(bool b, const int a) {
201 return ObscureTheTruth(b ? a : k);
202 }
203 static_assert_fold(MaybeReturnJunk(false, 0) == 42, ""); // ok
204 constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}}
205
206 constexpr const int MaybeReturnNonstaticRef(bool b, const int a) {
207 // If ObscureTheTruth returns a reference to 'a', the result is not a
208 // constant expression even though 'a' is still in scope.
209 return ObscureTheTruth(b ? a : k);
210 }
211 static_assert_fold(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
212 constexpr int b = MaybeReturnNonstaticRef(true, 0); // expected-error {{constant expression}}
213
214 constexpr int InternalReturnJunk(int n) {
215 // FIXME: We should reject this: it never produces a constant expression.
216 return MaybeReturnJunk(true, n);
217 }
218 constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}}
219
220 constexpr int LToR(int &n) { return n; }
221 constexpr int GrabCallersArgument(bool which, int a, int b) {
222 return LToR(which ? b : a);
223 }
224 static_assert_fold(GrabCallersArgument(false, 1, 2) == 1, "");
225 static_assert_fold(GrabCallersArgument(true, 4, 8) == 8, "");
226
227}
228
229namespace Pointers {
230
231 constexpr int f(int n, const int *a, const int *b, const int *c) {
232 return n == 0 ? 0 : *a + f(n-1, b, c, a);
233 }
234
235 const int x = 1, y = 10, z = 100;
236 static_assert_fold(f(23, &x, &y, &z) == 788, "");
237
238 constexpr int g(int n, int a, int b, int c) {
239 return f(n, &a, &b, &c);
240 }
241 static_assert_fold(g(23, x, y, z) == 788, "");
242
243}
244
245namespace FunctionPointers {
246
247 constexpr int Double(int n) { return 2 * n; }
248 constexpr int Triple(int n) { return 3 * n; }
249 constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
250 constexpr int Quadruple(int n) { return Twice(Double, n); }
251 constexpr auto Select(int n) -> int (*)(int) {
252 return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
253 }
254 constexpr int Apply(int (*F)(int), int n) { return F(n); }
255
256 static_assert_fold(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
257
258 constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}}
259
260}
261
262namespace PointerComparison {
263
264int x, y;
265static_assert_fold(&x == &y, "false"); // expected-error {{false}}
266static_assert_fold(&x != &y, "");
267constexpr bool g1 = &x == &y;
268constexpr bool g2 = &x != &y;
269constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
270constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
271constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
272constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
273
274struct S { int x, y; } s;
275static_assert_fold(&s.x == &s.y, "false"); // expected-error {{false}}
276static_assert_fold(&s.x != &s.y, "");
277static_assert_fold(&s.x <= &s.y, "");
278static_assert_fold(&s.x >= &s.y, "false"); // expected-error {{false}}
279static_assert_fold(&s.x < &s.y, "");
280static_assert_fold(&s.x > &s.y, "false"); // expected-error {{false}}
281
282static_assert_fold(0 == &y, "false"); // expected-error {{false}}
283static_assert_fold(0 != &y, "");
284constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
285constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
286constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
287constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
288
289static_assert_fold(&x == 0, "false"); // expected-error {{false}}
290static_assert_fold(&x != 0, "");
291constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
292constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
293constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
294constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
295
296static_assert_fold(&x == &x, "");
297static_assert_fold(&x != &x, "false"); // expected-error {{false}}
298static_assert_fold(&x <= &x, "");
299static_assert_fold(&x >= &x, "");
300static_assert_fold(&x < &x, "false"); // expected-error {{false}}
301static_assert_fold(&x > &x, "false"); // expected-error {{false}}
302
303constexpr S* sptr = &s;
304// FIXME: This is not a constant expression; check we reject this and move this
305// test elsewhere.
306constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr);
307
308extern char externalvar[];
309// FIXME: This is not a constant expression; check we reject this and move this
310// test elsewhere.
311constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
312constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
313static_assert_fold(0 != "foo", "");
314
315}
316
317namespace MaterializeTemporary {
318
319constexpr int f(const int &r) { return r; }
320constexpr int n = f(1);
321
322constexpr bool same(const int &a, const int &b) { return &a == &b; }
323constexpr bool sameTemporary(const int &n) { return same(n, n); }
324
325static_assert_fold(n, "");
326static_assert_fold(!same(4, 4), "");
327static_assert_fold(same(n, n), "");
328static_assert_fold(sameTemporary(9), "");
329
330}
331
332constexpr int strcmp_ce(const char *p, const char *q) {
333 return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
334}
335
336namespace StringLiteral {
337
338// FIXME: Refactor this once we support constexpr templates.
339constexpr int MangleChars(const char *p) {
340 return *p + 3 * (*p ? MangleChars(p+1) : 0);
341}
342constexpr int MangleChars(const char16_t *p) {
343 return *p + 3 * (*p ? MangleChars(p+1) : 0);
344}
345constexpr int MangleChars(const char32_t *p) {
346 return *p + 3 * (*p ? MangleChars(p+1) : 0);
347}
348
349static_assert_fold(MangleChars("constexpr!") == 1768383, "");
350static_assert_fold(MangleChars(u"constexpr!") == 1768383, "");
351static_assert_fold(MangleChars(U"constexpr!") == 1768383, "");
352
353constexpr char c0 = "nought index"[0];
354constexpr char c1 = "nice index"[10];
355constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{indexes past the end}}
356constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{indexes before the beginning}}
357constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}}
358
359constexpr const char *p = "test" + 2;
360static_assert_fold(*p == 's', "");
361
362constexpr const char *max_iter(const char *a, const char *b) {
363 return *a < *b ? b : a;
364}
365constexpr const char *max_element(const char *a, const char *b) {
366 return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
367}
368
369constexpr const char *begin(const char (&arr)[45]) { return arr; }
370constexpr const char *end(const char (&arr)[45]) { return arr + 45; }
371
372constexpr char str[] = "the quick brown fox jumped over the lazy dog";
373constexpr const char *max = max_element(begin(str), end(str));
374static_assert_fold(*max == 'z', "");
375static_assert_fold(max == str + 38, "");
376
377static_assert_fold(strcmp_ce("hello world", "hello world") == 0, "");
378static_assert_fold(strcmp_ce("hello world", "hello clang") > 0, "");
379static_assert_fold(strcmp_ce("constexpr", "test") < 0, "");
380static_assert_fold(strcmp_ce("", " ") < 0, "");
381
382}
383
384namespace Array {
385
386// FIXME: Use templates for these once we support constexpr templates.
387constexpr int Sum(const int *begin, const int *end) {
388 return begin == end ? 0 : *begin + Sum(begin+1, end);
389}
390constexpr const int *begin(const int (&xs)[5]) { return xs; }
391constexpr const int *end(const int (&xs)[5]) { return xs + 5; }
392
393constexpr int xs[] = { 1, 2, 3, 4, 5 };
394constexpr int ys[] = { 5, 4, 3, 2, 1 };
395constexpr int sum_xs = Sum(begin(xs), end(xs));
396static_assert_fold(sum_xs == 15, "");
397
398constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
399 const int *xs, const int *ys, int c) {
400 return n ? F(*xs, *ys, ZipFoldR(F, n-1, xs+1, ys+1, c)) : c;
401}
402constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
403constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
404static_assert_fold(InnerProduct == 35, "");
405
406constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
407constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
408static_assert_fold(DiffProd == 8, "");
409static_assert_fold(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // expected-error {{constant expression}}
410
411constexpr const int *p = xs + 3;
412constexpr int xs4 = p[1]; // ok
413constexpr int xs5 = p[2]; // expected-error {{constant expression}}
414constexpr int xs0 = p[-3]; // ok
415constexpr int xs_1 = p[-4]; // expected-error {{constant expression}}
416
417constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
418static_assert_fold(zs[0][0][0][0] == 1, "");
419static_assert_fold(zs[1][1][1][1] == 16, "");
420static_assert_fold(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}}
421static_assert_fold((&zs[0][0][0][2])[-1] == 2, "");
422static_assert_fold(**(**(zs + 1) + 1) == 11, "");
423static_assert_fold(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, "");
424
425constexpr int arr[40] = { 1, 2, 3, [8] = 4 };
426constexpr int SumNonzero(const int *p) {
427 return *p + (*p ? SumNonzero(p+1) : 0);
428}
429constexpr int CountZero(const int *p, const int *q) {
430 return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
431}
432static_assert_fold(SumNonzero(arr) == 6, "");
433static_assert_fold(CountZero(arr, arr + 40) == 36, "");
434
435}
436
437namespace DependentValues {
438
439struct I { int n; typedef I V[10]; };
440I::V x, y;
441template<bool B> struct S {
442 int k;
443 void f() {
444 I::V &cells = B ? x : y;
445 I &i = cells[k];
446 switch (i.n) {}
447 }
448};
449
450}
451
452namespace Class {
453
454struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
455constexpr int fn(const A &a) { return a.k; }
456static_assert_fold(fn(A(4,5)) == 9, "");
457
458struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
459struct C {
460 constexpr C(C *this_) : m(42), n(this_->m) {} // ok
461 int m, n;
462};
463struct D {
464 C c;
465 constexpr D() : c(&c) {}
466};
467static_assert_fold(D().c.n == 42, "");
468
469struct E {
470 constexpr E() : p(&p) {}
471 void *p;
472};
473constexpr const E &e1 = E(); // expected-error {{constant expression}}
474// This is a constant expression if we elide the copy constructor call, and
475// is not a constant expression if we don't! But we do, so it is.
476// FIXME: The move constructor is not currently implicitly defined as constexpr.
477// We notice this when evaluating an expression which uses it, but not when
478// checking its initializer.
479constexpr E e2 = E(); // unexpected-error {{constant expression}}
480static_assert_fold(e2.p == &e2.p, ""); // unexpected-error {{constant expression}}
481// FIXME: We don't pass through the fact that 'this' is ::e3 when checking the
482// initializer of this declaration.
483constexpr E e3; // unexpected-error {{constant expression}}
484static_assert_fold(e3.p == &e3.p, "");
485
486extern const class F f;
487struct F {
488 constexpr F() : p(&f.p) {}
489 const void *p;
490};
491constexpr F f = F();
492
493struct G {
494 struct T {
495 constexpr T(T *p) : u1(), u2(p) {}
496 union U1 {
497 constexpr U1() {}
498 int a, b = 42;
499 } u1;
500 union U2 {
501 constexpr U2(T *p) : c(p->u1.b) {}
502 int c, d;
503 } u2;
504 } t;
505 constexpr G() : t(&t) {}
506} constexpr g;
507
508static_assert_fold(g.t.u1.a == 42, ""); // expected-error {{constant expression}}
509static_assert_fold(g.t.u1.b == 42, "");
510static_assert_fold(g.t.u2.c == 42, "");
511static_assert_fold(g.t.u2.d == 42, ""); // expected-error {{constant expression}}
512
513struct S {
514 int a, b;
515 const S *p;
516 double d;
517 const char *q;
518
519 constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
520};
521
522S global(43, &global);
523
524static_assert_fold(S(15, &global).b == 15, "");
525
526constexpr bool CheckS(const S &s) {
527 return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
528}
529static_assert_fold(CheckS(S(27, &global)), "");
530
531struct Arr {
532 char arr[3];
533 constexpr Arr() : arr{'x', 'y', 'z'} {}
534};
535constexpr int hash(Arr &&a) {
536 return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
537}
538constexpr int k = hash(Arr());
539static_assert_fold(k == 0x007a7978, "");
540
541
542struct AggregateInit {
543 const char &c;
544 int n;
545 double d;
546 int arr[5];
547 void *p;
548};
549
550constexpr AggregateInit agg1 = { "hello"[0] };
551
552static_assert_fold(strcmp_ce(&agg1.c, "hello") == 0, "");
553static_assert_fold(agg1.n == 0, "");
554static_assert_fold(agg1.d == 0.0, "");
555static_assert_fold(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}}
556static_assert_fold(agg1.arr[0] == 0, "");
557static_assert_fold(agg1.arr[4] == 0, "");
558static_assert_fold(agg1.arr[5] == 0, ""); // expected-error {{constant expression}}
559static_assert_fold(agg1.p == nullptr, "");
560
561namespace SimpleDerivedClass {
562
563struct B {
564 constexpr B(int n) : a(n) {}
565 int a;
566};
567struct D : B {
568 constexpr D(int n) : B(n) {}
569};
570constexpr D d(3);
571static_assert_fold(d.a == 3, "");
572
573}
574
575struct Base {
576 constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
577 int a;
578 const char *b;
579};
580struct Base2 {
581 constexpr Base2(const int &r) : r(r) {}
582 int q = 123;
583 // FIXME: When we track the global for which we are computing the initializer,
584 // use a reference here.
585 //const int &r;
586 int r;
587};
588struct Derived : Base, Base2 {
589 constexpr Derived() : Base(76), Base2(a) {}
590 int c = r + b[1];
591};
592
593constexpr bool operator==(const Base &a, const Base &b) {
594 return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
595}
596
597constexpr Base base;
598constexpr Base base2(76);
599constexpr Derived derived;
600static_assert_fold(derived.a == 76, "");
601static_assert_fold(derived.b[2] == 's', "");
602static_assert_fold(derived.c == 76 + 'e', "");
603static_assert_fold(derived.q == 123, "");
604static_assert_fold(derived.r == 76, "");
605static_assert_fold(&derived.r == &derived.a, ""); // expected-error {{}}
606
607static_assert_fold(!(derived == base), "");
608static_assert_fold(derived == base2, "");
609
610}
611
612namespace Union {
613
614union U {
615 int a;
616 int b;
617};
618
619constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } };
620static_assert_fold(u[0].a == 0, "");
621static_assert_fold(u[0].b, ""); // expected-error {{constant expression}}
622static_assert_fold(u[1].b == 1, "");
623static_assert_fold((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}}
624static_assert_fold(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}}
625static_assert_fold((&(u[1]) + 1 + 1)->b == 3, "");
626
627}
628
629namespace Complex {
630
631class complex {
632 int re, im;
633public:
634 constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
635 constexpr complex(const complex &o) : re(o.re), im(o.im) {}
636 constexpr complex operator-() const { return complex(-re, -im); }
637 friend constexpr complex operator+(const complex &l, const complex &r) {
638 return complex(l.re + r.re, l.im + r.im);
639 }
640 friend constexpr complex operator-(const complex &l, const complex &r) {
641 return l + -r;
642 }
643 friend constexpr complex operator*(const complex &l, const complex &r) {
644 return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
645 }
646 friend constexpr bool operator==(const complex &l, const complex &r) {
647 return l.re == r.re && l.im == r.im;
648 }
649 constexpr bool operator!=(const complex &r) const {
650 return re != r.re || im != r.im;
651 }
652 constexpr int real() const { return re; }
653 constexpr int imag() const { return im; }
654};
655
656constexpr complex i = complex(0, 1);
657constexpr complex k = (3 + 4*i) * (6 - 4*i);
658static_assert_fold(complex(1,0).real() == 1, "");
659static_assert_fold(complex(1,0).imag() == 0, "");
660static_assert_fold(((complex)1).imag() == 0, "");
661static_assert_fold(k.real() == 34, "");
662static_assert_fold(k.imag() == 12, "");
663static_assert_fold(k - 34 == 12*i, "");
664static_assert_fold((complex)1 == complex(1), "");
665static_assert_fold((complex)1 != complex(0, 1), "");
666static_assert_fold(complex(1) == complex(1), "");
667static_assert_fold(complex(1) != complex(0, 1), "");
668constexpr complex makeComplex(int re, int im) { return complex(re, im); }
669static_assert_fold(makeComplex(1,0) == complex(1), "");
670static_assert_fold(makeComplex(1,0) != complex(0, 1), "");
671
672class complex_wrap : public complex {
673public:
674 constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
675 constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
676};
677
678static_assert_fold((complex_wrap)1 == complex(1), "");
679static_assert_fold((complex)1 != complex_wrap(0, 1), "");
680static_assert_fold(complex(1) == complex_wrap(1), "");
681static_assert_fold(complex_wrap(1) != complex(0, 1), "");
682constexpr complex_wrap makeComplexWrap(int re, int im) {
683 return complex_wrap(re, im);
684}
685static_assert_fold(makeComplexWrap(1,0) == complex(1), "");
686static_assert_fold(makeComplexWrap(1,0) != complex(0, 1), "");
687
688}