blob: e1b3781f9f4ea6b4debfa5d475535be91de0dbdf [file] [log] [blame]
Richard Smithc216a012011-12-12 12:46:16 +00001// RUN: %clang_cc1 -triple i686-linux -fsyntax-only -verify -std=c++11 -pedantic %s -Wno-comment
Richard Smith59efe262011-11-11 04:05:33 +00002
Richard Smith59efe262011-11-11 04:05:33 +00003namespace StaticAssertFoldTest {
4
5int x;
Richard Smith9eed49c2011-12-09 23:00:37 +00006static_assert(++x, "test"); // expected-error {{not an integral constant expression}}
7static_assert(false, "test"); // expected-error {{test}}
Richard Smith59efe262011-11-11 04:05:33 +00008
9}
10
Richard Smith1d238ea2011-12-21 02:55:12 +000011typedef 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; }
Richard Smith59efe262011-11-11 04:05:33 +000022
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;
Richard Smith9eed49c2011-12-09 23:00:37 +000040 static_assert((void*)p != (void*)q, "");
41 static_assert((A*)p == (A*)q, "");
42 static_assert((Aa*)p != (Aa*)q, "");
Richard Smith59efe262011-11-11 04:05:33 +000043
44 constexpr B &pp = d;
45 constexpr C &qq = d;
Richard Smith9eed49c2011-12-09 23:00:37 +000046 static_assert((void*)&pp != (void*)&qq, "");
47 static_assert(&(A&)pp == &(A&)qq, "");
48 static_assert(&(Aa&)pp != &(Aa&)qq, "");
Richard Smith59efe262011-11-11 04:05:33 +000049
50 constexpr V *v = p;
51 constexpr V *w = q;
52 constexpr V *x = (A*)p;
Richard Smith9eed49c2011-12-09 23:00:37 +000053 static_assert(v == w, "");
54 static_assert(v == x, "");
Richard Smith59efe262011-11-11 04:05:33 +000055
Richard Smith9eed49c2011-12-09 23:00:37 +000056 static_assert((U*)&d == p, "");
57 static_assert((U*)&d == q, "");
58 static_assert((U*)&d == v, "");
59 static_assert((U*)&d == w, "");
60 static_assert((U*)&d == x, "");
Richard Smith59efe262011-11-11 04:05:33 +000061
62 struct X {};
63 struct Y1 : virtual X {};
64 struct Y2 : X {};
65 struct Z : Y1, Y2 {};
66 Z z;
Richard Smith9eed49c2011-12-09 23:00:37 +000067 static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
Richard Smith59efe262011-11-11 04:05:33 +000068
69}
70
Richard Smithf64699e2011-11-11 08:28:03 +000071namespace 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
Richard Smith59efe262011-11-11 04:05:33 +000084namespace TemplateArgumentConversion {
85 template<int n> struct IntParam {};
86
87 using IntParam0 = IntParam<0>;
Richard Smith1d238ea2011-12-21 02:55:12 +000088 using IntParam0 = IntParam<id(0)>;
Richard Smith61802452011-12-22 02:22:31 +000089 using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}}
Richard Smith59efe262011-11-11 04:05:33 +000090}
91
92namespace CaseStatements {
93 void f(int n) {
94 switch (n) {
Richard Smith8ef7b202012-01-18 23:55:52 +000095 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'}}
Richard Smith59efe262011-11-11 04:05:33 +000097 return;
98 }
99 }
100}
101
102extern int &Recurse1;
Richard Smith099e7f62011-12-19 06:19:21 +0000103int &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}}
Richard Smith59efe262011-11-11 04:05:33 +0000111
112namespace MemberEnum {
113 struct WithMemberEnum {
114 enum E { A = 42 };
115 } wme;
116
Richard Smith9eed49c2011-12-09 23:00:37 +0000117 static_assert(wme.A == 42, "");
Richard Smith59efe262011-11-11 04:05:33 +0000118}
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;
Richard Smith9eed49c2011-12-09 23:00:37 +0000129static_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, "");
Richard Smith59efe262011-11-11 04:05:33 +0000134
135}
136
137namespace Ellipsis {
138
139// Note, values passed through an ellipsis can't actually be used.
140constexpr int F(int a, ...) { return a; }
Richard Smith9eed49c2011-12-09 23:00:37 +0000141static_assert(F(0) == 0, "");
142static_assert(F(1, 0) == 1, "");
143static_assert(F(2, "test") == 2, "");
144static_assert(F(3, &F) == 3, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000145int k = 0; // expected-note {{here}}
146static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
Richard Smith59efe262011-11-11 04:05:33 +0000147
148}
149
150namespace Recursion {
151 constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
Richard Smith9eed49c2011-12-09 23:00:37 +0000152 static_assert(fib(11) == 89, "");
Richard Smith59efe262011-11-11 04:05:33 +0000153
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
Richard Smith9eed49c2011-12-09 23:00:37 +0000161 static_assert(gcd(1749237, 5628959) == 7, "");
Richard Smith59efe262011-11-11 04:05:33 +0000162}
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)();
Richard Smithd7c56e12011-12-29 21:57:33 +0000170 int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}}
Richard Smith59efe262011-11-11 04:05:33 +0000171 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);
Richard Smith9eed49c2011-12-09 23:00:37 +0000181 static_assert(S::f(19) == 800, "");
182 static_assert(s.f(19) == 800, "");
183 static_assert(n == 800, "");
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000184
185 constexpr int (*sf1)(int) = &S::f;
186 constexpr int (*sf2)(int) = &s.f;
187 constexpr const int *sk = &s.k;
Richard Smith59efe262011-11-11 04:05:33 +0000188}
189
190namespace ParameterScopes {
191
192 const int k = 42;
Richard Smith099e7f62011-12-19 06:19:21 +0000193 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)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000196 }
Richard Smith9eed49c2011-12-09 23:00:37 +0000197 static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
Richard Smith099e7f62011-12-19 06:19:21 +0000198 constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnJunk(1, 0)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000199
Richard Smith099e7f62011-12-19 06:19:21 +0000200 constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { // expected-note {{here}}
Richard Smith59efe262011-11-11 04:05:33 +0000201 // If ObscureTheTruth returns a reference to 'a', the result is not a
202 // constant expression even though 'a' is still in scope.
Richard Smith099e7f62011-12-19 06:19:21 +0000203 return ObscureTheTruth(b ? a : k); // expected-note {{in call to 'ObscureTheTruth(a)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000204 }
Richard Smith9eed49c2011-12-09 23:00:37 +0000205 static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
Richard Smith099e7f62011-12-19 06:19:21 +0000206 constexpr int b = MaybeReturnNonstaticRef(true, 0); // expected-error {{constant expression}} expected-note {{in call to 'MaybeReturnNonstaticRef(1, 0)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000207
208 constexpr int InternalReturnJunk(int n) {
209 // FIXME: We should reject this: it never produces a constant expression.
Richard Smith099e7f62011-12-19 06:19:21 +0000210 return MaybeReturnJunk(true, n); // expected-note {{in call to 'MaybeReturnJunk(1, 0)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000211 }
Richard Smith099e7f62011-12-19 06:19:21 +0000212 constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000213
214 constexpr int LToR(int &n) { return n; }
215 constexpr int GrabCallersArgument(bool which, int a, int b) {
216 return LToR(which ? b : a);
217 }
Richard Smith9eed49c2011-12-09 23:00:37 +0000218 static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
219 static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
Richard Smith59efe262011-11-11 04:05:33 +0000220
221}
222
223namespace Pointers {
224
225 constexpr int f(int n, const int *a, const int *b, const int *c) {
226 return n == 0 ? 0 : *a + f(n-1, b, c, a);
227 }
228
229 const int x = 1, y = 10, z = 100;
Richard Smith9eed49c2011-12-09 23:00:37 +0000230 static_assert(f(23, &x, &y, &z) == 788, "");
Richard Smith59efe262011-11-11 04:05:33 +0000231
232 constexpr int g(int n, int a, int b, int c) {
233 return f(n, &a, &b, &c);
234 }
Richard Smith9eed49c2011-12-09 23:00:37 +0000235 static_assert(g(23, x, y, z) == 788, "");
Richard Smith59efe262011-11-11 04:05:33 +0000236
237}
238
239namespace FunctionPointers {
240
241 constexpr int Double(int n) { return 2 * n; }
242 constexpr int Triple(int n) { return 3 * n; }
243 constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
244 constexpr int Quadruple(int n) { return Twice(Double, n); }
245 constexpr auto Select(int n) -> int (*)(int) {
246 return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
247 }
Richard Smith099e7f62011-12-19 06:19:21 +0000248 constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}}
Richard Smith59efe262011-11-11 04:05:33 +0000249
Richard Smith9eed49c2011-12-09 23:00:37 +0000250 static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
Richard Smith59efe262011-11-11 04:05:33 +0000251
Richard Smith099e7f62011-12-19 06:19:21 +0000252 constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000253
254}
255
256namespace PointerComparison {
257
258int x, y;
Richard Smith9eed49c2011-12-09 23:00:37 +0000259static_assert(&x == &y, "false"); // expected-error {{false}}
260static_assert(&x != &y, "");
Richard Smith59efe262011-11-11 04:05:33 +0000261constexpr bool g1 = &x == &y;
262constexpr bool g2 = &x != &y;
263constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
264constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
265constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
266constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
267
268struct S { int x, y; } s;
Richard Smith9eed49c2011-12-09 23:00:37 +0000269static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
270static_assert(&s.x != &s.y, "");
271static_assert(&s.x <= &s.y, "");
272static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
273static_assert(&s.x < &s.y, "");
274static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
Richard Smith59efe262011-11-11 04:05:33 +0000275
Richard Smith9eed49c2011-12-09 23:00:37 +0000276static_assert(0 == &y, "false"); // expected-error {{false}}
277static_assert(0 != &y, "");
Richard Smith59efe262011-11-11 04:05:33 +0000278constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
279constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
280constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
281constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
282
Richard Smith9eed49c2011-12-09 23:00:37 +0000283static_assert(&x == 0, "false"); // expected-error {{false}}
284static_assert(&x != 0, "");
Richard Smith59efe262011-11-11 04:05:33 +0000285constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
286constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
287constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
288constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
289
Richard Smith9eed49c2011-12-09 23:00:37 +0000290static_assert(&x == &x, "");
291static_assert(&x != &x, "false"); // expected-error {{false}}
292static_assert(&x <= &x, "");
293static_assert(&x >= &x, "");
294static_assert(&x < &x, "false"); // expected-error {{false}}
295static_assert(&x > &x, "false"); // expected-error {{false}}
Richard Smith59efe262011-11-11 04:05:33 +0000296
297constexpr S* sptr = &s;
Richard Smith099e7f62011-12-19 06:19:21 +0000298constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
Richard Smith59efe262011-11-11 04:05:33 +0000299
Richard Smithc216a012011-12-12 12:46:16 +0000300struct Str {
Richard Smithc216a012011-12-12 12:46:16 +0000301 int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
Richard Smith244ee7b2012-01-15 03:51:30 +0000302 expected-warning {{not an integral constant expression}} \
Richard Smith4cd9b8f2011-12-12 19:10:03 +0000303 expected-note {{dynamic_cast is not allowed in a constant expression}}
Richard Smithc216a012011-12-12 12:46:16 +0000304 int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
Richard Smith244ee7b2012-01-15 03:51:30 +0000305 expected-warning {{not an integral constant expression}} \
Richard Smith4cd9b8f2011-12-12 19:10:03 +0000306 expected-note {{reinterpret_cast is not allowed in a constant expression}}
Richard Smithc216a012011-12-12 12:46:16 +0000307 int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
Richard Smith244ee7b2012-01-15 03:51:30 +0000308 expected-warning {{not an integral constant expression}} \
Richard Smith60f24e72011-12-12 19:33:27 +0000309 expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
Richard Smithc216a012011-12-12 12:46:16 +0000310 int d : (S*)(42) == (S*)(42); // \
Richard Smith244ee7b2012-01-15 03:51:30 +0000311 expected-warning {{not an integral constant expression}} \
Richard Smith60f24e72011-12-12 19:33:27 +0000312 expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
Richard Smithc216a012011-12-12 12:46:16 +0000313 int e : (Str*)(sptr) == (Str*)(sptr); // \
Richard Smith244ee7b2012-01-15 03:51:30 +0000314 expected-warning {{not an integral constant expression}} \
Richard Smith60f24e72011-12-12 19:33:27 +0000315 expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
Richard Smithc216a012011-12-12 12:46:16 +0000316 int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \
Richard Smith244ee7b2012-01-15 03:51:30 +0000317 expected-warning {{not an integral constant expression}} \
Richard Smith60f24e72011-12-12 19:33:27 +0000318 expected-note {{cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
Richard Smithc216a012011-12-12 12:46:16 +0000319 int g : (S*)(void*)(sptr) == sptr; // \
Richard Smith244ee7b2012-01-15 03:51:30 +0000320 expected-warning {{not an integral constant expression}} \
Richard Smith4cd9b8f2011-12-12 19:10:03 +0000321 expected-note {{cast from 'void *' is not allowed in a constant expression}}
Richard Smithc216a012011-12-12 12:46:16 +0000322};
323
Richard Smith59efe262011-11-11 04:05:33 +0000324extern char externalvar[];
Richard Smith9eed49c2011-12-09 23:00:37 +0000325constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
Richard Smith59efe262011-11-11 04:05:33 +0000326constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000327static_assert(0 != "foo", "");
Richard Smith59efe262011-11-11 04:05:33 +0000328
329}
330
331namespace MaterializeTemporary {
332
333constexpr int f(const int &r) { return r; }
334constexpr int n = f(1);
335
336constexpr bool same(const int &a, const int &b) { return &a == &b; }
337constexpr bool sameTemporary(const int &n) { return same(n, n); }
338
Richard Smith9eed49c2011-12-09 23:00:37 +0000339static_assert(n, "");
340static_assert(!same(4, 4), "");
341static_assert(same(n, n), "");
342static_assert(sameTemporary(9), "");
Richard Smith59efe262011-11-11 04:05:33 +0000343
344}
345
346constexpr int strcmp_ce(const char *p, const char *q) {
347 return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
348}
349
350namespace StringLiteral {
351
Richard Smith1d238ea2011-12-21 02:55:12 +0000352template<typename Char>
353constexpr int MangleChars(const Char *p) {
Richard Smith59efe262011-11-11 04:05:33 +0000354 return *p + 3 * (*p ? MangleChars(p+1) : 0);
355}
356
Richard Smith9eed49c2011-12-09 23:00:37 +0000357static_assert(MangleChars("constexpr!") == 1768383, "");
Richard Smithec789162012-01-12 18:54:33 +0000358static_assert(MangleChars(u8"constexpr!") == 1768383, "");
359static_assert(MangleChars(L"constexpr!") == 1768383, "");
Richard Smith9eed49c2011-12-09 23:00:37 +0000360static_assert(MangleChars(u"constexpr!") == 1768383, "");
361static_assert(MangleChars(U"constexpr!") == 1768383, "");
Richard Smith59efe262011-11-11 04:05:33 +0000362
363constexpr char c0 = "nought index"[0];
364constexpr char c1 = "nice index"[10];
Richard Smith7098cbd2011-12-21 05:04:46 +0000365constexpr 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}}
Richard Smithb4e85ed2012-01-06 16:39:00 +0000366constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} expected-note {{cannot refer to element -1 of array of 15 elements}}
367constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} expected-note {{cast which performs the conversions of a reinterpret_cast}}
Richard Smith59efe262011-11-11 04:05:33 +0000368
369constexpr const char *p = "test" + 2;
Richard Smith9eed49c2011-12-09 23:00:37 +0000370static_assert(*p == 's', "");
Richard Smith59efe262011-11-11 04:05:33 +0000371
372constexpr const char *max_iter(const char *a, const char *b) {
373 return *a < *b ? b : a;
374}
375constexpr const char *max_element(const char *a, const char *b) {
376 return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
377}
378
Richard Smith59efe262011-11-11 04:05:33 +0000379constexpr char str[] = "the quick brown fox jumped over the lazy dog";
380constexpr const char *max = max_element(begin(str), end(str));
Richard Smith9eed49c2011-12-09 23:00:37 +0000381static_assert(*max == 'z', "");
382static_assert(max == str + 38, "");
Richard Smith59efe262011-11-11 04:05:33 +0000383
Richard Smith9eed49c2011-12-09 23:00:37 +0000384static_assert(strcmp_ce("hello world", "hello world") == 0, "");
385static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
386static_assert(strcmp_ce("constexpr", "test") < 0, "");
387static_assert(strcmp_ce("", " ") < 0, "");
Richard Smith59efe262011-11-11 04:05:33 +0000388
Richard Smith7098cbd2011-12-21 05:04:46 +0000389struct S {
390 int n : "foo"[4]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
391};
392
Richard Smith974c5f92011-12-22 01:07:19 +0000393struct T {
394 char c[6];
395 constexpr T() : c{"foo"} {}
396};
397constexpr T t;
398
399static_assert(t.c[0] == 'f', "");
400static_assert(t.c[1] == 'o', "");
401static_assert(t.c[2] == 'o', "");
402static_assert(t.c[3] == 0, "");
403static_assert(t.c[4] == 0, "");
404static_assert(t.c[5] == 0, "");
405static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
406
Richard Smithec789162012-01-12 18:54:33 +0000407struct U {
408 wchar_t chars[6];
409 int n;
410} constexpr u = { { L"test" }, 0 };
411static_assert(u.chars[2] == L's', "");
412
Richard Smith59efe262011-11-11 04:05:33 +0000413}
414
415namespace Array {
416
Richard Smith1d238ea2011-12-21 02:55:12 +0000417template<typename Iter>
418constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) {
Richard Smith59efe262011-11-11 04:05:33 +0000419 return begin == end ? 0 : *begin + Sum(begin+1, end);
420}
Richard Smith59efe262011-11-11 04:05:33 +0000421
422constexpr int xs[] = { 1, 2, 3, 4, 5 };
423constexpr int ys[] = { 5, 4, 3, 2, 1 };
424constexpr int sum_xs = Sum(begin(xs), end(xs));
Richard Smith9eed49c2011-12-09 23:00:37 +0000425static_assert(sum_xs == 15, "");
Richard Smith59efe262011-11-11 04:05:33 +0000426
427constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
428 const int *xs, const int *ys, int c) {
Richard Smithdaaefc52011-12-14 23:32:26 +0000429 return n ? F(
Richard Smith7098cbd2011-12-21 05:04:46 +0000430 *xs, // expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smith08d6e032011-12-16 19:06:07 +0000431 *ys,
432 ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
433 expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
434 expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
435 : c;
Richard Smith59efe262011-11-11 04:05:33 +0000436}
437constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
438constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
Richard Smith9eed49c2011-12-09 23:00:37 +0000439static_assert(InnerProduct == 35, "");
Richard Smith59efe262011-11-11 04:05:33 +0000440
441constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
442constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
Richard Smith9eed49c2011-12-09 23:00:37 +0000443static_assert(DiffProd == 8, "");
Richard Smith08d6e032011-12-16 19:06:07 +0000444static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
445 expected-error {{constant expression}} \
446 expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000447
448constexpr const int *p = xs + 3;
449constexpr int xs4 = p[1]; // ok
Richard Smith7098cbd2011-12-21 05:04:46 +0000450constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smithb4e85ed2012-01-06 16:39:00 +0000451constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}}
Richard Smith59efe262011-11-11 04:05:33 +0000452constexpr int xs0 = p[-3]; // ok
Richard Smithb4e85ed2012-01-06 16:39:00 +0000453constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
Richard Smith59efe262011-11-11 04:05:33 +0000454
455constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
Richard Smith9eed49c2011-12-09 23:00:37 +0000456static_assert(zs[0][0][0][0] == 1, "");
457static_assert(zs[1][1][1][1] == 16, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000458static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000459static_assert((&zs[0][0][0][2])[-1] == 2, "");
460static_assert(**(**(zs + 1) + 1) == 11, "");
Richard Smithb4e85ed2012-01-06 16:39:00 +0000461static_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}}
462static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
463constexpr 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}}
Richard Smith59efe262011-11-11 04:05:33 +0000464
Richard Smith08d6e032011-12-16 19:06:07 +0000465constexpr int fail(const int &p) {
Richard Smithb4e85ed2012-01-06 16:39:00 +0000466 return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
Richard Smith08d6e032011-12-16 19:06:07 +0000467}
Richard Smithb4e85ed2012-01-06 16:39:00 +0000468static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \
Richard Smith08d6e032011-12-16 19:06:07 +0000469expected-error {{static_assert expression is not an integral constant expression}} \
470expected-note {{in call to 'fail(zs[1][0][1][0])'}}
471
Richard Smithd7c56e12011-12-29 21:57:33 +0000472constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}}
Richard Smith59efe262011-11-11 04:05:33 +0000473constexpr int SumNonzero(const int *p) {
474 return *p + (*p ? SumNonzero(p+1) : 0);
475}
476constexpr int CountZero(const int *p, const int *q) {
477 return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
478}
Richard Smith9eed49c2011-12-09 23:00:37 +0000479static_assert(SumNonzero(arr) == 6, "");
480static_assert(CountZero(arr, arr + 40) == 36, "");
Richard Smith59efe262011-11-11 04:05:33 +0000481
Richard Smithe24f5fc2011-11-17 22:56:20 +0000482struct ArrayElem {
483 constexpr ArrayElem() : n(0) {}
484 int n;
485 constexpr int f() { return n; }
486};
487struct ArrayRVal {
488 constexpr ArrayRVal() {}
489 ArrayElem elems[10];
490};
Richard Smith9eed49c2011-12-09 23:00:37 +0000491static_assert(ArrayRVal().elems[3].f() == 0, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000492
Richard Smith59efe262011-11-11 04:05:33 +0000493}
494
495namespace DependentValues {
496
497struct I { int n; typedef I V[10]; };
498I::V x, y;
499template<bool B> struct S {
500 int k;
501 void f() {
502 I::V &cells = B ? x : y;
503 I &i = cells[k];
504 switch (i.n) {}
505 }
506};
507
508}
509
510namespace Class {
511
512struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
513constexpr int fn(const A &a) { return a.k; }
Richard Smith9eed49c2011-12-09 23:00:37 +0000514static_assert(fn(A(4,5)) == 9, "");
Richard Smith59efe262011-11-11 04:05:33 +0000515
516struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
517struct C {
518 constexpr C(C *this_) : m(42), n(this_->m) {} // ok
519 int m, n;
520};
521struct D {
522 C c;
523 constexpr D() : c(&c) {}
524};
Richard Smith9eed49c2011-12-09 23:00:37 +0000525static_assert(D().c.n == 42, "");
Richard Smith59efe262011-11-11 04:05:33 +0000526
Richard Smith61802452011-12-22 02:22:31 +0000527struct E {
Richard Smith28c1ce72012-01-15 03:25:41 +0000528 constexpr E() : p(&p) {} // expected-note {{pointer to subobject of temporary cannot be used to initialize a member in a constant expression}}
Richard Smith59efe262011-11-11 04:05:33 +0000529 void *p;
530};
Richard Smith099e7f62011-12-19 06:19:21 +0000531constexpr const E &e1 = E(); // expected-error {{constant expression}} expected-note {{in call to 'E()'}} expected-note {{temporary created here}}
Richard Smith59efe262011-11-11 04:05:33 +0000532// This is a constant expression if we elide the copy constructor call, and
533// is not a constant expression if we don't! But we do, so it is.
Richard Smith61802452011-12-22 02:22:31 +0000534constexpr E e2 = E();
535static_assert(e2.p == &e2.p, "");
Richard Smith099e7f62011-12-19 06:19:21 +0000536constexpr E e3;
Richard Smith9eed49c2011-12-09 23:00:37 +0000537static_assert(e3.p == &e3.p, "");
Richard Smith59efe262011-11-11 04:05:33 +0000538
539extern const class F f;
540struct F {
541 constexpr F() : p(&f.p) {}
542 const void *p;
543};
Richard Smith099e7f62011-12-19 06:19:21 +0000544constexpr F f;
Richard Smith59efe262011-11-11 04:05:33 +0000545
546struct G {
547 struct T {
548 constexpr T(T *p) : u1(), u2(p) {}
549 union U1 {
550 constexpr U1() {}
551 int a, b = 42;
552 } u1;
553 union U2 {
554 constexpr U2(T *p) : c(p->u1.b) {}
555 int c, d;
556 } u2;
557 } t;
558 constexpr G() : t(&t) {}
559} constexpr g;
560
Richard Smith7098cbd2011-12-21 05:04:46 +0000561static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000562static_assert(g.t.u1.b == 42, "");
563static_assert(g.t.u2.c == 42, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000564static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
Richard Smith59efe262011-11-11 04:05:33 +0000565
566struct S {
567 int a, b;
568 const S *p;
569 double d;
570 const char *q;
571
572 constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
573};
574
575S global(43, &global);
576
Richard Smith9eed49c2011-12-09 23:00:37 +0000577static_assert(S(15, &global).b == 15, "");
Richard Smith59efe262011-11-11 04:05:33 +0000578
579constexpr bool CheckS(const S &s) {
580 return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
581}
Richard Smith9eed49c2011-12-09 23:00:37 +0000582static_assert(CheckS(S(27, &global)), "");
Richard Smith59efe262011-11-11 04:05:33 +0000583
584struct Arr {
585 char arr[3];
586 constexpr Arr() : arr{'x', 'y', 'z'} {}
587};
588constexpr int hash(Arr &&a) {
589 return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
590}
591constexpr int k = hash(Arr());
Richard Smith9eed49c2011-12-09 23:00:37 +0000592static_assert(k == 0x007a7978, "");
Richard Smith59efe262011-11-11 04:05:33 +0000593
594
595struct AggregateInit {
596 const char &c;
597 int n;
598 double d;
599 int arr[5];
600 void *p;
601};
602
603constexpr AggregateInit agg1 = { "hello"[0] };
604
Richard Smith9eed49c2011-12-09 23:00:37 +0000605static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
606static_assert(agg1.n == 0, "");
607static_assert(agg1.d == 0.0, "");
Richard Smithb4e85ed2012-01-06 16:39:00 +0000608static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000609static_assert(agg1.arr[0] == 0, "");
610static_assert(agg1.arr[4] == 0, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000611static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000612static_assert(agg1.p == nullptr, "");
Richard Smith59efe262011-11-11 04:05:33 +0000613
614namespace SimpleDerivedClass {
615
616struct B {
617 constexpr B(int n) : a(n) {}
618 int a;
619};
620struct D : B {
621 constexpr D(int n) : B(n) {}
622};
623constexpr D d(3);
Richard Smith9eed49c2011-12-09 23:00:37 +0000624static_assert(d.a == 3, "");
Richard Smith59efe262011-11-11 04:05:33 +0000625
626}
627
Richard Smithe24f5fc2011-11-17 22:56:20 +0000628struct Bottom { constexpr Bottom() {} };
629struct Base : Bottom {
Richard Smith59efe262011-11-11 04:05:33 +0000630 constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
631 int a;
632 const char *b;
633};
Richard Smithe24f5fc2011-11-17 22:56:20 +0000634struct Base2 : Bottom {
Richard Smith59efe262011-11-11 04:05:33 +0000635 constexpr Base2(const int &r) : r(r) {}
636 int q = 123;
Richard Smith099e7f62011-12-19 06:19:21 +0000637 const int &r;
Richard Smith59efe262011-11-11 04:05:33 +0000638};
639struct Derived : Base, Base2 {
640 constexpr Derived() : Base(76), Base2(a) {}
641 int c = r + b[1];
642};
643
644constexpr bool operator==(const Base &a, const Base &b) {
645 return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
646}
647
648constexpr Base base;
649constexpr Base base2(76);
650constexpr Derived derived;
Richard Smith9eed49c2011-12-09 23:00:37 +0000651static_assert(derived.a == 76, "");
652static_assert(derived.b[2] == 's', "");
653static_assert(derived.c == 76 + 'e', "");
654static_assert(derived.q == 123, "");
655static_assert(derived.r == 76, "");
Richard Smith099e7f62011-12-19 06:19:21 +0000656static_assert(&derived.r == &derived.a, "");
Richard Smith59efe262011-11-11 04:05:33 +0000657
Richard Smith9eed49c2011-12-09 23:00:37 +0000658static_assert(!(derived == base), "");
659static_assert(derived == base2, "");
Richard Smith59efe262011-11-11 04:05:33 +0000660
Richard Smithe24f5fc2011-11-17 22:56:20 +0000661constexpr Bottom &bot1 = (Base&)derived;
662constexpr Bottom &bot2 = (Base2&)derived;
Richard Smith9eed49c2011-12-09 23:00:37 +0000663static_assert(&bot1 != &bot2, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000664
665constexpr Bottom *pb1 = (Base*)&derived;
666constexpr Bottom *pb2 = (Base2*)&derived;
Richard Smith9eed49c2011-12-09 23:00:37 +0000667static_assert(pb1 != pb2, "");
668static_assert(pb1 == &bot1, "");
669static_assert(pb2 == &bot2, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000670
Richard Smithb4e85ed2012-01-06 16:39:00 +0000671constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
672constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
Richard Smithe24f5fc2011-11-17 22:56:20 +0000673constexpr Base2 &ok2 = (Base2&)bot2;
Richard Smith9eed49c2011-12-09 23:00:37 +0000674static_assert(&ok2 == &derived, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000675
Richard Smithb4e85ed2012-01-06 16:39:00 +0000676constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
677constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}}
Richard Smithe24f5fc2011-11-17 22:56:20 +0000678constexpr Base2 *pok2 = (Base2*)pb2;
Richard Smith9eed49c2011-12-09 23:00:37 +0000679static_assert(pok2 == &derived, "");
680static_assert(&ok2 == pok2, "");
681static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
682static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000683
684constexpr Base *nullB = 42 - 6 * 7;
Richard Smith9eed49c2011-12-09 23:00:37 +0000685static_assert((Bottom*)nullB == 0, "");
686static_assert((Derived*)nullB == 0, "");
687static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000688
Richard Smith7d580a42012-01-17 21:17:26 +0000689namespace ConversionOperators {
690
691struct T {
692 constexpr T(int n) : k(5*n - 3) {}
693 constexpr operator int() { return k; }
694 int k;
695};
696
697struct S {
698 constexpr S(int n) : k(2*n + 1) {}
699 constexpr operator int() { return k; }
700 constexpr operator T() { return T(k); }
701 int k;
702};
703
704constexpr bool check(T a, T b) { return a == b.k; }
705
706static_assert(S(5) == 11, "");
707static_assert(check(S(5), 11), "");
708
709}
710
Richard Smithe24f5fc2011-11-17 22:56:20 +0000711}
712
713namespace Temporaries {
714
715struct S {
716 constexpr S() {}
717 constexpr int f();
718};
719struct T : S {
720 constexpr T(int n) : S(), n(n) {}
721 int n;
722};
723constexpr int S::f() {
724 // 'this' must be the postfix-expression in a class member access expression,
725 // so we can't just use
726 // return static_cast<T*>(this)->n;
727 return this->*(int(S::*))&T::n;
728}
729// The T temporary is implicitly cast to an S subobject, but we can recover the
730// T full-object via a base-to-derived cast, or a derived-to-base-casted member
731// pointer.
Richard Smith9eed49c2011-12-09 23:00:37 +0000732static_assert(T(3).f() == 3, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000733
734constexpr int f(const S &s) {
735 return static_cast<const T&>(s).n;
736}
737constexpr int n = f(T(5));
Richard Smith9eed49c2011-12-09 23:00:37 +0000738static_assert(f(T(5)) == 5, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000739
Richard Smith59efe262011-11-11 04:05:33 +0000740}
741
742namespace Union {
743
744union U {
745 int a;
746 int b;
747};
748
Richard Smithd7c56e12011-12-29 21:57:33 +0000749constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{C99 feature}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000750static_assert(u[0].a == 0, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000751static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000752static_assert(u[1].b == 1, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000753static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smithb4e85ed2012-01-06 16:39:00 +0000754static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000755static_assert((&(u[1]) + 1 + 1)->b == 3, "");
Richard Smith59efe262011-11-11 04:05:33 +0000756
Richard Smithec789162012-01-12 18:54:33 +0000757constexpr U v = {};
758static_assert(v.a == 0, "");
759
760union Empty {};
761constexpr Empty e = {};
762
Richard Smith610a60c2012-01-10 04:32:03 +0000763// Make sure we handle trivial copy constructors for unions.
764constexpr U x = {42};
765constexpr U y = x;
766static_assert(y.a == 42, "");
767static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}}
768
Richard Smith59efe262011-11-11 04:05:33 +0000769}
770
Richard Smithe24f5fc2011-11-17 22:56:20 +0000771namespace MemberPointer {
772 struct A {
773 constexpr A(int n) : n(n) {}
774 int n;
775 constexpr int f() { return n + 3; }
776 };
777 constexpr A a(7);
Richard Smith9eed49c2011-12-09 23:00:37 +0000778 static_assert(A(5).*&A::n == 5, "");
779 static_assert((&a)->*&A::n == 7, "");
780 static_assert((A(8).*&A::f)() == 11, "");
781 static_assert(((&a)->*&A::f)() == 10, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000782
783 struct B : A {
784 constexpr B(int n, int m) : A(n), m(m) {}
785 int m;
786 constexpr int g() { return n + m + 1; }
787 };
788 constexpr B b(9, 13);
Richard Smith9eed49c2011-12-09 23:00:37 +0000789 static_assert(B(4, 11).*&A::n == 4, "");
790 static_assert(B(4, 11).*&B::m == 11, "");
791 static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
792 static_assert((&b)->*&A::n == 9, "");
793 static_assert((&b)->*&B::m == 13, "");
794 static_assert((&b)->*(int(A::*))&B::m == 13, "");
795 static_assert((B(4, 11).*&A::f)() == 7, "");
796 static_assert((B(4, 11).*&B::g)() == 16, "");
797 static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
798 static_assert(((&b)->*&A::f)() == 12, "");
799 static_assert(((&b)->*&B::g)() == 23, "");
800 static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000801
802 struct S {
803 constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
804 m(m), n(n), pf(pf), pn(pn) {}
805 constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
806
807 constexpr int f() { return this->*pn; }
808 virtual int g() const;
809
810 int m, n;
811 int (S::*pf)() const;
812 int S::*pn;
813 };
814
815 constexpr int S::*pm = &S::m;
816 constexpr int S::*pn = &S::n;
817 constexpr int (S::*pf)() const = &S::f;
818 constexpr int (S::*pg)() const = &S::g;
819
820 constexpr S s(2, 5, &S::f, &S::m);
821
Richard Smith9eed49c2011-12-09 23:00:37 +0000822 static_assert((s.*&S::f)() == 2, "");
823 static_assert((s.*s.pf)() == 2, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000824
825 template<int n> struct T : T<n-1> {};
826 template<> struct T<0> { int n; };
827 template<> struct T<30> : T<29> { int m; };
828
829 T<17> t17;
830 T<30> t30;
831
832 constexpr int (T<10>::*deepn) = &T<0>::n;
Richard Smith9eed49c2011-12-09 23:00:37 +0000833 static_assert(&(t17.*deepn) == &t17.n, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000834
835 constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
836 constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000837 static_assert(&(t30.*deepm) == &t30.m, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000838
839 constexpr T<5> *p17_5 = &t17;
840 constexpr T<13> *p17_13 = (T<13>*)p17_5;
Richard Smithb4e85ed2012-01-06 16:39:00 +0000841 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>'}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000842 static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
843 static_assert(&(p17_13->*deepn) == &t17.n, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000844 constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
845
846 constexpr T<5> *p30_5 = &t30;
847 constexpr T<23> *p30_23 = (T<23>*)p30_5;
848 constexpr T<13> *p30_13 = p30_23;
Richard Smith9eed49c2011-12-09 23:00:37 +0000849 static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
850 static_assert(&(p30_13->*deepn) == &t30.n, "");
851 static_assert(&(p30_23->*deepn) == &t30.n, "");
852 static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
853 static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
854 static_assert(&(p30_23->*deepm) == &t30.m, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000855}
856
857namespace ArrayBaseDerived {
858
859 struct Base {
860 constexpr Base() {}
861 int n = 0;
862 };
863 struct Derived : Base {
864 constexpr Derived() {}
865 constexpr const int *f() { return &n; }
866 };
867
868 constexpr Derived a[10];
869 constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
870 constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
Richard Smith9eed49c2011-12-09 23:00:37 +0000871 static_assert(pb3 == pd3, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000872
873 // pb3 does not point to an array element.
874 constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
Richard Smithb4e85ed2012-01-06 16:39:00 +0000875 constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}}
876 constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}}
877 constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}}
878 constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}}
879 constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}}
Richard Smithe24f5fc2011-11-17 22:56:20 +0000880 constexpr Base *pb3a = pb4 - 1;
881
882 // pb4 does not point to a Derived.
Richard Smithb4e85ed2012-01-06 16:39:00 +0000883 constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}}
Richard Smithe24f5fc2011-11-17 22:56:20 +0000884 constexpr Derived *pd3a = (Derived*)pb3a;
885 constexpr int pd3n = pd3a->n;
886
887 // pd3a still points to the Derived array.
888 constexpr Derived *pd6 = pd3a + 3;
Richard Smith9eed49c2011-12-09 23:00:37 +0000889 static_assert(pd6 == &a[6], "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000890 constexpr Derived *pd9 = pd6 + 3;
891 constexpr Derived *pd10 = pd6 + 4;
892 constexpr int pd9n = pd9->n; // ok
Richard Smithb4e85ed2012-01-06 16:39:00 +0000893 constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}}
Richard Smithe24f5fc2011-11-17 22:56:20 +0000894 constexpr int pd0n = pd10[-10].n;
Richard Smithb4e85ed2012-01-06 16:39:00 +0000895 constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}}
Richard Smithe24f5fc2011-11-17 22:56:20 +0000896
897 constexpr Base *pb9 = pd9;
898 constexpr const int *(Base::*pfb)() const =
899 static_cast<const int *(Base::*)() const>(&Derived::f);
Richard Smith9eed49c2011-12-09 23:00:37 +0000900 static_assert((pb9->*pfb)() == &a[9].n, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000901}
902
Richard Smith59efe262011-11-11 04:05:33 +0000903namespace Complex {
904
905class complex {
906 int re, im;
907public:
908 constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
909 constexpr complex(const complex &o) : re(o.re), im(o.im) {}
910 constexpr complex operator-() const { return complex(-re, -im); }
911 friend constexpr complex operator+(const complex &l, const complex &r) {
912 return complex(l.re + r.re, l.im + r.im);
913 }
914 friend constexpr complex operator-(const complex &l, const complex &r) {
915 return l + -r;
916 }
917 friend constexpr complex operator*(const complex &l, const complex &r) {
918 return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
919 }
920 friend constexpr bool operator==(const complex &l, const complex &r) {
921 return l.re == r.re && l.im == r.im;
922 }
923 constexpr bool operator!=(const complex &r) const {
924 return re != r.re || im != r.im;
925 }
926 constexpr int real() const { return re; }
927 constexpr int imag() const { return im; }
928};
929
930constexpr complex i = complex(0, 1);
931constexpr complex k = (3 + 4*i) * (6 - 4*i);
Richard Smith9eed49c2011-12-09 23:00:37 +0000932static_assert(complex(1,0).real() == 1, "");
933static_assert(complex(1,0).imag() == 0, "");
934static_assert(((complex)1).imag() == 0, "");
935static_assert(k.real() == 34, "");
936static_assert(k.imag() == 12, "");
937static_assert(k - 34 == 12*i, "");
938static_assert((complex)1 == complex(1), "");
939static_assert((complex)1 != complex(0, 1), "");
940static_assert(complex(1) == complex(1), "");
941static_assert(complex(1) != complex(0, 1), "");
Richard Smith59efe262011-11-11 04:05:33 +0000942constexpr complex makeComplex(int re, int im) { return complex(re, im); }
Richard Smith9eed49c2011-12-09 23:00:37 +0000943static_assert(makeComplex(1,0) == complex(1), "");
944static_assert(makeComplex(1,0) != complex(0, 1), "");
Richard Smith59efe262011-11-11 04:05:33 +0000945
946class complex_wrap : public complex {
947public:
948 constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
949 constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
950};
951
Richard Smith9eed49c2011-12-09 23:00:37 +0000952static_assert((complex_wrap)1 == complex(1), "");
953static_assert((complex)1 != complex_wrap(0, 1), "");
954static_assert(complex(1) == complex_wrap(1), "");
955static_assert(complex_wrap(1) != complex(0, 1), "");
Richard Smith59efe262011-11-11 04:05:33 +0000956constexpr complex_wrap makeComplexWrap(int re, int im) {
957 return complex_wrap(re, im);
958}
Richard Smith9eed49c2011-12-09 23:00:37 +0000959static_assert(makeComplexWrap(1,0) == complex(1), "");
960static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
Richard Smith59efe262011-11-11 04:05:33 +0000961
962}
Eli Friedmanf59ff8c2011-12-17 02:24:21 +0000963
964namespace PR11595 {
965 struct A { constexpr bool operator==(int x) { return true; } };
Richard Smithaf2c7a12011-12-19 22:01:37 +0000966 struct B { B(); A& x; };
967 static_assert(B().x == 3, ""); // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
968
969 constexpr bool f(int k) {
970 return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
971 }
972 constexpr int n = f(1); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'f(1)'}}
Eli Friedmanf59ff8c2011-12-17 02:24:21 +0000973}
Richard Smithbc6abe92011-12-19 22:12:41 +0000974
975namespace ExprWithCleanups {
976 struct A { A(); ~A(); int get(); };
977 constexpr int get(bool FromA) { return FromA ? A().get() : 1; }
978 constexpr int n = get(false);
979}
Richard Smith7098cbd2011-12-21 05:04:46 +0000980
981namespace Volatile {
982
983volatile constexpr int n1 = 0; // expected-note {{here}}
984volatile const int n2 = 0; // expected-note {{here}}
985int n3 = 37; // expected-note {{declared here}}
986
987constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
988constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
989
990struct T { int n; };
991const T t = { 42 }; // expected-note {{declared here}}
992
993constexpr int f(volatile int &&r) {
994 return r; // expected-note {{read of volatile temporary is not allowed in a constant expression}}
995}
996struct S {
997 int k : f(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'f(0)'}}
998 int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
999 int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}}
1000};
1001
1002}
Richard Smithdd4b3502011-12-25 21:17:58 +00001003
1004namespace ExternConstexpr {
1005 extern constexpr int n = 0;
1006 extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}}
1007 void f() {
1008 extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}}
1009 constexpr int j = 0;
1010 constexpr int k; // expected-error {{default initialization of an object of const type}}
1011 }
1012}
Eli Friedman7ead5c72012-01-10 04:58:17 +00001013
1014namespace ComplexConstexpr {
1015 constexpr _Complex float test1 = {};
1016 constexpr _Complex float test2 = {1};
1017 constexpr _Complex double test3 = {1,2};
1018 constexpr _Complex int test4 = {4};
1019 constexpr _Complex int test5 = 4;
1020 constexpr _Complex int test6 = {5,6};
Eli Friedmanf6c17a42012-01-13 23:34:56 +00001021 typedef _Complex float fcomplex;
1022 constexpr fcomplex test7 = fcomplex();
Eli Friedman7ead5c72012-01-10 04:58:17 +00001023}
Eli Friedman6b3014b2012-01-18 02:54:10 +00001024
1025namespace InstantiateCaseStmt {
1026 template<int x> constexpr int f() { return x; }
1027 template<int x> int g(int c) { switch(c) { case f<x>(): return 1; } return 0; }
1028 int gg(int c) { return g<4>(c); }
1029}