blob: af0d5f3e1c37905c54860d16ac5cd224e5f5a9f3 [file] [log] [blame]
Richard Smith6d6ecc32011-12-12 12:46:16 +00001// RUN: %clang_cc1 -triple i686-linux -fsyntax-only -verify -std=c++11 -pedantic %s -Wno-comment
Richard Smithe97cbd72011-11-11 04:05:33 +00002
Richard Smithe97cbd72011-11-11 04:05:33 +00003namespace StaticAssertFoldTest {
4
5int x;
Richard Smith0765bec2011-12-09 23:00:37 +00006static_assert(++x, "test"); // expected-error {{not an integral constant expression}}
7static_assert(false, "test"); // expected-error {{test}}
Richard Smithe97cbd72011-11-11 04:05:33 +00008
9}
10
Richard Smith242ad892011-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 Smithe97cbd72011-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 Smith0765bec2011-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 Smithe97cbd72011-11-11 04:05:33 +000043
44 constexpr B &pp = d;
45 constexpr C &qq = d;
Richard Smith0765bec2011-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 Smithe97cbd72011-11-11 04:05:33 +000049
50 constexpr V *v = p;
51 constexpr V *w = q;
52 constexpr V *x = (A*)p;
Richard Smith0765bec2011-12-09 23:00:37 +000053 static_assert(v == w, "");
54 static_assert(v == x, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000055
Richard Smith0765bec2011-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 Smithe97cbd72011-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 Smith0765bec2011-12-09 23:00:37 +000067 static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000068
69}
70
Richard Smith6804be52011-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 Smithe97cbd72011-11-11 04:05:33 +000084namespace TemplateArgumentConversion {
85 template<int n> struct IntParam {};
86
87 using IntParam0 = IntParam<0>;
Richard Smith242ad892011-12-21 02:55:12 +000088 using IntParam0 = IntParam<id(0)>;
Richard Smithe97cbd72011-11-11 04:05:33 +000089 using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} expected-error {{not an integral constant expression}}
90}
91
92namespace CaseStatements {
93 void f(int n) {
94 switch (n) {
95 // FIXME: Produce the 'add ()' fixit for this.
Richard Smith357362d2011-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 Smith242ad892011-12-21 02:55:12 +000097 case id(1):
Richard Smithe97cbd72011-11-11 04:05:33 +000098 return;
99 }
100 }
101}
102
103extern int &Recurse1;
Richard Smithd0b4dd62011-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 Smithe97cbd72011-11-11 04:05:33 +0000112
113namespace MemberEnum {
114 struct WithMemberEnum {
115 enum E { A = 42 };
116 } wme;
117
Richard Smith0765bec2011-12-09 23:00:37 +0000118 static_assert(wme.A == 42, "");
Richard Smithe97cbd72011-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 Smith0765bec2011-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 Smithe97cbd72011-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 Smith0765bec2011-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 Smithf2b681b2011-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 Smithe97cbd72011-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 Smith0765bec2011-12-09 23:00:37 +0000153 static_assert(fib(11) == 89, "");
Richard Smithe97cbd72011-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 Smith0765bec2011-12-09 23:00:37 +0000162 static_assert(gcd(1749237, 5628959) == 7, "");
Richard Smithe97cbd72011-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 Smith6d6ecc32011-12-12 12:46:16 +0000171 int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{extension}}
Richard Smithe97cbd72011-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 Smith0765bec2011-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 Smithce40ad62011-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 Smithe97cbd72011-11-11 04:05:33 +0000189}
190
191namespace ParameterScopes {
192
193 const int k = 42;
Richard Smithd0b4dd62011-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 Smithe97cbd72011-11-11 04:05:33 +0000197 }
Richard Smith0765bec2011-12-09 23:00:37 +0000198 static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
Richard Smithd0b4dd62011-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 Smithe97cbd72011-11-11 04:05:33 +0000200
Richard Smithd0b4dd62011-12-19 06:19:21 +0000201 constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { // expected-note {{here}}
Richard Smithe97cbd72011-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 Smithd0b4dd62011-12-19 06:19:21 +0000204 return ObscureTheTruth(b ? a : k); // expected-note {{in call to 'ObscureTheTruth(a)'}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000205 }
Richard Smith0765bec2011-12-09 23:00:37 +0000206 static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
Richard Smithd0b4dd62011-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 Smithe97cbd72011-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 Smithd0b4dd62011-12-19 06:19:21 +0000211 return MaybeReturnJunk(true, n); // expected-note {{in call to 'MaybeReturnJunk(1, 0)'}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000212 }
Richard Smithd0b4dd62011-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 Smithe97cbd72011-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 Smith0765bec2011-12-09 23:00:37 +0000219 static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
220 static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
Richard Smithe97cbd72011-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 Smith0765bec2011-12-09 23:00:37 +0000231 static_assert(f(23, &x, &y, &z) == 788, "");
Richard Smithe97cbd72011-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 Smith0765bec2011-12-09 23:00:37 +0000236 static_assert(g(23, x, y, z) == 788, "");
Richard Smithe97cbd72011-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 Smithd0b4dd62011-12-19 06:19:21 +0000249 constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000250
Richard Smith0765bec2011-12-09 23:00:37 +0000251 static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000252
Richard Smithd0b4dd62011-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 Smithe97cbd72011-11-11 04:05:33 +0000254
255}
256
257namespace PointerComparison {
258
259int x, y;
Richard Smith0765bec2011-12-09 23:00:37 +0000260static_assert(&x == &y, "false"); // expected-error {{false}}
261static_assert(&x != &y, "");
Richard Smithe97cbd72011-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 Smith0765bec2011-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 Smithe97cbd72011-11-11 04:05:33 +0000276
Richard Smith0765bec2011-12-09 23:00:37 +0000277static_assert(0 == &y, "false"); // expected-error {{false}}
278static_assert(0 != &y, "");
Richard Smithe97cbd72011-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 Smith0765bec2011-12-09 23:00:37 +0000284static_assert(&x == 0, "false"); // expected-error {{false}}
285static_assert(&x != 0, "");
Richard Smithe97cbd72011-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 Smith0765bec2011-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 Smithe97cbd72011-11-11 04:05:33 +0000297
298constexpr S* sptr = &s;
Richard Smithd0b4dd62011-12-19 06:19:21 +0000299constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000300
Richard Smith6d6ecc32011-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 Smithff07af12011-12-12 19:10:03 +0000305 expected-note {{dynamic_cast is not allowed in a constant expression}}
Richard Smith6d6ecc32011-12-12 12:46:16 +0000306 int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
307 expected-warning {{not integer constant expression}} \
Richard Smithff07af12011-12-12 19:10:03 +0000308 expected-note {{reinterpret_cast is not allowed in a constant expression}}
Richard Smith6d6ecc32011-12-12 12:46:16 +0000309 int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
310 expected-warning {{not integer constant expression}} \
Richard Smithdcf1b2c2011-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 Smith6d6ecc32011-12-12 12:46:16 +0000312 int d : (S*)(42) == (S*)(42); // \
313 expected-warning {{not integer constant expression}} \
Richard Smithdcf1b2c2011-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 Smith6d6ecc32011-12-12 12:46:16 +0000315 int e : (Str*)(sptr) == (Str*)(sptr); // \
316 expected-warning {{not integer constant expression}} \
Richard Smithdcf1b2c2011-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 Smith6d6ecc32011-12-12 12:46:16 +0000318 int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \
319 expected-warning {{not integer constant expression}} \
Richard Smithdcf1b2c2011-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 Smith6d6ecc32011-12-12 12:46:16 +0000321 int g : (S*)(void*)(sptr) == sptr; // \
322 expected-warning {{not integer constant expression}} \
Richard Smithff07af12011-12-12 19:10:03 +0000323 expected-note {{cast from 'void *' is not allowed in a constant expression}}
Richard Smith6d6ecc32011-12-12 12:46:16 +0000324};
325
Richard Smithe97cbd72011-11-11 04:05:33 +0000326extern char externalvar[];
Richard Smith0765bec2011-12-09 23:00:37 +0000327constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000328constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
Richard Smith0765bec2011-12-09 23:00:37 +0000329static_assert(0 != "foo", "");
Richard Smithe97cbd72011-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 Smith0765bec2011-12-09 23:00:37 +0000341static_assert(n, "");
342static_assert(!same(4, 4), "");
343static_assert(same(n, n), "");
344static_assert(sameTemporary(9), "");
Richard Smithe97cbd72011-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 Smith242ad892011-12-21 02:55:12 +0000354template<typename Char>
355constexpr int MangleChars(const Char *p) {
Richard Smithe97cbd72011-11-11 04:05:33 +0000356 return *p + 3 * (*p ? MangleChars(p+1) : 0);
357}
358
Richard Smith0765bec2011-12-09 23:00:37 +0000359static_assert(MangleChars("constexpr!") == 1768383, "");
360static_assert(MangleChars(u"constexpr!") == 1768383, "");
361static_assert(MangleChars(U"constexpr!") == 1768383, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000362
363constexpr char c0 = "nought index"[0];
364constexpr char c1 = "nice index"[10];
Richard Smithf2b681b2011-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}}
366// FIXME: block the pointer arithmetic with undefined behavior here
367constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000368constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}}
369
370constexpr const char *p = "test" + 2;
Richard Smith0765bec2011-12-09 23:00:37 +0000371static_assert(*p == 's', "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000372
373constexpr const char *max_iter(const char *a, const char *b) {
374 return *a < *b ? b : a;
375}
376constexpr const char *max_element(const char *a, const char *b) {
377 return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
378}
379
Richard Smithe97cbd72011-11-11 04:05:33 +0000380constexpr char str[] = "the quick brown fox jumped over the lazy dog";
381constexpr const char *max = max_element(begin(str), end(str));
Richard Smith0765bec2011-12-09 23:00:37 +0000382static_assert(*max == 'z', "");
383static_assert(max == str + 38, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000384
Richard Smith0765bec2011-12-09 23:00:37 +0000385static_assert(strcmp_ce("hello world", "hello world") == 0, "");
386static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
387static_assert(strcmp_ce("constexpr", "test") < 0, "");
388static_assert(strcmp_ce("", " ") < 0, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000389
Richard Smithf2b681b2011-12-21 05:04:46 +0000390struct S {
391 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}}
392};
393
Richard Smithe97cbd72011-11-11 04:05:33 +0000394}
395
396namespace Array {
397
Richard Smith242ad892011-12-21 02:55:12 +0000398template<typename Iter>
399constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) {
Richard Smithe97cbd72011-11-11 04:05:33 +0000400 return begin == end ? 0 : *begin + Sum(begin+1, end);
401}
Richard Smithe97cbd72011-11-11 04:05:33 +0000402
403constexpr int xs[] = { 1, 2, 3, 4, 5 };
404constexpr int ys[] = { 5, 4, 3, 2, 1 };
405constexpr int sum_xs = Sum(begin(xs), end(xs));
Richard Smith0765bec2011-12-09 23:00:37 +0000406static_assert(sum_xs == 15, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000407
408constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
409 const int *xs, const int *ys, int c) {
Richard Smith902ca212011-12-14 23:32:26 +0000410 return n ? F(
Richard Smithf2b681b2011-12-21 05:04:46 +0000411 *xs, // expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smithf6f003a2011-12-16 19:06:07 +0000412 *ys,
413 ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
414 expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
415 expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
416 : c;
Richard Smithe97cbd72011-11-11 04:05:33 +0000417}
418constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
419constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
Richard Smith0765bec2011-12-09 23:00:37 +0000420static_assert(InnerProduct == 35, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000421
422constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
423constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
Richard Smith0765bec2011-12-09 23:00:37 +0000424static_assert(DiffProd == 8, "");
Richard Smithf6f003a2011-12-16 19:06:07 +0000425static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
426 expected-error {{constant expression}} \
427 expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000428
429constexpr const int *p = xs + 3;
430constexpr int xs4 = p[1]; // ok
Richard Smithf2b681b2011-12-21 05:04:46 +0000431constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000432constexpr int xs0 = p[-3]; // ok
Richard Smithf2b681b2011-12-21 05:04:46 +0000433// FIXME: check pointer arithmetic here
434constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000435
436constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
Richard Smith0765bec2011-12-09 23:00:37 +0000437static_assert(zs[0][0][0][0] == 1, "");
438static_assert(zs[1][1][1][1] == 16, "");
Richard Smithf2b681b2011-12-21 05:04:46 +0000439static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smith0765bec2011-12-09 23:00:37 +0000440static_assert((&zs[0][0][0][2])[-1] == 2, "");
441static_assert(**(**(zs + 1) + 1) == 11, "");
442static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000443
Richard Smithf6f003a2011-12-16 19:06:07 +0000444constexpr int fail(const int &p) {
Richard Smithf2b681b2011-12-21 05:04:46 +0000445 return (&p)[64]; // expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smithf6f003a2011-12-16 19:06:07 +0000446}
447static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1)) == 11, ""); // \
448expected-error {{static_assert expression is not an integral constant expression}} \
449expected-note {{in call to 'fail(zs[1][0][1][0])'}}
450
Richard Smith6d6ecc32011-12-12 12:46:16 +0000451constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{extension}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000452constexpr int SumNonzero(const int *p) {
453 return *p + (*p ? SumNonzero(p+1) : 0);
454}
455constexpr int CountZero(const int *p, const int *q) {
456 return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
457}
Richard Smith0765bec2011-12-09 23:00:37 +0000458static_assert(SumNonzero(arr) == 6, "");
459static_assert(CountZero(arr, arr + 40) == 36, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000460
Richard Smith027bf112011-11-17 22:56:20 +0000461struct ArrayElem {
462 constexpr ArrayElem() : n(0) {}
463 int n;
464 constexpr int f() { return n; }
465};
466struct ArrayRVal {
467 constexpr ArrayRVal() {}
468 ArrayElem elems[10];
469};
Richard Smith0765bec2011-12-09 23:00:37 +0000470static_assert(ArrayRVal().elems[3].f() == 0, "");
Richard Smith027bf112011-11-17 22:56:20 +0000471
Richard Smithe97cbd72011-11-11 04:05:33 +0000472}
473
474namespace DependentValues {
475
476struct I { int n; typedef I V[10]; };
477I::V x, y;
478template<bool B> struct S {
479 int k;
480 void f() {
481 I::V &cells = B ? x : y;
482 I &i = cells[k];
483 switch (i.n) {}
484 }
485};
486
487}
488
489namespace Class {
490
491struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
492constexpr int fn(const A &a) { return a.k; }
Richard Smith0765bec2011-12-09 23:00:37 +0000493static_assert(fn(A(4,5)) == 9, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000494
495struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
496struct C {
497 constexpr C(C *this_) : m(42), n(this_->m) {} // ok
498 int m, n;
499};
500struct D {
501 C c;
502 constexpr D() : c(&c) {}
503};
Richard Smith0765bec2011-12-09 23:00:37 +0000504static_assert(D().c.n == 42, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000505
Richard Smithd0b4dd62011-12-19 06:19:21 +0000506struct E { // expected-note {{here}}
507 constexpr E() : p(&p) {} // expected-note {{pointer to temporary cannot be used to initialize a member in a constant expression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000508 void *p;
509};
Richard Smithd0b4dd62011-12-19 06:19:21 +0000510constexpr const E &e1 = E(); // expected-error {{constant expression}} expected-note {{in call to 'E()'}} expected-note {{temporary created here}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000511// This is a constant expression if we elide the copy constructor call, and
512// is not a constant expression if we don't! But we do, so it is.
513// FIXME: The move constructor is not currently implicitly defined as constexpr.
Richard Smithd0b4dd62011-12-19 06:19:21 +0000514constexpr E e2 = E(); // unexpected-error {{constant expression}} unexpected-note {{here}} unexpected-note {{non-constexpr constructor 'E' cannot be used in a constant expression}}
515static_assert(e2.p == &e2.p, ""); // unexpected-error {{constant expression}} unexpected-note {{initializer of 'e2' is not a constant expression}}
516constexpr E e3;
Richard Smith0765bec2011-12-09 23:00:37 +0000517static_assert(e3.p == &e3.p, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000518
519extern const class F f;
520struct F {
521 constexpr F() : p(&f.p) {}
522 const void *p;
523};
Richard Smithd0b4dd62011-12-19 06:19:21 +0000524constexpr F f;
Richard Smithe97cbd72011-11-11 04:05:33 +0000525
526struct G {
527 struct T {
528 constexpr T(T *p) : u1(), u2(p) {}
529 union U1 {
530 constexpr U1() {}
531 int a, b = 42;
532 } u1;
533 union U2 {
534 constexpr U2(T *p) : c(p->u1.b) {}
535 int c, d;
536 } u2;
537 } t;
538 constexpr G() : t(&t) {}
539} constexpr g;
540
Richard Smithf2b681b2011-12-21 05:04:46 +0000541static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
Richard Smith0765bec2011-12-09 23:00:37 +0000542static_assert(g.t.u1.b == 42, "");
543static_assert(g.t.u2.c == 42, "");
Richard Smithf2b681b2011-12-21 05:04:46 +0000544static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000545
546struct S {
547 int a, b;
548 const S *p;
549 double d;
550 const char *q;
551
552 constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
553};
554
555S global(43, &global);
556
Richard Smith0765bec2011-12-09 23:00:37 +0000557static_assert(S(15, &global).b == 15, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000558
559constexpr bool CheckS(const S &s) {
560 return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
561}
Richard Smith0765bec2011-12-09 23:00:37 +0000562static_assert(CheckS(S(27, &global)), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000563
564struct Arr {
565 char arr[3];
566 constexpr Arr() : arr{'x', 'y', 'z'} {}
567};
568constexpr int hash(Arr &&a) {
569 return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
570}
571constexpr int k = hash(Arr());
Richard Smith0765bec2011-12-09 23:00:37 +0000572static_assert(k == 0x007a7978, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000573
574
575struct AggregateInit {
576 const char &c;
577 int n;
578 double d;
579 int arr[5];
580 void *p;
581};
582
583constexpr AggregateInit agg1 = { "hello"[0] };
584
Richard Smith0765bec2011-12-09 23:00:37 +0000585static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
586static_assert(agg1.n == 0, "");
587static_assert(agg1.d == 0.0, "");
Richard Smithf2b681b2011-12-21 05:04:46 +0000588// FIXME: check pointer arithmetic here.
589static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
Richard Smith0765bec2011-12-09 23:00:37 +0000590static_assert(agg1.arr[0] == 0, "");
591static_assert(agg1.arr[4] == 0, "");
Richard Smithf2b681b2011-12-21 05:04:46 +0000592static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
Richard Smith0765bec2011-12-09 23:00:37 +0000593static_assert(agg1.p == nullptr, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000594
595namespace SimpleDerivedClass {
596
597struct B {
598 constexpr B(int n) : a(n) {}
599 int a;
600};
601struct D : B {
602 constexpr D(int n) : B(n) {}
603};
604constexpr D d(3);
Richard Smith0765bec2011-12-09 23:00:37 +0000605static_assert(d.a == 3, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000606
607}
608
Richard Smith027bf112011-11-17 22:56:20 +0000609struct Bottom { constexpr Bottom() {} };
610struct Base : Bottom {
Richard Smithe97cbd72011-11-11 04:05:33 +0000611 constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
612 int a;
613 const char *b;
614};
Richard Smith027bf112011-11-17 22:56:20 +0000615struct Base2 : Bottom {
Richard Smithe97cbd72011-11-11 04:05:33 +0000616 constexpr Base2(const int &r) : r(r) {}
617 int q = 123;
Richard Smithd0b4dd62011-12-19 06:19:21 +0000618 const int &r;
Richard Smithe97cbd72011-11-11 04:05:33 +0000619};
620struct Derived : Base, Base2 {
621 constexpr Derived() : Base(76), Base2(a) {}
622 int c = r + b[1];
623};
624
625constexpr bool operator==(const Base &a, const Base &b) {
626 return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
627}
628
629constexpr Base base;
630constexpr Base base2(76);
631constexpr Derived derived;
Richard Smith0765bec2011-12-09 23:00:37 +0000632static_assert(derived.a == 76, "");
633static_assert(derived.b[2] == 's', "");
634static_assert(derived.c == 76 + 'e', "");
635static_assert(derived.q == 123, "");
636static_assert(derived.r == 76, "");
Richard Smithd0b4dd62011-12-19 06:19:21 +0000637static_assert(&derived.r == &derived.a, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000638
Richard Smith0765bec2011-12-09 23:00:37 +0000639static_assert(!(derived == base), "");
640static_assert(derived == base2, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000641
Richard Smith027bf112011-11-17 22:56:20 +0000642constexpr Bottom &bot1 = (Base&)derived;
643constexpr Bottom &bot2 = (Base2&)derived;
Richard Smith0765bec2011-12-09 23:00:37 +0000644static_assert(&bot1 != &bot2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000645
646constexpr Bottom *pb1 = (Base*)&derived;
647constexpr Bottom *pb2 = (Base2*)&derived;
Richard Smith0765bec2011-12-09 23:00:37 +0000648static_assert(pb1 != pb2, "");
649static_assert(pb1 == &bot1, "");
650static_assert(pb2 == &bot2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000651
652constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}}
653constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}}
654constexpr Base2 &ok2 = (Base2&)bot2;
Richard Smith0765bec2011-12-09 23:00:37 +0000655static_assert(&ok2 == &derived, "");
Richard Smith027bf112011-11-17 22:56:20 +0000656
657constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}}
658constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}}
659constexpr Base2 *pok2 = (Base2*)pb2;
Richard Smith0765bec2011-12-09 23:00:37 +0000660static_assert(pok2 == &derived, "");
661static_assert(&ok2 == pok2, "");
662static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
663static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000664
665constexpr Base *nullB = 42 - 6 * 7;
Richard Smith0765bec2011-12-09 23:00:37 +0000666static_assert((Bottom*)nullB == 0, "");
667static_assert((Derived*)nullB == 0, "");
668static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
Richard Smith027bf112011-11-17 22:56:20 +0000669
670}
671
672namespace Temporaries {
673
674struct S {
675 constexpr S() {}
676 constexpr int f();
677};
678struct T : S {
679 constexpr T(int n) : S(), n(n) {}
680 int n;
681};
682constexpr int S::f() {
683 // 'this' must be the postfix-expression in a class member access expression,
684 // so we can't just use
685 // return static_cast<T*>(this)->n;
686 return this->*(int(S::*))&T::n;
687}
688// The T temporary is implicitly cast to an S subobject, but we can recover the
689// T full-object via a base-to-derived cast, or a derived-to-base-casted member
690// pointer.
Richard Smith0765bec2011-12-09 23:00:37 +0000691static_assert(T(3).f() == 3, "");
Richard Smith027bf112011-11-17 22:56:20 +0000692
693constexpr int f(const S &s) {
694 return static_cast<const T&>(s).n;
695}
696constexpr int n = f(T(5));
Richard Smith0765bec2011-12-09 23:00:37 +0000697static_assert(f(T(5)) == 5, "");
Richard Smith027bf112011-11-17 22:56:20 +0000698
Richard Smithe97cbd72011-11-11 04:05:33 +0000699}
700
701namespace Union {
702
703union U {
704 int a;
705 int b;
706};
707
Richard Smith6d6ecc32011-12-12 12:46:16 +0000708constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{extension}}
Richard Smith0765bec2011-12-09 23:00:37 +0000709static_assert(u[0].a == 0, "");
Richard Smithf2b681b2011-12-21 05:04:46 +0000710static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
Richard Smith0765bec2011-12-09 23:00:37 +0000711static_assert(u[1].b == 1, "");
Richard Smithf2b681b2011-12-21 05:04:46 +0000712static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smith902ca212011-12-14 23:32:26 +0000713static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000714static_assert((&(u[1]) + 1 + 1)->b == 3, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000715
716}
717
Richard Smith027bf112011-11-17 22:56:20 +0000718namespace MemberPointer {
719 struct A {
720 constexpr A(int n) : n(n) {}
721 int n;
722 constexpr int f() { return n + 3; }
723 };
724 constexpr A a(7);
Richard Smith0765bec2011-12-09 23:00:37 +0000725 static_assert(A(5).*&A::n == 5, "");
726 static_assert((&a)->*&A::n == 7, "");
727 static_assert((A(8).*&A::f)() == 11, "");
728 static_assert(((&a)->*&A::f)() == 10, "");
Richard Smith027bf112011-11-17 22:56:20 +0000729
730 struct B : A {
731 constexpr B(int n, int m) : A(n), m(m) {}
732 int m;
733 constexpr int g() { return n + m + 1; }
734 };
735 constexpr B b(9, 13);
Richard Smith0765bec2011-12-09 23:00:37 +0000736 static_assert(B(4, 11).*&A::n == 4, "");
737 static_assert(B(4, 11).*&B::m == 11, "");
738 static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
739 static_assert((&b)->*&A::n == 9, "");
740 static_assert((&b)->*&B::m == 13, "");
741 static_assert((&b)->*(int(A::*))&B::m == 13, "");
742 static_assert((B(4, 11).*&A::f)() == 7, "");
743 static_assert((B(4, 11).*&B::g)() == 16, "");
744 static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
745 static_assert(((&b)->*&A::f)() == 12, "");
746 static_assert(((&b)->*&B::g)() == 23, "");
747 static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
Richard Smith027bf112011-11-17 22:56:20 +0000748
749 struct S {
750 constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
751 m(m), n(n), pf(pf), pn(pn) {}
752 constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
753
754 constexpr int f() { return this->*pn; }
755 virtual int g() const;
756
757 int m, n;
758 int (S::*pf)() const;
759 int S::*pn;
760 };
761
762 constexpr int S::*pm = &S::m;
763 constexpr int S::*pn = &S::n;
764 constexpr int (S::*pf)() const = &S::f;
765 constexpr int (S::*pg)() const = &S::g;
766
767 constexpr S s(2, 5, &S::f, &S::m);
768
Richard Smith0765bec2011-12-09 23:00:37 +0000769 static_assert((s.*&S::f)() == 2, "");
770 static_assert((s.*s.pf)() == 2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000771
772 template<int n> struct T : T<n-1> {};
773 template<> struct T<0> { int n; };
774 template<> struct T<30> : T<29> { int m; };
775
776 T<17> t17;
777 T<30> t30;
778
779 constexpr int (T<10>::*deepn) = &T<0>::n;
Richard Smith0765bec2011-12-09 23:00:37 +0000780 static_assert(&(t17.*deepn) == &t17.n, "");
Richard Smith027bf112011-11-17 22:56:20 +0000781
782 constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
783 constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000784 static_assert(&(t30.*deepm) == &t30.m, "");
Richard Smith027bf112011-11-17 22:56:20 +0000785
786 constexpr T<5> *p17_5 = &t17;
787 constexpr T<13> *p17_13 = (T<13>*)p17_5;
788 constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000789 static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
790 static_assert(&(p17_13->*deepn) == &t17.n, "");
Richard Smith027bf112011-11-17 22:56:20 +0000791 constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
792
793 constexpr T<5> *p30_5 = &t30;
794 constexpr T<23> *p30_23 = (T<23>*)p30_5;
795 constexpr T<13> *p30_13 = p30_23;
Richard Smith0765bec2011-12-09 23:00:37 +0000796 static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
797 static_assert(&(p30_13->*deepn) == &t30.n, "");
798 static_assert(&(p30_23->*deepn) == &t30.n, "");
799 static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
800 static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
801 static_assert(&(p30_23->*deepm) == &t30.m, "");
Richard Smith027bf112011-11-17 22:56:20 +0000802}
803
804namespace ArrayBaseDerived {
805
806 struct Base {
807 constexpr Base() {}
808 int n = 0;
809 };
810 struct Derived : Base {
811 constexpr Derived() {}
812 constexpr const int *f() { return &n; }
813 };
814
815 constexpr Derived a[10];
816 constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
817 constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
Richard Smith0765bec2011-12-09 23:00:37 +0000818 static_assert(pb3 == pd3, "");
Richard Smith027bf112011-11-17 22:56:20 +0000819
820 // pb3 does not point to an array element.
821 constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
822 constexpr int pb4n = pb4->n; // expected-error {{constant expression}}
823 constexpr Base *err_pb5 = pb3 + 2; // FIXME: reject this.
824 constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}}
825 constexpr Base *err_pb2 = pb3 - 1; // FIXME: reject this.
826 constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}}
827 constexpr Base *pb3a = pb4 - 1;
828
829 // pb4 does not point to a Derived.
830 constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}}
831 constexpr Derived *pd3a = (Derived*)pb3a;
832 constexpr int pd3n = pd3a->n;
833
834 // pd3a still points to the Derived array.
835 constexpr Derived *pd6 = pd3a + 3;
Richard Smith0765bec2011-12-09 23:00:37 +0000836 static_assert(pd6 == &a[6], "");
Richard Smith027bf112011-11-17 22:56:20 +0000837 constexpr Derived *pd9 = pd6 + 3;
838 constexpr Derived *pd10 = pd6 + 4;
839 constexpr int pd9n = pd9->n; // ok
Richard Smithf2b681b2011-12-21 05:04:46 +0000840 constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smith027bf112011-11-17 22:56:20 +0000841 constexpr int pd0n = pd10[-10].n;
Richard Smithf2b681b2011-12-21 05:04:46 +0000842 // FIXME: check pointer arithmetic here.
843 constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smith027bf112011-11-17 22:56:20 +0000844
845 constexpr Base *pb9 = pd9;
846 constexpr const int *(Base::*pfb)() const =
847 static_cast<const int *(Base::*)() const>(&Derived::f);
Richard Smith0765bec2011-12-09 23:00:37 +0000848 static_assert((pb9->*pfb)() == &a[9].n, "");
Richard Smith027bf112011-11-17 22:56:20 +0000849}
850
Richard Smithe97cbd72011-11-11 04:05:33 +0000851namespace Complex {
852
853class complex {
854 int re, im;
855public:
856 constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
857 constexpr complex(const complex &o) : re(o.re), im(o.im) {}
858 constexpr complex operator-() const { return complex(-re, -im); }
859 friend constexpr complex operator+(const complex &l, const complex &r) {
860 return complex(l.re + r.re, l.im + r.im);
861 }
862 friend constexpr complex operator-(const complex &l, const complex &r) {
863 return l + -r;
864 }
865 friend constexpr complex operator*(const complex &l, const complex &r) {
866 return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
867 }
868 friend constexpr bool operator==(const complex &l, const complex &r) {
869 return l.re == r.re && l.im == r.im;
870 }
871 constexpr bool operator!=(const complex &r) const {
872 return re != r.re || im != r.im;
873 }
874 constexpr int real() const { return re; }
875 constexpr int imag() const { return im; }
876};
877
878constexpr complex i = complex(0, 1);
879constexpr complex k = (3 + 4*i) * (6 - 4*i);
Richard Smith0765bec2011-12-09 23:00:37 +0000880static_assert(complex(1,0).real() == 1, "");
881static_assert(complex(1,0).imag() == 0, "");
882static_assert(((complex)1).imag() == 0, "");
883static_assert(k.real() == 34, "");
884static_assert(k.imag() == 12, "");
885static_assert(k - 34 == 12*i, "");
886static_assert((complex)1 == complex(1), "");
887static_assert((complex)1 != complex(0, 1), "");
888static_assert(complex(1) == complex(1), "");
889static_assert(complex(1) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000890constexpr complex makeComplex(int re, int im) { return complex(re, im); }
Richard Smith0765bec2011-12-09 23:00:37 +0000891static_assert(makeComplex(1,0) == complex(1), "");
892static_assert(makeComplex(1,0) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000893
894class complex_wrap : public complex {
895public:
896 constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
897 constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
898};
899
Richard Smith0765bec2011-12-09 23:00:37 +0000900static_assert((complex_wrap)1 == complex(1), "");
901static_assert((complex)1 != complex_wrap(0, 1), "");
902static_assert(complex(1) == complex_wrap(1), "");
903static_assert(complex_wrap(1) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000904constexpr complex_wrap makeComplexWrap(int re, int im) {
905 return complex_wrap(re, im);
906}
Richard Smith0765bec2011-12-09 23:00:37 +0000907static_assert(makeComplexWrap(1,0) == complex(1), "");
908static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000909
910}
Eli Friedman79281d12011-12-17 02:24:21 +0000911
912namespace PR11595 {
913 struct A { constexpr bool operator==(int x) { return true; } };
Richard Smithd0b111c2011-12-19 22:01:37 +0000914 struct B { B(); A& x; };
915 static_assert(B().x == 3, ""); // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
916
917 constexpr bool f(int k) {
918 return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}}
919 }
920 constexpr int n = f(1); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'f(1)'}}
Eli Friedman79281d12011-12-17 02:24:21 +0000921}
Richard Smith5894a912011-12-19 22:12:41 +0000922
923namespace ExprWithCleanups {
924 struct A { A(); ~A(); int get(); };
925 constexpr int get(bool FromA) { return FromA ? A().get() : 1; }
926 constexpr int n = get(false);
927}
Richard Smithf2b681b2011-12-21 05:04:46 +0000928
929namespace Volatile {
930
931volatile constexpr int n1 = 0; // expected-note {{here}}
932volatile const int n2 = 0; // expected-note {{here}}
933int n3 = 37; // expected-note {{declared here}}
934
935constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
936constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
937
938struct T { int n; };
939const T t = { 42 }; // expected-note {{declared here}}
940
941constexpr int f(volatile int &&r) {
942 return r; // expected-note {{read of volatile temporary is not allowed in a constant expression}}
943}
944struct S {
945 int k : f(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'f(0)'}}
946 int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
947 int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}}
948};
949
950}