blob: 3ba87b3d91e9b6cf5994d5190717aea6086eff6f [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
Richard Smithe97cbd72011-11-11 04:05:33 +00003namespace StaticAssertFoldTest {
4
5int x;
Richard Smith0765bec2011-12-09 23:00:37 +00006static_assert(++x, "test"); // expected-error {{not an integral constant expression}}
7static_assert(false, "test"); // expected-error {{test}}
Richard Smithe97cbd72011-11-11 04:05:33 +00008
9}
10
11// FIXME: support const T& parameters here.
12//template<typename T> constexpr T id(const T &t) { return t; }
13template<typename T> constexpr T id(T t) { return t; }
14// FIXME: support templates here.
15//template<typename T> constexpr T min(const T &a, const T &b) {
16// return a < b ? a : b;
17//}
18//template<typename T> constexpr T max(const T &a, const T &b) {
19// return a < b ? b : a;
20//}
21constexpr int min(const int &a, const int &b) { return a < b ? a : b; }
22constexpr int max(const int &a, const int &b) { return a < b ? b : a; }
23
24struct MemberZero {
25 constexpr int zero() { return 0; }
26};
27
28namespace DerivedToVBaseCast {
29
30 struct U { int n; };
31 struct V : U { int n; };
32 struct A : virtual V { int n; };
33 struct Aa { int n; };
34 struct B : virtual A, Aa {};
35 struct C : virtual A, Aa {};
36 struct D : B, C {};
37
38 D d;
39 constexpr B *p = &d;
40 constexpr C *q = &d;
Richard Smith0765bec2011-12-09 23:00:37 +000041 static_assert((void*)p != (void*)q, "");
42 static_assert((A*)p == (A*)q, "");
43 static_assert((Aa*)p != (Aa*)q, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000044
45 constexpr B &pp = d;
46 constexpr C &qq = d;
Richard Smith0765bec2011-12-09 23:00:37 +000047 static_assert((void*)&pp != (void*)&qq, "");
48 static_assert(&(A&)pp == &(A&)qq, "");
49 static_assert(&(Aa&)pp != &(Aa&)qq, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000050
51 constexpr V *v = p;
52 constexpr V *w = q;
53 constexpr V *x = (A*)p;
Richard Smith0765bec2011-12-09 23:00:37 +000054 static_assert(v == w, "");
55 static_assert(v == x, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000056
Richard Smith0765bec2011-12-09 23:00:37 +000057 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, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000062
63 struct X {};
64 struct Y1 : virtual X {};
65 struct Y2 : X {};
66 struct Z : Y1, Y2 {};
67 Z z;
Richard Smith0765bec2011-12-09 23:00:37 +000068 static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000069
70}
71
Richard Smith6804be52011-11-11 08:28:03 +000072namespace ConstCast {
73
74constexpr int n1 = 0;
75constexpr int n2 = const_cast<int&>(n1);
76constexpr int *n3 = const_cast<int*>(&n1);
77constexpr int n4 = *const_cast<int*>(&n1);
78constexpr const int * const *n5 = const_cast<const int* const*>(&n3);
79constexpr int **n6 = const_cast<int**>(&n3);
80constexpr int n7 = **n5;
81constexpr int n8 = **n6;
82
83}
84
Richard Smithe97cbd72011-11-11 04:05:33 +000085namespace TemplateArgumentConversion {
86 template<int n> struct IntParam {};
87
88 using IntParam0 = IntParam<0>;
Richard Smith027bf112011-11-17 22:56:20 +000089 // FIXME: This should be accepted once we implement the new ICE rules.
Richard Smithe97cbd72011-11-11 04:05:33 +000090 using IntParam0 = IntParam<id(0)>; // expected-error {{not an integral constant expression}}
91 using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} expected-error {{not an integral constant expression}}
92}
93
94namespace CaseStatements {
95 void f(int n) {
96 switch (n) {
97 // FIXME: Produce the 'add ()' fixit for this.
98 case MemberZero().zero: // desired-error {{did you mean to call it with no arguments?}} expected-error {{not an integer constant expression}}
Richard Smith027bf112011-11-17 22:56:20 +000099 // FIXME: This should be accepted once we implement the new ICE rules.
Richard Smithe97cbd72011-11-11 04:05:33 +0000100 case id(1): // expected-error {{not an integer constant expression}}
101 return;
102 }
103 }
104}
105
106extern int &Recurse1;
107int &Recurse2 = Recurse1, &Recurse1 = Recurse2;
108constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}}
109
110namespace MemberEnum {
111 struct WithMemberEnum {
112 enum E { A = 42 };
113 } wme;
114
Richard Smith0765bec2011-12-09 23:00:37 +0000115 static_assert(wme.A == 42, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000116}
117
118namespace DefaultArguments {
119
120const int z = int();
121constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
122 return a + b + *c + d;
123}
124const int four = 4;
125constexpr int eight = 8;
126constexpr const int twentyseven = 27;
Richard Smith0765bec2011-12-09 23:00:37 +0000127static_assert(Sum() == 0, "");
128static_assert(Sum(1) == 1, "");
129static_assert(Sum(1, four) == 5, "");
130static_assert(Sum(1, eight, &twentyseven) == 36, "");
131static_assert(Sum(1, 2, &four, eight) == 15, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000132
133}
134
135namespace Ellipsis {
136
137// Note, values passed through an ellipsis can't actually be used.
138constexpr int F(int a, ...) { return a; }
Richard Smith0765bec2011-12-09 23:00:37 +0000139static_assert(F(0) == 0, "");
140static_assert(F(1, 0) == 1, "");
141static_assert(F(2, "test") == 2, "");
142static_assert(F(3, &F) == 3, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000143int k = 0;
Richard Smith0765bec2011-12-09 23:00:37 +0000144static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000145
146}
147
148namespace Recursion {
149 constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
Richard Smith0765bec2011-12-09 23:00:37 +0000150 static_assert(fib(11) == 89, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000151
152 constexpr int gcd_inner(int a, int b) {
153 return b == 0 ? a : gcd_inner(b, a % b);
154 }
155 constexpr int gcd(int a, int b) {
156 return gcd_inner(max(a, b), min(a, b));
157 }
158
Richard Smith0765bec2011-12-09 23:00:37 +0000159 static_assert(gcd(1749237, 5628959) == 7, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000160}
161
162namespace FunctionCast {
163 // When folding, we allow functions to be cast to different types. Such
164 // cast functions cannot be called, even if they're constexpr.
165 constexpr int f() { return 1; }
166 typedef double (*DoubleFn)();
167 typedef int (*IntFn)();
168 int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}}
169 int b[(int)IntFn(f)()]; // ok
170}
171
172namespace StaticMemberFunction {
173 struct S {
174 static constexpr int k = 42;
175 static constexpr int f(int n) { return n * k + 2; }
176 } s;
177
178 constexpr int n = s.f(19);
Richard Smith0765bec2011-12-09 23:00:37 +0000179 static_assert(S::f(19) == 800, "");
180 static_assert(s.f(19) == 800, "");
181 static_assert(n == 800, "");
Richard Smithce40ad62011-11-12 22:28:03 +0000182
183 constexpr int (*sf1)(int) = &S::f;
184 constexpr int (*sf2)(int) = &s.f;
185 constexpr const int *sk = &s.k;
Richard Smithe97cbd72011-11-11 04:05:33 +0000186}
187
188namespace ParameterScopes {
189
190 const int k = 42;
191 constexpr const int &ObscureTheTruth(const int &a) { return a; }
192 constexpr const int &MaybeReturnJunk(bool b, const int a) {
193 return ObscureTheTruth(b ? a : k);
194 }
Richard Smith0765bec2011-12-09 23:00:37 +0000195 static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
Richard Smithe97cbd72011-11-11 04:05:33 +0000196 constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}}
197
198 constexpr const int MaybeReturnNonstaticRef(bool b, const int a) {
199 // If ObscureTheTruth returns a reference to 'a', the result is not a
200 // constant expression even though 'a' is still in scope.
201 return ObscureTheTruth(b ? a : k);
202 }
Richard Smith0765bec2011-12-09 23:00:37 +0000203 static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
Richard Smithe97cbd72011-11-11 04:05:33 +0000204 constexpr int b = MaybeReturnNonstaticRef(true, 0); // expected-error {{constant expression}}
205
206 constexpr int InternalReturnJunk(int n) {
207 // FIXME: We should reject this: it never produces a constant expression.
208 return MaybeReturnJunk(true, n);
209 }
210 constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}}
211
212 constexpr int LToR(int &n) { return n; }
213 constexpr int GrabCallersArgument(bool which, int a, int b) {
214 return LToR(which ? b : a);
215 }
Richard Smith0765bec2011-12-09 23:00:37 +0000216 static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
217 static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000218
219}
220
221namespace Pointers {
222
223 constexpr int f(int n, const int *a, const int *b, const int *c) {
224 return n == 0 ? 0 : *a + f(n-1, b, c, a);
225 }
226
227 const int x = 1, y = 10, z = 100;
Richard Smith0765bec2011-12-09 23:00:37 +0000228 static_assert(f(23, &x, &y, &z) == 788, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000229
230 constexpr int g(int n, int a, int b, int c) {
231 return f(n, &a, &b, &c);
232 }
Richard Smith0765bec2011-12-09 23:00:37 +0000233 static_assert(g(23, x, y, z) == 788, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000234
235}
236
237namespace FunctionPointers {
238
239 constexpr int Double(int n) { return 2 * n; }
240 constexpr int Triple(int n) { return 3 * n; }
241 constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
242 constexpr int Quadruple(int n) { return Twice(Double, n); }
243 constexpr auto Select(int n) -> int (*)(int) {
244 return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
245 }
246 constexpr int Apply(int (*F)(int), int n) { return F(n); }
247
Richard Smith0765bec2011-12-09 23:00:37 +0000248 static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000249
250 constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}}
251
252}
253
254namespace PointerComparison {
255
256int x, y;
Richard Smith0765bec2011-12-09 23:00:37 +0000257static_assert(&x == &y, "false"); // expected-error {{false}}
258static_assert(&x != &y, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000259constexpr bool g1 = &x == &y;
260constexpr bool g2 = &x != &y;
261constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
262constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
263constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
264constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
265
266struct S { int x, y; } s;
Richard Smith0765bec2011-12-09 23:00:37 +0000267static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
268static_assert(&s.x != &s.y, "");
269static_assert(&s.x <= &s.y, "");
270static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
271static_assert(&s.x < &s.y, "");
272static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000273
Richard Smith0765bec2011-12-09 23:00:37 +0000274static_assert(0 == &y, "false"); // expected-error {{false}}
275static_assert(0 != &y, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000276constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
277constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
278constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
279constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
280
Richard Smith0765bec2011-12-09 23:00:37 +0000281static_assert(&x == 0, "false"); // expected-error {{false}}
282static_assert(&x != 0, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000283constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
284constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
285constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
286constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
287
Richard Smith0765bec2011-12-09 23:00:37 +0000288static_assert(&x == &x, "");
289static_assert(&x != &x, "false"); // expected-error {{false}}
290static_assert(&x <= &x, "");
291static_assert(&x >= &x, "");
292static_assert(&x < &x, "false"); // expected-error {{false}}
293static_assert(&x > &x, "false"); // expected-error {{false}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000294
295constexpr S* sptr = &s;
296// FIXME: This is not a constant expression; check we reject this and move this
297// test elsewhere.
298constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr);
299
300extern char externalvar[];
301// FIXME: This is not a constant expression; check we reject this and move this
302// test elsewhere.
Richard Smith0765bec2011-12-09 23:00:37 +0000303constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000304constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
Richard Smith0765bec2011-12-09 23:00:37 +0000305static_assert(0 != "foo", "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000306
307}
308
309namespace MaterializeTemporary {
310
311constexpr int f(const int &r) { return r; }
312constexpr int n = f(1);
313
314constexpr bool same(const int &a, const int &b) { return &a == &b; }
315constexpr bool sameTemporary(const int &n) { return same(n, n); }
316
Richard Smith0765bec2011-12-09 23:00:37 +0000317static_assert(n, "");
318static_assert(!same(4, 4), "");
319static_assert(same(n, n), "");
320static_assert(sameTemporary(9), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000321
322}
323
324constexpr int strcmp_ce(const char *p, const char *q) {
325 return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
326}
327
328namespace StringLiteral {
329
330// FIXME: Refactor this once we support constexpr templates.
331constexpr int MangleChars(const char *p) {
332 return *p + 3 * (*p ? MangleChars(p+1) : 0);
333}
334constexpr int MangleChars(const char16_t *p) {
335 return *p + 3 * (*p ? MangleChars(p+1) : 0);
336}
337constexpr int MangleChars(const char32_t *p) {
338 return *p + 3 * (*p ? MangleChars(p+1) : 0);
339}
340
Richard Smith0765bec2011-12-09 23:00:37 +0000341static_assert(MangleChars("constexpr!") == 1768383, "");
342static_assert(MangleChars(u"constexpr!") == 1768383, "");
343static_assert(MangleChars(U"constexpr!") == 1768383, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000344
345constexpr char c0 = "nought index"[0];
346constexpr char c1 = "nice index"[10];
Matt Beaumont-Gay63be1912011-11-24 00:27:38 +0000347constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}}
348constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000349constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}}
350
351constexpr const char *p = "test" + 2;
Richard Smith0765bec2011-12-09 23:00:37 +0000352static_assert(*p == 's', "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000353
354constexpr const char *max_iter(const char *a, const char *b) {
355 return *a < *b ? b : a;
356}
357constexpr const char *max_element(const char *a, const char *b) {
358 return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
359}
360
361constexpr const char *begin(const char (&arr)[45]) { return arr; }
362constexpr const char *end(const char (&arr)[45]) { return arr + 45; }
363
364constexpr char str[] = "the quick brown fox jumped over the lazy dog";
365constexpr const char *max = max_element(begin(str), end(str));
Richard Smith0765bec2011-12-09 23:00:37 +0000366static_assert(*max == 'z', "");
367static_assert(max == str + 38, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000368
Richard Smith0765bec2011-12-09 23:00:37 +0000369static_assert(strcmp_ce("hello world", "hello world") == 0, "");
370static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
371static_assert(strcmp_ce("constexpr", "test") < 0, "");
372static_assert(strcmp_ce("", " ") < 0, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000373
374}
375
376namespace Array {
377
378// FIXME: Use templates for these once we support constexpr templates.
379constexpr int Sum(const int *begin, const int *end) {
380 return begin == end ? 0 : *begin + Sum(begin+1, end);
381}
382constexpr const int *begin(const int (&xs)[5]) { return xs; }
383constexpr const int *end(const int (&xs)[5]) { return xs + 5; }
384
385constexpr int xs[] = { 1, 2, 3, 4, 5 };
386constexpr int ys[] = { 5, 4, 3, 2, 1 };
387constexpr int sum_xs = Sum(begin(xs), end(xs));
Richard Smith0765bec2011-12-09 23:00:37 +0000388static_assert(sum_xs == 15, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000389
390constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
391 const int *xs, const int *ys, int c) {
392 return n ? F(*xs, *ys, ZipFoldR(F, n-1, xs+1, ys+1, c)) : c;
393}
394constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
395constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
Richard Smith0765bec2011-12-09 23:00:37 +0000396static_assert(InnerProduct == 35, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000397
398constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
399constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
Richard Smith0765bec2011-12-09 23:00:37 +0000400static_assert(DiffProd == 8, "");
401static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // expected-error {{constant expression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000402
403constexpr const int *p = xs + 3;
404constexpr int xs4 = p[1]; // ok
405constexpr int xs5 = p[2]; // expected-error {{constant expression}}
406constexpr int xs0 = p[-3]; // ok
407constexpr int xs_1 = p[-4]; // expected-error {{constant expression}}
408
409constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
Richard Smith0765bec2011-12-09 23:00:37 +0000410static_assert(zs[0][0][0][0] == 1, "");
411static_assert(zs[1][1][1][1] == 16, "");
412static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}}
413static_assert((&zs[0][0][0][2])[-1] == 2, "");
414static_assert(**(**(zs + 1) + 1) == 11, "");
415static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000416
417constexpr int arr[40] = { 1, 2, 3, [8] = 4 };
418constexpr int SumNonzero(const int *p) {
419 return *p + (*p ? SumNonzero(p+1) : 0);
420}
421constexpr int CountZero(const int *p, const int *q) {
422 return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
423}
Richard Smith0765bec2011-12-09 23:00:37 +0000424static_assert(SumNonzero(arr) == 6, "");
425static_assert(CountZero(arr, arr + 40) == 36, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000426
Richard Smith027bf112011-11-17 22:56:20 +0000427struct ArrayElem {
428 constexpr ArrayElem() : n(0) {}
429 int n;
430 constexpr int f() { return n; }
431};
432struct ArrayRVal {
433 constexpr ArrayRVal() {}
434 ArrayElem elems[10];
435};
Richard Smith0765bec2011-12-09 23:00:37 +0000436static_assert(ArrayRVal().elems[3].f() == 0, "");
Richard Smith027bf112011-11-17 22:56:20 +0000437
Richard Smithe97cbd72011-11-11 04:05:33 +0000438}
439
440namespace DependentValues {
441
442struct I { int n; typedef I V[10]; };
443I::V x, y;
444template<bool B> struct S {
445 int k;
446 void f() {
447 I::V &cells = B ? x : y;
448 I &i = cells[k];
449 switch (i.n) {}
450 }
451};
452
453}
454
455namespace Class {
456
457struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
458constexpr int fn(const A &a) { return a.k; }
Richard Smith0765bec2011-12-09 23:00:37 +0000459static_assert(fn(A(4,5)) == 9, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000460
461struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
462struct C {
463 constexpr C(C *this_) : m(42), n(this_->m) {} // ok
464 int m, n;
465};
466struct D {
467 C c;
468 constexpr D() : c(&c) {}
469};
Richard Smith0765bec2011-12-09 23:00:37 +0000470static_assert(D().c.n == 42, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000471
472struct E {
473 constexpr E() : p(&p) {}
474 void *p;
475};
476constexpr const E &e1 = E(); // expected-error {{constant expression}}
477// This is a constant expression if we elide the copy constructor call, and
478// is not a constant expression if we don't! But we do, so it is.
479// FIXME: The move constructor is not currently implicitly defined as constexpr.
480// We notice this when evaluating an expression which uses it, but not when
481// checking its initializer.
482constexpr E e2 = E(); // unexpected-error {{constant expression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000483static_assert(e2.p == &e2.p, ""); // unexpected-error {{constant expression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000484// FIXME: We don't pass through the fact that 'this' is ::e3 when checking the
485// initializer of this declaration.
486constexpr E e3; // unexpected-error {{constant expression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000487static_assert(e3.p == &e3.p, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000488
489extern const class F f;
490struct F {
491 constexpr F() : p(&f.p) {}
492 const void *p;
493};
494constexpr F f = F();
495
496struct G {
497 struct T {
498 constexpr T(T *p) : u1(), u2(p) {}
499 union U1 {
500 constexpr U1() {}
501 int a, b = 42;
502 } u1;
503 union U2 {
504 constexpr U2(T *p) : c(p->u1.b) {}
505 int c, d;
506 } u2;
507 } t;
508 constexpr G() : t(&t) {}
509} constexpr g;
510
Richard Smith0765bec2011-12-09 23:00:37 +0000511static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}}
512static_assert(g.t.u1.b == 42, "");
513static_assert(g.t.u2.c == 42, "");
514static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000515
516struct S {
517 int a, b;
518 const S *p;
519 double d;
520 const char *q;
521
522 constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
523};
524
525S global(43, &global);
526
Richard Smith0765bec2011-12-09 23:00:37 +0000527static_assert(S(15, &global).b == 15, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000528
529constexpr bool CheckS(const S &s) {
530 return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
531}
Richard Smith0765bec2011-12-09 23:00:37 +0000532static_assert(CheckS(S(27, &global)), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000533
534struct Arr {
535 char arr[3];
536 constexpr Arr() : arr{'x', 'y', 'z'} {}
537};
538constexpr int hash(Arr &&a) {
539 return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
540}
541constexpr int k = hash(Arr());
Richard Smith0765bec2011-12-09 23:00:37 +0000542static_assert(k == 0x007a7978, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000543
544
545struct AggregateInit {
546 const char &c;
547 int n;
548 double d;
549 int arr[5];
550 void *p;
551};
552
553constexpr AggregateInit agg1 = { "hello"[0] };
554
Richard Smith0765bec2011-12-09 23:00:37 +0000555static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
556static_assert(agg1.n == 0, "");
557static_assert(agg1.d == 0.0, "");
558static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}}
559static_assert(agg1.arr[0] == 0, "");
560static_assert(agg1.arr[4] == 0, "");
561static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}}
562static_assert(agg1.p == nullptr, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000563
564namespace SimpleDerivedClass {
565
566struct B {
567 constexpr B(int n) : a(n) {}
568 int a;
569};
570struct D : B {
571 constexpr D(int n) : B(n) {}
572};
573constexpr D d(3);
Richard Smith0765bec2011-12-09 23:00:37 +0000574static_assert(d.a == 3, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000575
576}
577
Richard Smith027bf112011-11-17 22:56:20 +0000578struct Bottom { constexpr Bottom() {} };
579struct Base : Bottom {
Richard Smithe97cbd72011-11-11 04:05:33 +0000580 constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
581 int a;
582 const char *b;
583};
Richard Smith027bf112011-11-17 22:56:20 +0000584struct Base2 : Bottom {
Richard Smithe97cbd72011-11-11 04:05:33 +0000585 constexpr Base2(const int &r) : r(r) {}
586 int q = 123;
587 // FIXME: When we track the global for which we are computing the initializer,
588 // use a reference here.
589 //const int &r;
590 int r;
591};
592struct Derived : Base, Base2 {
593 constexpr Derived() : Base(76), Base2(a) {}
594 int c = r + b[1];
595};
596
597constexpr bool operator==(const Base &a, const Base &b) {
598 return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
599}
600
601constexpr Base base;
602constexpr Base base2(76);
603constexpr Derived derived;
Richard Smith0765bec2011-12-09 23:00:37 +0000604static_assert(derived.a == 76, "");
605static_assert(derived.b[2] == 's', "");
606static_assert(derived.c == 76 + 'e', "");
607static_assert(derived.q == 123, "");
608static_assert(derived.r == 76, "");
609static_assert(&derived.r == &derived.a, ""); // expected-error {{}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000610
Richard Smith0765bec2011-12-09 23:00:37 +0000611static_assert(!(derived == base), "");
612static_assert(derived == base2, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000613
Richard Smith027bf112011-11-17 22:56:20 +0000614constexpr Bottom &bot1 = (Base&)derived;
615constexpr Bottom &bot2 = (Base2&)derived;
Richard Smith0765bec2011-12-09 23:00:37 +0000616static_assert(&bot1 != &bot2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000617
618constexpr Bottom *pb1 = (Base*)&derived;
619constexpr Bottom *pb2 = (Base2*)&derived;
Richard Smith0765bec2011-12-09 23:00:37 +0000620static_assert(pb1 != pb2, "");
621static_assert(pb1 == &bot1, "");
622static_assert(pb2 == &bot2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000623
624constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}}
625constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}}
626constexpr Base2 &ok2 = (Base2&)bot2;
Richard Smith0765bec2011-12-09 23:00:37 +0000627static_assert(&ok2 == &derived, "");
Richard Smith027bf112011-11-17 22:56:20 +0000628
629constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}}
630constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}}
631constexpr Base2 *pok2 = (Base2*)pb2;
Richard Smith0765bec2011-12-09 23:00:37 +0000632static_assert(pok2 == &derived, "");
633static_assert(&ok2 == pok2, "");
634static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
635static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000636
637constexpr Base *nullB = 42 - 6 * 7;
Richard Smith0765bec2011-12-09 23:00:37 +0000638static_assert((Bottom*)nullB == 0, "");
639static_assert((Derived*)nullB == 0, "");
640static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
Richard Smith027bf112011-11-17 22:56:20 +0000641
642}
643
644namespace Temporaries {
645
646struct S {
647 constexpr S() {}
648 constexpr int f();
649};
650struct T : S {
651 constexpr T(int n) : S(), n(n) {}
652 int n;
653};
654constexpr int S::f() {
655 // 'this' must be the postfix-expression in a class member access expression,
656 // so we can't just use
657 // return static_cast<T*>(this)->n;
658 return this->*(int(S::*))&T::n;
659}
660// The T temporary is implicitly cast to an S subobject, but we can recover the
661// T full-object via a base-to-derived cast, or a derived-to-base-casted member
662// pointer.
Richard Smith0765bec2011-12-09 23:00:37 +0000663static_assert(T(3).f() == 3, "");
Richard Smith027bf112011-11-17 22:56:20 +0000664
665constexpr int f(const S &s) {
666 return static_cast<const T&>(s).n;
667}
668constexpr int n = f(T(5));
Richard Smith0765bec2011-12-09 23:00:37 +0000669static_assert(f(T(5)) == 5, "");
Richard Smith027bf112011-11-17 22:56:20 +0000670
Richard Smithe97cbd72011-11-11 04:05:33 +0000671}
672
673namespace Union {
674
675union U {
676 int a;
677 int b;
678};
679
680constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } };
Richard Smith0765bec2011-12-09 23:00:37 +0000681static_assert(u[0].a == 0, "");
682static_assert(u[0].b, ""); // expected-error {{constant expression}}
683static_assert(u[1].b == 1, "");
684static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}}
685static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}}
686static_assert((&(u[1]) + 1 + 1)->b == 3, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000687
688}
689
Richard Smith027bf112011-11-17 22:56:20 +0000690namespace MemberPointer {
691 struct A {
692 constexpr A(int n) : n(n) {}
693 int n;
694 constexpr int f() { return n + 3; }
695 };
696 constexpr A a(7);
Richard Smith0765bec2011-12-09 23:00:37 +0000697 static_assert(A(5).*&A::n == 5, "");
698 static_assert((&a)->*&A::n == 7, "");
699 static_assert((A(8).*&A::f)() == 11, "");
700 static_assert(((&a)->*&A::f)() == 10, "");
Richard Smith027bf112011-11-17 22:56:20 +0000701
702 struct B : A {
703 constexpr B(int n, int m) : A(n), m(m) {}
704 int m;
705 constexpr int g() { return n + m + 1; }
706 };
707 constexpr B b(9, 13);
Richard Smith0765bec2011-12-09 23:00:37 +0000708 static_assert(B(4, 11).*&A::n == 4, "");
709 static_assert(B(4, 11).*&B::m == 11, "");
710 static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
711 static_assert((&b)->*&A::n == 9, "");
712 static_assert((&b)->*&B::m == 13, "");
713 static_assert((&b)->*(int(A::*))&B::m == 13, "");
714 static_assert((B(4, 11).*&A::f)() == 7, "");
715 static_assert((B(4, 11).*&B::g)() == 16, "");
716 static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
717 static_assert(((&b)->*&A::f)() == 12, "");
718 static_assert(((&b)->*&B::g)() == 23, "");
719 static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
Richard Smith027bf112011-11-17 22:56:20 +0000720
721 struct S {
722 constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
723 m(m), n(n), pf(pf), pn(pn) {}
724 constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
725
726 constexpr int f() { return this->*pn; }
727 virtual int g() const;
728
729 int m, n;
730 int (S::*pf)() const;
731 int S::*pn;
732 };
733
734 constexpr int S::*pm = &S::m;
735 constexpr int S::*pn = &S::n;
736 constexpr int (S::*pf)() const = &S::f;
737 constexpr int (S::*pg)() const = &S::g;
738
739 constexpr S s(2, 5, &S::f, &S::m);
740
Richard Smith0765bec2011-12-09 23:00:37 +0000741 static_assert((s.*&S::f)() == 2, "");
742 static_assert((s.*s.pf)() == 2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000743
744 template<int n> struct T : T<n-1> {};
745 template<> struct T<0> { int n; };
746 template<> struct T<30> : T<29> { int m; };
747
748 T<17> t17;
749 T<30> t30;
750
751 constexpr int (T<10>::*deepn) = &T<0>::n;
Richard Smith0765bec2011-12-09 23:00:37 +0000752 static_assert(&(t17.*deepn) == &t17.n, "");
Richard Smith027bf112011-11-17 22:56:20 +0000753
754 constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
755 constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000756 static_assert(&(t30.*deepm) == &t30.m, "");
Richard Smith027bf112011-11-17 22:56:20 +0000757
758 constexpr T<5> *p17_5 = &t17;
759 constexpr T<13> *p17_13 = (T<13>*)p17_5;
760 constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000761 static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
762 static_assert(&(p17_13->*deepn) == &t17.n, "");
Richard Smith027bf112011-11-17 22:56:20 +0000763 constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
764
765 constexpr T<5> *p30_5 = &t30;
766 constexpr T<23> *p30_23 = (T<23>*)p30_5;
767 constexpr T<13> *p30_13 = p30_23;
Richard Smith0765bec2011-12-09 23:00:37 +0000768 static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
769 static_assert(&(p30_13->*deepn) == &t30.n, "");
770 static_assert(&(p30_23->*deepn) == &t30.n, "");
771 static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
772 static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
773 static_assert(&(p30_23->*deepm) == &t30.m, "");
Richard Smith027bf112011-11-17 22:56:20 +0000774}
775
776namespace ArrayBaseDerived {
777
778 struct Base {
779 constexpr Base() {}
780 int n = 0;
781 };
782 struct Derived : Base {
783 constexpr Derived() {}
784 constexpr const int *f() { return &n; }
785 };
786
787 constexpr Derived a[10];
788 constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
789 constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
Richard Smith0765bec2011-12-09 23:00:37 +0000790 static_assert(pb3 == pd3, "");
Richard Smith027bf112011-11-17 22:56:20 +0000791
792 // pb3 does not point to an array element.
793 constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
794 constexpr int pb4n = pb4->n; // expected-error {{constant expression}}
795 constexpr Base *err_pb5 = pb3 + 2; // FIXME: reject this.
796 constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}}
797 constexpr Base *err_pb2 = pb3 - 1; // FIXME: reject this.
798 constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}}
799 constexpr Base *pb3a = pb4 - 1;
800
801 // pb4 does not point to a Derived.
802 constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}}
803 constexpr Derived *pd3a = (Derived*)pb3a;
804 constexpr int pd3n = pd3a->n;
805
806 // pd3a still points to the Derived array.
807 constexpr Derived *pd6 = pd3a + 3;
Richard Smith0765bec2011-12-09 23:00:37 +0000808 static_assert(pd6 == &a[6], "");
Richard Smith027bf112011-11-17 22:56:20 +0000809 constexpr Derived *pd9 = pd6 + 3;
810 constexpr Derived *pd10 = pd6 + 4;
811 constexpr int pd9n = pd9->n; // ok
812 constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}}
813 constexpr int pd0n = pd10[-10].n;
814 constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}}
815
816 constexpr Base *pb9 = pd9;
817 constexpr const int *(Base::*pfb)() const =
818 static_cast<const int *(Base::*)() const>(&Derived::f);
Richard Smith0765bec2011-12-09 23:00:37 +0000819 static_assert((pb9->*pfb)() == &a[9].n, "");
Richard Smith027bf112011-11-17 22:56:20 +0000820}
821
Richard Smithe97cbd72011-11-11 04:05:33 +0000822namespace Complex {
823
824class complex {
825 int re, im;
826public:
827 constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
828 constexpr complex(const complex &o) : re(o.re), im(o.im) {}
829 constexpr complex operator-() const { return complex(-re, -im); }
830 friend constexpr complex operator+(const complex &l, const complex &r) {
831 return complex(l.re + r.re, l.im + r.im);
832 }
833 friend constexpr complex operator-(const complex &l, const complex &r) {
834 return l + -r;
835 }
836 friend constexpr complex operator*(const complex &l, const complex &r) {
837 return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
838 }
839 friend constexpr bool operator==(const complex &l, const complex &r) {
840 return l.re == r.re && l.im == r.im;
841 }
842 constexpr bool operator!=(const complex &r) const {
843 return re != r.re || im != r.im;
844 }
845 constexpr int real() const { return re; }
846 constexpr int imag() const { return im; }
847};
848
849constexpr complex i = complex(0, 1);
850constexpr complex k = (3 + 4*i) * (6 - 4*i);
Richard Smith0765bec2011-12-09 23:00:37 +0000851static_assert(complex(1,0).real() == 1, "");
852static_assert(complex(1,0).imag() == 0, "");
853static_assert(((complex)1).imag() == 0, "");
854static_assert(k.real() == 34, "");
855static_assert(k.imag() == 12, "");
856static_assert(k - 34 == 12*i, "");
857static_assert((complex)1 == complex(1), "");
858static_assert((complex)1 != complex(0, 1), "");
859static_assert(complex(1) == complex(1), "");
860static_assert(complex(1) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000861constexpr complex makeComplex(int re, int im) { return complex(re, im); }
Richard Smith0765bec2011-12-09 23:00:37 +0000862static_assert(makeComplex(1,0) == complex(1), "");
863static_assert(makeComplex(1,0) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000864
865class complex_wrap : public complex {
866public:
867 constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
868 constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
869};
870
Richard Smith0765bec2011-12-09 23:00:37 +0000871static_assert((complex_wrap)1 == complex(1), "");
872static_assert((complex)1 != complex_wrap(0, 1), "");
873static_assert(complex(1) == complex_wrap(1), "");
874static_assert(complex_wrap(1) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000875constexpr complex_wrap makeComplexWrap(int re, int im) {
876 return complex_wrap(re, im);
877}
Richard Smith0765bec2011-12-09 23:00:37 +0000878static_assert(makeComplexWrap(1,0) == complex(1), "");
879static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000880
881}