blob: 71ce75887ba7390b2bd5737cd90f50c1edb1e5db [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) {
95 // FIXME: Produce the 'add ()' fixit for this.
Richard Smithc1c5f272011-12-13 06:39:58 +000096 case MemberZero().zero: // desired-error {{did you mean to call it with no arguments?}} expected-error {{not an integer constant expression}} expected-note {{non-literal type '<bound member function type>'}}
Richard Smith1d238ea2011-12-21 02:55:12 +000097 case id(1):
Richard Smith59efe262011-11-11 04:05:33 +000098 return;
99 }
100 }
101}
102
103extern int &Recurse1;
Richard Smith099e7f62011-12-19 06:19:21 +0000104int &Recurse2 = Recurse1; // expected-note 2{{declared here}} expected-note {{initializer of 'Recurse1' is not a constant expression}}
105int &Recurse1 = Recurse2; // expected-note {{declared here}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
106constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
107
108extern const int RecurseA;
109const int RecurseB = RecurseA; // expected-note {{declared here}}
110const int RecurseA = 10;
111constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
Richard Smith59efe262011-11-11 04:05:33 +0000112
113namespace MemberEnum {
114 struct WithMemberEnum {
115 enum E { A = 42 };
116 } wme;
117
Richard Smith9eed49c2011-12-09 23:00:37 +0000118 static_assert(wme.A == 42, "");
Richard Smith59efe262011-11-11 04:05:33 +0000119}
120
121namespace DefaultArguments {
122
123const int z = int();
124constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
125 return a + b + *c + d;
126}
127const int four = 4;
128constexpr int eight = 8;
129constexpr const int twentyseven = 27;
Richard Smith9eed49c2011-12-09 23:00:37 +0000130static_assert(Sum() == 0, "");
131static_assert(Sum(1) == 1, "");
132static_assert(Sum(1, four) == 5, "");
133static_assert(Sum(1, eight, &twentyseven) == 36, "");
134static_assert(Sum(1, 2, &four, eight) == 15, "");
Richard Smith59efe262011-11-11 04:05:33 +0000135
136}
137
138namespace Ellipsis {
139
140// Note, values passed through an ellipsis can't actually be used.
141constexpr int F(int a, ...) { return a; }
Richard Smith9eed49c2011-12-09 23:00:37 +0000142static_assert(F(0) == 0, "");
143static_assert(F(1, 0) == 1, "");
144static_assert(F(2, "test") == 2, "");
145static_assert(F(3, &F) == 3, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000146int k = 0; // expected-note {{here}}
147static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
Richard Smith59efe262011-11-11 04:05:33 +0000148
149}
150
151namespace Recursion {
152 constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
Richard Smith9eed49c2011-12-09 23:00:37 +0000153 static_assert(fib(11) == 89, "");
Richard Smith59efe262011-11-11 04:05:33 +0000154
155 constexpr int gcd_inner(int a, int b) {
156 return b == 0 ? a : gcd_inner(b, a % b);
157 }
158 constexpr int gcd(int a, int b) {
159 return gcd_inner(max(a, b), min(a, b));
160 }
161
Richard Smith9eed49c2011-12-09 23:00:37 +0000162 static_assert(gcd(1749237, 5628959) == 7, "");
Richard Smith59efe262011-11-11 04:05:33 +0000163}
164
165namespace FunctionCast {
166 // When folding, we allow functions to be cast to different types. Such
167 // cast functions cannot be called, even if they're constexpr.
168 constexpr int f() { return 1; }
169 typedef double (*DoubleFn)();
170 typedef int (*IntFn)();
Richard Smithd7c56e12011-12-29 21:57:33 +0000171 int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}}
Richard Smith59efe262011-11-11 04:05:33 +0000172 int b[(int)IntFn(f)()]; // ok
173}
174
175namespace StaticMemberFunction {
176 struct S {
177 static constexpr int k = 42;
178 static constexpr int f(int n) { return n * k + 2; }
179 } s;
180
181 constexpr int n = s.f(19);
Richard Smith9eed49c2011-12-09 23:00:37 +0000182 static_assert(S::f(19) == 800, "");
183 static_assert(s.f(19) == 800, "");
184 static_assert(n == 800, "");
Richard Smith1bf9a9e2011-11-12 22:28:03 +0000185
186 constexpr int (*sf1)(int) = &S::f;
187 constexpr int (*sf2)(int) = &s.f;
188 constexpr const int *sk = &s.k;
Richard Smith59efe262011-11-11 04:05:33 +0000189}
190
191namespace ParameterScopes {
192
193 const int k = 42;
Richard Smith099e7f62011-12-19 06:19:21 +0000194 constexpr const int &ObscureTheTruth(const int &a) { return a; } // expected-note 3{{reference to 'a' cannot be returned from a constexpr function}}
195 constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}}
196 return ObscureTheTruth(b ? a : k); // expected-note 2{{in call to 'ObscureTheTruth(a)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000197 }
Richard Smith9eed49c2011-12-09 23:00:37 +0000198 static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
Richard Smith099e7f62011-12-19 06:19:21 +0000199 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 +0000200
Richard Smith099e7f62011-12-19 06:19:21 +0000201 constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { // expected-note {{here}}
Richard Smith59efe262011-11-11 04:05:33 +0000202 // If ObscureTheTruth returns a reference to 'a', the result is not a
203 // constant expression even though 'a' is still in scope.
Richard Smith099e7f62011-12-19 06:19:21 +0000204 return ObscureTheTruth(b ? a : k); // expected-note {{in call to 'ObscureTheTruth(a)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000205 }
Richard Smith9eed49c2011-12-09 23:00:37 +0000206 static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
Richard Smith099e7f62011-12-19 06:19:21 +0000207 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 +0000208
209 constexpr int InternalReturnJunk(int n) {
210 // FIXME: We should reject this: it never produces a constant expression.
Richard Smith099e7f62011-12-19 06:19:21 +0000211 return MaybeReturnJunk(true, n); // expected-note {{in call to 'MaybeReturnJunk(1, 0)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000212 }
Richard Smith099e7f62011-12-19 06:19:21 +0000213 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 +0000214
215 constexpr int LToR(int &n) { return n; }
216 constexpr int GrabCallersArgument(bool which, int a, int b) {
217 return LToR(which ? b : a);
218 }
Richard Smith9eed49c2011-12-09 23:00:37 +0000219 static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
220 static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
Richard Smith59efe262011-11-11 04:05:33 +0000221
222}
223
224namespace Pointers {
225
226 constexpr int f(int n, const int *a, const int *b, const int *c) {
227 return n == 0 ? 0 : *a + f(n-1, b, c, a);
228 }
229
230 const int x = 1, y = 10, z = 100;
Richard Smith9eed49c2011-12-09 23:00:37 +0000231 static_assert(f(23, &x, &y, &z) == 788, "");
Richard Smith59efe262011-11-11 04:05:33 +0000232
233 constexpr int g(int n, int a, int b, int c) {
234 return f(n, &a, &b, &c);
235 }
Richard Smith9eed49c2011-12-09 23:00:37 +0000236 static_assert(g(23, x, y, z) == 788, "");
Richard Smith59efe262011-11-11 04:05:33 +0000237
238}
239
240namespace FunctionPointers {
241
242 constexpr int Double(int n) { return 2 * n; }
243 constexpr int Triple(int n) { return 3 * n; }
244 constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
245 constexpr int Quadruple(int n) { return Twice(Double, n); }
246 constexpr auto Select(int n) -> int (*)(int) {
247 return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
248 }
Richard Smith099e7f62011-12-19 06:19:21 +0000249 constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}}
Richard Smith59efe262011-11-11 04:05:33 +0000250
Richard Smith9eed49c2011-12-09 23:00:37 +0000251 static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
Richard Smith59efe262011-11-11 04:05:33 +0000252
Richard Smith099e7f62011-12-19 06:19:21 +0000253 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 +0000254
255}
256
257namespace PointerComparison {
258
259int x, y;
Richard Smith9eed49c2011-12-09 23:00:37 +0000260static_assert(&x == &y, "false"); // expected-error {{false}}
261static_assert(&x != &y, "");
Richard Smith59efe262011-11-11 04:05:33 +0000262constexpr bool g1 = &x == &y;
263constexpr bool g2 = &x != &y;
264constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
265constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
266constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
267constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
268
269struct S { int x, y; } s;
Richard Smith9eed49c2011-12-09 23:00:37 +0000270static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
271static_assert(&s.x != &s.y, "");
272static_assert(&s.x <= &s.y, "");
273static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
274static_assert(&s.x < &s.y, "");
275static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
Richard Smith59efe262011-11-11 04:05:33 +0000276
Richard Smith9eed49c2011-12-09 23:00:37 +0000277static_assert(0 == &y, "false"); // expected-error {{false}}
278static_assert(0 != &y, "");
Richard Smith59efe262011-11-11 04:05:33 +0000279constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
280constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
281constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
282constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
283
Richard Smith9eed49c2011-12-09 23:00:37 +0000284static_assert(&x == 0, "false"); // expected-error {{false}}
285static_assert(&x != 0, "");
Richard Smith59efe262011-11-11 04:05:33 +0000286constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
287constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
288constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
289constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
290
Richard Smith9eed49c2011-12-09 23:00:37 +0000291static_assert(&x == &x, "");
292static_assert(&x != &x, "false"); // expected-error {{false}}
293static_assert(&x <= &x, "");
294static_assert(&x >= &x, "");
295static_assert(&x < &x, "false"); // expected-error {{false}}
296static_assert(&x > &x, "false"); // expected-error {{false}}
Richard Smith59efe262011-11-11 04:05:33 +0000297
298constexpr S* sptr = &s;
Richard Smith099e7f62011-12-19 06:19:21 +0000299constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
Richard Smith59efe262011-11-11 04:05:33 +0000300
Richard Smithc216a012011-12-12 12:46:16 +0000301struct Str {
302 // FIXME: In C++ mode, we should say 'integral' not 'integer'
303 int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
304 expected-warning {{not integer constant expression}} \
Richard Smith4cd9b8f2011-12-12 19:10:03 +0000305 expected-note {{dynamic_cast is not allowed in a constant expression}}
Richard Smithc216a012011-12-12 12:46:16 +0000306 int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
307 expected-warning {{not integer constant expression}} \
Richard Smith4cd9b8f2011-12-12 19:10:03 +0000308 expected-note {{reinterpret_cast is not allowed in a constant expression}}
Richard Smithc216a012011-12-12 12:46:16 +0000309 int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
310 expected-warning {{not integer constant expression}} \
Richard Smith60f24e72011-12-12 19:33:27 +0000311 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 +0000312 int d : (S*)(42) == (S*)(42); // \
313 expected-warning {{not integer constant expression}} \
Richard Smith60f24e72011-12-12 19:33:27 +0000314 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 +0000315 int e : (Str*)(sptr) == (Str*)(sptr); // \
316 expected-warning {{not integer constant expression}} \
Richard Smith60f24e72011-12-12 19:33:27 +0000317 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 +0000318 int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \
319 expected-warning {{not integer constant expression}} \
Richard Smith60f24e72011-12-12 19:33:27 +0000320 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 +0000321 int g : (S*)(void*)(sptr) == sptr; // \
322 expected-warning {{not integer constant expression}} \
Richard Smith4cd9b8f2011-12-12 19:10:03 +0000323 expected-note {{cast from 'void *' is not allowed in a constant expression}}
Richard Smithc216a012011-12-12 12:46:16 +0000324};
325
Richard Smith59efe262011-11-11 04:05:33 +0000326extern char externalvar[];
Richard Smith9eed49c2011-12-09 23:00:37 +0000327constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
Richard Smith59efe262011-11-11 04:05:33 +0000328constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000329static_assert(0 != "foo", "");
Richard Smith59efe262011-11-11 04:05:33 +0000330
331}
332
333namespace MaterializeTemporary {
334
335constexpr int f(const int &r) { return r; }
336constexpr int n = f(1);
337
338constexpr bool same(const int &a, const int &b) { return &a == &b; }
339constexpr bool sameTemporary(const int &n) { return same(n, n); }
340
Richard Smith9eed49c2011-12-09 23:00:37 +0000341static_assert(n, "");
342static_assert(!same(4, 4), "");
343static_assert(same(n, n), "");
344static_assert(sameTemporary(9), "");
Richard Smith59efe262011-11-11 04:05:33 +0000345
346}
347
348constexpr int strcmp_ce(const char *p, const char *q) {
349 return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
350}
351
352namespace StringLiteral {
353
Richard Smith1d238ea2011-12-21 02:55:12 +0000354template<typename Char>
355constexpr int MangleChars(const Char *p) {
Richard Smith59efe262011-11-11 04:05:33 +0000356 return *p + 3 * (*p ? MangleChars(p+1) : 0);
357}
358
Richard Smith9eed49c2011-12-09 23:00:37 +0000359static_assert(MangleChars("constexpr!") == 1768383, "");
Richard Smithec789162012-01-12 18:54:33 +0000360static_assert(MangleChars(u8"constexpr!") == 1768383, "");
361static_assert(MangleChars(L"constexpr!") == 1768383, "");
Richard Smith9eed49c2011-12-09 23:00:37 +0000362static_assert(MangleChars(u"constexpr!") == 1768383, "");
363static_assert(MangleChars(U"constexpr!") == 1768383, "");
Richard Smith59efe262011-11-11 04:05:33 +0000364
365constexpr char c0 = "nought index"[0];
366constexpr char c1 = "nice index"[10];
Richard Smith7098cbd2011-12-21 05:04:46 +0000367constexpr 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 +0000368constexpr 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}}
369constexpr 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 +0000370
371constexpr const char *p = "test" + 2;
Richard Smith9eed49c2011-12-09 23:00:37 +0000372static_assert(*p == 's', "");
Richard Smith59efe262011-11-11 04:05:33 +0000373
374constexpr const char *max_iter(const char *a, const char *b) {
375 return *a < *b ? b : a;
376}
377constexpr const char *max_element(const char *a, const char *b) {
378 return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
379}
380
Richard Smith59efe262011-11-11 04:05:33 +0000381constexpr char str[] = "the quick brown fox jumped over the lazy dog";
382constexpr const char *max = max_element(begin(str), end(str));
Richard Smith9eed49c2011-12-09 23:00:37 +0000383static_assert(*max == 'z', "");
384static_assert(max == str + 38, "");
Richard Smith59efe262011-11-11 04:05:33 +0000385
Richard Smith9eed49c2011-12-09 23:00:37 +0000386static_assert(strcmp_ce("hello world", "hello world") == 0, "");
387static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
388static_assert(strcmp_ce("constexpr", "test") < 0, "");
389static_assert(strcmp_ce("", " ") < 0, "");
Richard Smith59efe262011-11-11 04:05:33 +0000390
Richard Smith7098cbd2011-12-21 05:04:46 +0000391struct S {
392 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}}
393};
394
Richard Smith974c5f92011-12-22 01:07:19 +0000395struct T {
396 char c[6];
397 constexpr T() : c{"foo"} {}
398};
399constexpr T t;
400
401static_assert(t.c[0] == 'f', "");
402static_assert(t.c[1] == 'o', "");
403static_assert(t.c[2] == 'o', "");
404static_assert(t.c[3] == 0, "");
405static_assert(t.c[4] == 0, "");
406static_assert(t.c[5] == 0, "");
407static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
408
Richard Smithec789162012-01-12 18:54:33 +0000409struct U {
410 wchar_t chars[6];
411 int n;
412} constexpr u = { { L"test" }, 0 };
413static_assert(u.chars[2] == L's', "");
414
Richard Smith59efe262011-11-11 04:05:33 +0000415}
416
417namespace Array {
418
Richard Smith1d238ea2011-12-21 02:55:12 +0000419template<typename Iter>
420constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) {
Richard Smith59efe262011-11-11 04:05:33 +0000421 return begin == end ? 0 : *begin + Sum(begin+1, end);
422}
Richard Smith59efe262011-11-11 04:05:33 +0000423
424constexpr int xs[] = { 1, 2, 3, 4, 5 };
425constexpr int ys[] = { 5, 4, 3, 2, 1 };
426constexpr int sum_xs = Sum(begin(xs), end(xs));
Richard Smith9eed49c2011-12-09 23:00:37 +0000427static_assert(sum_xs == 15, "");
Richard Smith59efe262011-11-11 04:05:33 +0000428
429constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
430 const int *xs, const int *ys, int c) {
Richard Smithdaaefc52011-12-14 23:32:26 +0000431 return n ? F(
Richard Smith7098cbd2011-12-21 05:04:46 +0000432 *xs, // expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smith08d6e032011-12-16 19:06:07 +0000433 *ys,
434 ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
435 expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
436 expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
437 : c;
Richard Smith59efe262011-11-11 04:05:33 +0000438}
439constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
440constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
Richard Smith9eed49c2011-12-09 23:00:37 +0000441static_assert(InnerProduct == 35, "");
Richard Smith59efe262011-11-11 04:05:33 +0000442
443constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
444constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
Richard Smith9eed49c2011-12-09 23:00:37 +0000445static_assert(DiffProd == 8, "");
Richard Smith08d6e032011-12-16 19:06:07 +0000446static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
447 expected-error {{constant expression}} \
448 expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
Richard Smith59efe262011-11-11 04:05:33 +0000449
450constexpr const int *p = xs + 3;
451constexpr int xs4 = p[1]; // ok
Richard Smith7098cbd2011-12-21 05:04:46 +0000452constexpr 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 +0000453constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}}
Richard Smith59efe262011-11-11 04:05:33 +0000454constexpr int xs0 = p[-3]; // ok
Richard Smithb4e85ed2012-01-06 16:39:00 +0000455constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
Richard Smith59efe262011-11-11 04:05:33 +0000456
457constexpr 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 +0000458static_assert(zs[0][0][0][0] == 1, "");
459static_assert(zs[1][1][1][1] == 16, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000460static_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 +0000461static_assert((&zs[0][0][0][2])[-1] == 2, "");
462static_assert(**(**(zs + 1) + 1) == 11, "");
Richard Smithb4e85ed2012-01-06 16:39:00 +0000463static_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}}
464static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
465constexpr 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 +0000466
Richard Smith08d6e032011-12-16 19:06:07 +0000467constexpr int fail(const int &p) {
Richard Smithb4e85ed2012-01-06 16:39:00 +0000468 return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
Richard Smith08d6e032011-12-16 19:06:07 +0000469}
Richard Smithb4e85ed2012-01-06 16:39:00 +0000470static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \
Richard Smith08d6e032011-12-16 19:06:07 +0000471expected-error {{static_assert expression is not an integral constant expression}} \
472expected-note {{in call to 'fail(zs[1][0][1][0])'}}
473
Richard Smithd7c56e12011-12-29 21:57:33 +0000474constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}}
Richard Smith59efe262011-11-11 04:05:33 +0000475constexpr int SumNonzero(const int *p) {
476 return *p + (*p ? SumNonzero(p+1) : 0);
477}
478constexpr int CountZero(const int *p, const int *q) {
479 return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
480}
Richard Smith9eed49c2011-12-09 23:00:37 +0000481static_assert(SumNonzero(arr) == 6, "");
482static_assert(CountZero(arr, arr + 40) == 36, "");
Richard Smith59efe262011-11-11 04:05:33 +0000483
Richard Smithe24f5fc2011-11-17 22:56:20 +0000484struct ArrayElem {
485 constexpr ArrayElem() : n(0) {}
486 int n;
487 constexpr int f() { return n; }
488};
489struct ArrayRVal {
490 constexpr ArrayRVal() {}
491 ArrayElem elems[10];
492};
Richard Smith9eed49c2011-12-09 23:00:37 +0000493static_assert(ArrayRVal().elems[3].f() == 0, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000494
Richard Smith59efe262011-11-11 04:05:33 +0000495}
496
497namespace DependentValues {
498
499struct I { int n; typedef I V[10]; };
500I::V x, y;
501template<bool B> struct S {
502 int k;
503 void f() {
504 I::V &cells = B ? x : y;
505 I &i = cells[k];
506 switch (i.n) {}
507 }
508};
509
510}
511
512namespace Class {
513
514struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
515constexpr int fn(const A &a) { return a.k; }
Richard Smith9eed49c2011-12-09 23:00:37 +0000516static_assert(fn(A(4,5)) == 9, "");
Richard Smith59efe262011-11-11 04:05:33 +0000517
518struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
519struct C {
520 constexpr C(C *this_) : m(42), n(this_->m) {} // ok
521 int m, n;
522};
523struct D {
524 C c;
525 constexpr D() : c(&c) {}
526};
Richard Smith9eed49c2011-12-09 23:00:37 +0000527static_assert(D().c.n == 42, "");
Richard Smith59efe262011-11-11 04:05:33 +0000528
Richard Smith61802452011-12-22 02:22:31 +0000529struct E {
Richard Smith099e7f62011-12-19 06:19:21 +0000530 constexpr E() : p(&p) {} // expected-note {{pointer to temporary cannot be used to initialize a member in a constant expression}}
Richard Smith59efe262011-11-11 04:05:33 +0000531 void *p;
532};
Richard Smith099e7f62011-12-19 06:19:21 +0000533constexpr 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 +0000534// This is a constant expression if we elide the copy constructor call, and
535// is not a constant expression if we don't! But we do, so it is.
Richard Smith61802452011-12-22 02:22:31 +0000536constexpr E e2 = E();
537static_assert(e2.p == &e2.p, "");
Richard Smith099e7f62011-12-19 06:19:21 +0000538constexpr E e3;
Richard Smith9eed49c2011-12-09 23:00:37 +0000539static_assert(e3.p == &e3.p, "");
Richard Smith59efe262011-11-11 04:05:33 +0000540
541extern const class F f;
542struct F {
543 constexpr F() : p(&f.p) {}
544 const void *p;
545};
Richard Smith099e7f62011-12-19 06:19:21 +0000546constexpr F f;
Richard Smith59efe262011-11-11 04:05:33 +0000547
548struct G {
549 struct T {
550 constexpr T(T *p) : u1(), u2(p) {}
551 union U1 {
552 constexpr U1() {}
553 int a, b = 42;
554 } u1;
555 union U2 {
556 constexpr U2(T *p) : c(p->u1.b) {}
557 int c, d;
558 } u2;
559 } t;
560 constexpr G() : t(&t) {}
561} constexpr g;
562
Richard Smith7098cbd2011-12-21 05:04:46 +0000563static_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 +0000564static_assert(g.t.u1.b == 42, "");
565static_assert(g.t.u2.c == 42, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000566static_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 +0000567
568struct S {
569 int a, b;
570 const S *p;
571 double d;
572 const char *q;
573
574 constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
575};
576
577S global(43, &global);
578
Richard Smith9eed49c2011-12-09 23:00:37 +0000579static_assert(S(15, &global).b == 15, "");
Richard Smith59efe262011-11-11 04:05:33 +0000580
581constexpr bool CheckS(const S &s) {
582 return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
583}
Richard Smith9eed49c2011-12-09 23:00:37 +0000584static_assert(CheckS(S(27, &global)), "");
Richard Smith59efe262011-11-11 04:05:33 +0000585
586struct Arr {
587 char arr[3];
588 constexpr Arr() : arr{'x', 'y', 'z'} {}
589};
590constexpr int hash(Arr &&a) {
591 return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
592}
593constexpr int k = hash(Arr());
Richard Smith9eed49c2011-12-09 23:00:37 +0000594static_assert(k == 0x007a7978, "");
Richard Smith59efe262011-11-11 04:05:33 +0000595
596
597struct AggregateInit {
598 const char &c;
599 int n;
600 double d;
601 int arr[5];
602 void *p;
603};
604
605constexpr AggregateInit agg1 = { "hello"[0] };
606
Richard Smith9eed49c2011-12-09 23:00:37 +0000607static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
608static_assert(agg1.n == 0, "");
609static_assert(agg1.d == 0.0, "");
Richard Smithb4e85ed2012-01-06 16:39:00 +0000610static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000611static_assert(agg1.arr[0] == 0, "");
612static_assert(agg1.arr[4] == 0, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000613static_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 +0000614static_assert(agg1.p == nullptr, "");
Richard Smith59efe262011-11-11 04:05:33 +0000615
616namespace SimpleDerivedClass {
617
618struct B {
619 constexpr B(int n) : a(n) {}
620 int a;
621};
622struct D : B {
623 constexpr D(int n) : B(n) {}
624};
625constexpr D d(3);
Richard Smith9eed49c2011-12-09 23:00:37 +0000626static_assert(d.a == 3, "");
Richard Smith59efe262011-11-11 04:05:33 +0000627
628}
629
Richard Smithe24f5fc2011-11-17 22:56:20 +0000630struct Bottom { constexpr Bottom() {} };
631struct Base : Bottom {
Richard Smith59efe262011-11-11 04:05:33 +0000632 constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
633 int a;
634 const char *b;
635};
Richard Smithe24f5fc2011-11-17 22:56:20 +0000636struct Base2 : Bottom {
Richard Smith59efe262011-11-11 04:05:33 +0000637 constexpr Base2(const int &r) : r(r) {}
638 int q = 123;
Richard Smith099e7f62011-12-19 06:19:21 +0000639 const int &r;
Richard Smith59efe262011-11-11 04:05:33 +0000640};
641struct Derived : Base, Base2 {
642 constexpr Derived() : Base(76), Base2(a) {}
643 int c = r + b[1];
644};
645
646constexpr bool operator==(const Base &a, const Base &b) {
647 return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
648}
649
650constexpr Base base;
651constexpr Base base2(76);
652constexpr Derived derived;
Richard Smith9eed49c2011-12-09 23:00:37 +0000653static_assert(derived.a == 76, "");
654static_assert(derived.b[2] == 's', "");
655static_assert(derived.c == 76 + 'e', "");
656static_assert(derived.q == 123, "");
657static_assert(derived.r == 76, "");
Richard Smith099e7f62011-12-19 06:19:21 +0000658static_assert(&derived.r == &derived.a, "");
Richard Smith59efe262011-11-11 04:05:33 +0000659
Richard Smith9eed49c2011-12-09 23:00:37 +0000660static_assert(!(derived == base), "");
661static_assert(derived == base2, "");
Richard Smith59efe262011-11-11 04:05:33 +0000662
Richard Smithe24f5fc2011-11-17 22:56:20 +0000663constexpr Bottom &bot1 = (Base&)derived;
664constexpr Bottom &bot2 = (Base2&)derived;
Richard Smith9eed49c2011-12-09 23:00:37 +0000665static_assert(&bot1 != &bot2, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000666
667constexpr Bottom *pb1 = (Base*)&derived;
668constexpr Bottom *pb2 = (Base2*)&derived;
Richard Smith9eed49c2011-12-09 23:00:37 +0000669static_assert(pb1 != pb2, "");
670static_assert(pb1 == &bot1, "");
671static_assert(pb2 == &bot2, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000672
Richard Smithb4e85ed2012-01-06 16:39:00 +0000673constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
674constexpr 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 +0000675constexpr Base2 &ok2 = (Base2&)bot2;
Richard Smith9eed49c2011-12-09 23:00:37 +0000676static_assert(&ok2 == &derived, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000677
Richard Smithb4e85ed2012-01-06 16:39:00 +0000678constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}}
679constexpr 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 +0000680constexpr Base2 *pok2 = (Base2*)pb2;
Richard Smith9eed49c2011-12-09 23:00:37 +0000681static_assert(pok2 == &derived, "");
682static_assert(&ok2 == pok2, "");
683static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
684static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000685
686constexpr Base *nullB = 42 - 6 * 7;
Richard Smith9eed49c2011-12-09 23:00:37 +0000687static_assert((Bottom*)nullB == 0, "");
688static_assert((Derived*)nullB == 0, "");
689static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000690
691}
692
693namespace Temporaries {
694
695struct S {
696 constexpr S() {}
697 constexpr int f();
698};
699struct T : S {
700 constexpr T(int n) : S(), n(n) {}
701 int n;
702};
703constexpr int S::f() {
704 // 'this' must be the postfix-expression in a class member access expression,
705 // so we can't just use
706 // return static_cast<T*>(this)->n;
707 return this->*(int(S::*))&T::n;
708}
709// The T temporary is implicitly cast to an S subobject, but we can recover the
710// T full-object via a base-to-derived cast, or a derived-to-base-casted member
711// pointer.
Richard Smith9eed49c2011-12-09 23:00:37 +0000712static_assert(T(3).f() == 3, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000713
714constexpr int f(const S &s) {
715 return static_cast<const T&>(s).n;
716}
717constexpr int n = f(T(5));
Richard Smith9eed49c2011-12-09 23:00:37 +0000718static_assert(f(T(5)) == 5, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000719
Richard Smith59efe262011-11-11 04:05:33 +0000720}
721
722namespace Union {
723
724union U {
725 int a;
726 int b;
727};
728
Richard Smithd7c56e12011-12-29 21:57:33 +0000729constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{C99 feature}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000730static_assert(u[0].a == 0, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000731static_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 +0000732static_assert(u[1].b == 1, "");
Richard Smith7098cbd2011-12-21 05:04:46 +0000733static_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 +0000734static_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 +0000735static_assert((&(u[1]) + 1 + 1)->b == 3, "");
Richard Smith59efe262011-11-11 04:05:33 +0000736
Richard Smithec789162012-01-12 18:54:33 +0000737constexpr U v = {};
738static_assert(v.a == 0, "");
739
740union Empty {};
741constexpr Empty e = {};
742
Richard Smith610a60c2012-01-10 04:32:03 +0000743// Make sure we handle trivial copy constructors for unions.
744constexpr U x = {42};
745constexpr U y = x;
746static_assert(y.a == 42, "");
747static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}}
748
Richard Smith59efe262011-11-11 04:05:33 +0000749}
750
Richard Smithe24f5fc2011-11-17 22:56:20 +0000751namespace MemberPointer {
752 struct A {
753 constexpr A(int n) : n(n) {}
754 int n;
755 constexpr int f() { return n + 3; }
756 };
757 constexpr A a(7);
Richard Smith9eed49c2011-12-09 23:00:37 +0000758 static_assert(A(5).*&A::n == 5, "");
759 static_assert((&a)->*&A::n == 7, "");
760 static_assert((A(8).*&A::f)() == 11, "");
761 static_assert(((&a)->*&A::f)() == 10, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000762
763 struct B : A {
764 constexpr B(int n, int m) : A(n), m(m) {}
765 int m;
766 constexpr int g() { return n + m + 1; }
767 };
768 constexpr B b(9, 13);
Richard Smith9eed49c2011-12-09 23:00:37 +0000769 static_assert(B(4, 11).*&A::n == 4, "");
770 static_assert(B(4, 11).*&B::m == 11, "");
771 static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
772 static_assert((&b)->*&A::n == 9, "");
773 static_assert((&b)->*&B::m == 13, "");
774 static_assert((&b)->*(int(A::*))&B::m == 13, "");
775 static_assert((B(4, 11).*&A::f)() == 7, "");
776 static_assert((B(4, 11).*&B::g)() == 16, "");
777 static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
778 static_assert(((&b)->*&A::f)() == 12, "");
779 static_assert(((&b)->*&B::g)() == 23, "");
780 static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000781
782 struct S {
783 constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
784 m(m), n(n), pf(pf), pn(pn) {}
785 constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
786
787 constexpr int f() { return this->*pn; }
788 virtual int g() const;
789
790 int m, n;
791 int (S::*pf)() const;
792 int S::*pn;
793 };
794
795 constexpr int S::*pm = &S::m;
796 constexpr int S::*pn = &S::n;
797 constexpr int (S::*pf)() const = &S::f;
798 constexpr int (S::*pg)() const = &S::g;
799
800 constexpr S s(2, 5, &S::f, &S::m);
801
Richard Smith9eed49c2011-12-09 23:00:37 +0000802 static_assert((s.*&S::f)() == 2, "");
803 static_assert((s.*s.pf)() == 2, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000804
805 template<int n> struct T : T<n-1> {};
806 template<> struct T<0> { int n; };
807 template<> struct T<30> : T<29> { int m; };
808
809 T<17> t17;
810 T<30> t30;
811
812 constexpr int (T<10>::*deepn) = &T<0>::n;
Richard Smith9eed49c2011-12-09 23:00:37 +0000813 static_assert(&(t17.*deepn) == &t17.n, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000814
815 constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
816 constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
Richard Smith9eed49c2011-12-09 23:00:37 +0000817 static_assert(&(t30.*deepm) == &t30.m, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000818
819 constexpr T<5> *p17_5 = &t17;
820 constexpr T<13> *p17_13 = (T<13>*)p17_5;
Richard Smithb4e85ed2012-01-06 16:39:00 +0000821 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 +0000822 static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
823 static_assert(&(p17_13->*deepn) == &t17.n, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000824 constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
825
826 constexpr T<5> *p30_5 = &t30;
827 constexpr T<23> *p30_23 = (T<23>*)p30_5;
828 constexpr T<13> *p30_13 = p30_23;
Richard Smith9eed49c2011-12-09 23:00:37 +0000829 static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
830 static_assert(&(p30_13->*deepn) == &t30.n, "");
831 static_assert(&(p30_23->*deepn) == &t30.n, "");
832 static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
833 static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
834 static_assert(&(p30_23->*deepm) == &t30.m, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000835}
836
837namespace ArrayBaseDerived {
838
839 struct Base {
840 constexpr Base() {}
841 int n = 0;
842 };
843 struct Derived : Base {
844 constexpr Derived() {}
845 constexpr const int *f() { return &n; }
846 };
847
848 constexpr Derived a[10];
849 constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
850 constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
Richard Smith9eed49c2011-12-09 23:00:37 +0000851 static_assert(pb3 == pd3, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000852
853 // pb3 does not point to an array element.
854 constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
Richard Smithb4e85ed2012-01-06 16:39:00 +0000855 constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}}
856 constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}}
857 constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}}
858 constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}}
859 constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}}
Richard Smithe24f5fc2011-11-17 22:56:20 +0000860 constexpr Base *pb3a = pb4 - 1;
861
862 // pb4 does not point to a Derived.
Richard Smithb4e85ed2012-01-06 16:39:00 +0000863 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 +0000864 constexpr Derived *pd3a = (Derived*)pb3a;
865 constexpr int pd3n = pd3a->n;
866
867 // pd3a still points to the Derived array.
868 constexpr Derived *pd6 = pd3a + 3;
Richard Smith9eed49c2011-12-09 23:00:37 +0000869 static_assert(pd6 == &a[6], "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000870 constexpr Derived *pd9 = pd6 + 3;
871 constexpr Derived *pd10 = pd6 + 4;
872 constexpr int pd9n = pd9->n; // ok
Richard Smithb4e85ed2012-01-06 16:39:00 +0000873 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 +0000874 constexpr int pd0n = pd10[-10].n;
Richard Smithb4e85ed2012-01-06 16:39:00 +0000875 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 +0000876
877 constexpr Base *pb9 = pd9;
878 constexpr const int *(Base::*pfb)() const =
879 static_cast<const int *(Base::*)() const>(&Derived::f);
Richard Smith9eed49c2011-12-09 23:00:37 +0000880 static_assert((pb9->*pfb)() == &a[9].n, "");
Richard Smithe24f5fc2011-11-17 22:56:20 +0000881}
882
Richard Smith59efe262011-11-11 04:05:33 +0000883namespace Complex {
884
885class complex {
886 int re, im;
887public:
888 constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
889 constexpr complex(const complex &o) : re(o.re), im(o.im) {}
890 constexpr complex operator-() const { return complex(-re, -im); }
891 friend constexpr complex operator+(const complex &l, const complex &r) {
892 return complex(l.re + r.re, l.im + r.im);
893 }
894 friend constexpr complex operator-(const complex &l, const complex &r) {
895 return l + -r;
896 }
897 friend constexpr complex operator*(const complex &l, const complex &r) {
898 return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
899 }
900 friend constexpr bool operator==(const complex &l, const complex &r) {
901 return l.re == r.re && l.im == r.im;
902 }
903 constexpr bool operator!=(const complex &r) const {
904 return re != r.re || im != r.im;
905 }
906 constexpr int real() const { return re; }
907 constexpr int imag() const { return im; }
908};
909
910constexpr complex i = complex(0, 1);
911constexpr complex k = (3 + 4*i) * (6 - 4*i);
Richard Smith9eed49c2011-12-09 23:00:37 +0000912static_assert(complex(1,0).real() == 1, "");
913static_assert(complex(1,0).imag() == 0, "");
914static_assert(((complex)1).imag() == 0, "");
915static_assert(k.real() == 34, "");
916static_assert(k.imag() == 12, "");
917static_assert(k - 34 == 12*i, "");
918static_assert((complex)1 == complex(1), "");
919static_assert((complex)1 != complex(0, 1), "");
920static_assert(complex(1) == complex(1), "");
921static_assert(complex(1) != complex(0, 1), "");
Richard Smith59efe262011-11-11 04:05:33 +0000922constexpr complex makeComplex(int re, int im) { return complex(re, im); }
Richard Smith9eed49c2011-12-09 23:00:37 +0000923static_assert(makeComplex(1,0) == complex(1), "");
924static_assert(makeComplex(1,0) != complex(0, 1), "");
Richard Smith59efe262011-11-11 04:05:33 +0000925
926class complex_wrap : public complex {
927public:
928 constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
929 constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
930};
931
Richard Smith9eed49c2011-12-09 23:00:37 +0000932static_assert((complex_wrap)1 == complex(1), "");
933static_assert((complex)1 != complex_wrap(0, 1), "");
934static_assert(complex(1) == complex_wrap(1), "");
935static_assert(complex_wrap(1) != complex(0, 1), "");
Richard Smith59efe262011-11-11 04:05:33 +0000936constexpr complex_wrap makeComplexWrap(int re, int im) {
937 return complex_wrap(re, im);
938}
Richard Smith9eed49c2011-12-09 23:00:37 +0000939static_assert(makeComplexWrap(1,0) == complex(1), "");
940static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
Richard Smith59efe262011-11-11 04:05:33 +0000941
942}
Eli Friedmanf59ff8c2011-12-17 02:24:21 +0000943
944namespace PR11595 {
945 struct A { constexpr bool operator==(int x) { return true; } };
Richard Smithaf2c7a12011-12-19 22:01:37 +0000946 struct B { B(); A& x; };
947 static_assert(B().x == 3, ""); // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
948
949 constexpr bool f(int k) {
950 return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
951 }
952 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 +0000953}
Richard Smithbc6abe92011-12-19 22:12:41 +0000954
955namespace ExprWithCleanups {
956 struct A { A(); ~A(); int get(); };
957 constexpr int get(bool FromA) { return FromA ? A().get() : 1; }
958 constexpr int n = get(false);
959}
Richard Smith7098cbd2011-12-21 05:04:46 +0000960
961namespace Volatile {
962
963volatile constexpr int n1 = 0; // expected-note {{here}}
964volatile const int n2 = 0; // expected-note {{here}}
965int n3 = 37; // expected-note {{declared here}}
966
967constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
968constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
969
970struct T { int n; };
971const T t = { 42 }; // expected-note {{declared here}}
972
973constexpr int f(volatile int &&r) {
974 return r; // expected-note {{read of volatile temporary is not allowed in a constant expression}}
975}
976struct S {
977 int k : f(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'f(0)'}}
978 int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
979 int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}}
980};
981
982}
Richard Smithdd4b3502011-12-25 21:17:58 +0000983
984namespace ExternConstexpr {
985 extern constexpr int n = 0;
986 extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}}
987 void f() {
988 extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}}
989 constexpr int j = 0;
990 constexpr int k; // expected-error {{default initialization of an object of const type}}
991 }
992}
Eli Friedman7ead5c72012-01-10 04:58:17 +0000993
994namespace ComplexConstexpr {
995 constexpr _Complex float test1 = {};
996 constexpr _Complex float test2 = {1};
997 constexpr _Complex double test3 = {1,2};
998 constexpr _Complex int test4 = {4};
999 constexpr _Complex int test5 = 4;
1000 constexpr _Complex int test6 = {5,6};
1001}