blob: 4b59157ea684d5b7497d06d076c2129db4b64588 [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
11// FIXME: support const T& parameters here.
12//template<typename T> constexpr T id(const T &t) { return t; }
Richard Smith357362d2011-12-13 06:39:58 +000013template<typename T> constexpr T id(T t) { return t; } // expected-note {{here}}
Richard Smithe97cbd72011-11-11 04:05:33 +000014// FIXME: support templates here.
15//template<typename T> constexpr T min(const T &a, const T &b) {
16// return a < b ? a : b;
17//}
18//template<typename T> constexpr T max(const T &a, const T &b) {
19// return a < b ? b : a;
20//}
21constexpr int min(const int &a, const int &b) { return a < b ? a : b; }
22constexpr int max(const int &a, const int &b) { return a < b ? b : a; }
23
24struct MemberZero {
25 constexpr int zero() { return 0; }
26};
27
28namespace DerivedToVBaseCast {
29
30 struct U { int n; };
31 struct V : U { int n; };
32 struct A : virtual V { int n; };
33 struct Aa { int n; };
34 struct B : virtual A, Aa {};
35 struct C : virtual A, Aa {};
36 struct D : B, C {};
37
38 D d;
39 constexpr B *p = &d;
40 constexpr C *q = &d;
Richard Smith0765bec2011-12-09 23:00:37 +000041 static_assert((void*)p != (void*)q, "");
42 static_assert((A*)p == (A*)q, "");
43 static_assert((Aa*)p != (Aa*)q, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000044
45 constexpr B &pp = d;
46 constexpr C &qq = d;
Richard Smith0765bec2011-12-09 23:00:37 +000047 static_assert((void*)&pp != (void*)&qq, "");
48 static_assert(&(A&)pp == &(A&)qq, "");
49 static_assert(&(Aa&)pp != &(Aa&)qq, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000050
51 constexpr V *v = p;
52 constexpr V *w = q;
53 constexpr V *x = (A*)p;
Richard Smith0765bec2011-12-09 23:00:37 +000054 static_assert(v == w, "");
55 static_assert(v == x, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000056
Richard Smith0765bec2011-12-09 23:00:37 +000057 static_assert((U*)&d == p, "");
58 static_assert((U*)&d == q, "");
59 static_assert((U*)&d == v, "");
60 static_assert((U*)&d == w, "");
61 static_assert((U*)&d == x, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000062
63 struct X {};
64 struct Y1 : virtual X {};
65 struct Y2 : X {};
66 struct Z : Y1, Y2 {};
67 Z z;
Richard Smith0765bec2011-12-09 23:00:37 +000068 static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, "");
Richard Smithe97cbd72011-11-11 04:05:33 +000069
70}
71
Richard Smith6804be52011-11-11 08:28:03 +000072namespace ConstCast {
73
74constexpr int n1 = 0;
75constexpr int n2 = const_cast<int&>(n1);
76constexpr int *n3 = const_cast<int*>(&n1);
77constexpr int n4 = *const_cast<int*>(&n1);
78constexpr const int * const *n5 = const_cast<const int* const*>(&n3);
79constexpr int **n6 = const_cast<int**>(&n3);
80constexpr int n7 = **n5;
81constexpr int n8 = **n6;
82
83}
84
Richard Smithe97cbd72011-11-11 04:05:33 +000085namespace TemplateArgumentConversion {
86 template<int n> struct IntParam {};
87
88 using IntParam0 = IntParam<0>;
Richard Smith027bf112011-11-17 22:56:20 +000089 // FIXME: This should be accepted once we implement the new ICE rules.
Richard Smithe97cbd72011-11-11 04:05:33 +000090 using IntParam0 = IntParam<id(0)>; // expected-error {{not an integral constant expression}}
91 using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} expected-error {{not an integral constant expression}}
92}
93
94namespace CaseStatements {
95 void f(int n) {
96 switch (n) {
97 // FIXME: Produce the 'add ()' fixit for this.
Richard Smith357362d2011-12-13 06:39:58 +000098 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 Smith027bf112011-11-17 22:56:20 +000099 // FIXME: This should be accepted once we implement the new ICE rules.
Richard Smith357362d2011-12-13 06:39:58 +0000100 case id(1): // expected-error {{not an integer constant expression}} expected-note {{undefined function}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000101 return;
102 }
103 }
104}
105
106extern int &Recurse1;
107int &Recurse2 = Recurse1, &Recurse1 = Recurse2;
108constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}}
109
110namespace MemberEnum {
111 struct WithMemberEnum {
112 enum E { A = 42 };
113 } wme;
114
Richard Smith0765bec2011-12-09 23:00:37 +0000115 static_assert(wme.A == 42, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000116}
117
118namespace DefaultArguments {
119
120const int z = int();
121constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) {
122 return a + b + *c + d;
123}
124const int four = 4;
125constexpr int eight = 8;
126constexpr const int twentyseven = 27;
Richard Smith0765bec2011-12-09 23:00:37 +0000127static_assert(Sum() == 0, "");
128static_assert(Sum(1) == 1, "");
129static_assert(Sum(1, four) == 5, "");
130static_assert(Sum(1, eight, &twentyseven) == 36, "");
131static_assert(Sum(1, 2, &four, eight) == 15, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000132
133}
134
135namespace Ellipsis {
136
137// Note, values passed through an ellipsis can't actually be used.
138constexpr int F(int a, ...) { return a; }
Richard Smith0765bec2011-12-09 23:00:37 +0000139static_assert(F(0) == 0, "");
140static_assert(F(1, 0) == 1, "");
141static_assert(F(2, "test") == 2, "");
142static_assert(F(3, &F) == 3, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000143int k = 0;
Richard Smith902ca212011-12-14 23:32:26 +0000144static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000145
146}
147
148namespace Recursion {
149 constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; }
Richard Smith0765bec2011-12-09 23:00:37 +0000150 static_assert(fib(11) == 89, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000151
152 constexpr int gcd_inner(int a, int b) {
153 return b == 0 ? a : gcd_inner(b, a % b);
154 }
155 constexpr int gcd(int a, int b) {
156 return gcd_inner(max(a, b), min(a, b));
157 }
158
Richard Smith0765bec2011-12-09 23:00:37 +0000159 static_assert(gcd(1749237, 5628959) == 7, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000160}
161
162namespace FunctionCast {
163 // When folding, we allow functions to be cast to different types. Such
164 // cast functions cannot be called, even if they're constexpr.
165 constexpr int f() { return 1; }
166 typedef double (*DoubleFn)();
167 typedef int (*IntFn)();
Richard Smith6d6ecc32011-12-12 12:46:16 +0000168 int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{extension}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000169 int b[(int)IntFn(f)()]; // ok
170}
171
172namespace StaticMemberFunction {
173 struct S {
174 static constexpr int k = 42;
175 static constexpr int f(int n) { return n * k + 2; }
176 } s;
177
178 constexpr int n = s.f(19);
Richard Smith0765bec2011-12-09 23:00:37 +0000179 static_assert(S::f(19) == 800, "");
180 static_assert(s.f(19) == 800, "");
181 static_assert(n == 800, "");
Richard Smithce40ad62011-11-12 22:28:03 +0000182
183 constexpr int (*sf1)(int) = &S::f;
184 constexpr int (*sf2)(int) = &s.f;
185 constexpr const int *sk = &s.k;
Richard Smithe97cbd72011-11-11 04:05:33 +0000186}
187
188namespace ParameterScopes {
189
190 const int k = 42;
191 constexpr const int &ObscureTheTruth(const int &a) { return a; }
192 constexpr const int &MaybeReturnJunk(bool b, const int a) {
193 return ObscureTheTruth(b ? a : k);
194 }
Richard Smith0765bec2011-12-09 23:00:37 +0000195 static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
Richard Smithe97cbd72011-11-11 04:05:33 +0000196 constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}}
197
198 constexpr const int MaybeReturnNonstaticRef(bool b, const int a) {
199 // If ObscureTheTruth returns a reference to 'a', the result is not a
200 // constant expression even though 'a' is still in scope.
201 return ObscureTheTruth(b ? a : k);
202 }
Richard Smith0765bec2011-12-09 23:00:37 +0000203 static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
Richard Smithe97cbd72011-11-11 04:05:33 +0000204 constexpr int b = MaybeReturnNonstaticRef(true, 0); // expected-error {{constant expression}}
205
206 constexpr int InternalReturnJunk(int n) {
207 // FIXME: We should reject this: it never produces a constant expression.
208 return MaybeReturnJunk(true, n);
209 }
210 constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}}
211
212 constexpr int LToR(int &n) { return n; }
213 constexpr int GrabCallersArgument(bool which, int a, int b) {
214 return LToR(which ? b : a);
215 }
Richard Smith0765bec2011-12-09 23:00:37 +0000216 static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
217 static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000218
219}
220
221namespace Pointers {
222
223 constexpr int f(int n, const int *a, const int *b, const int *c) {
224 return n == 0 ? 0 : *a + f(n-1, b, c, a);
225 }
226
227 const int x = 1, y = 10, z = 100;
Richard Smith0765bec2011-12-09 23:00:37 +0000228 static_assert(f(23, &x, &y, &z) == 788, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000229
230 constexpr int g(int n, int a, int b, int c) {
231 return f(n, &a, &b, &c);
232 }
Richard Smith0765bec2011-12-09 23:00:37 +0000233 static_assert(g(23, x, y, z) == 788, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000234
235}
236
237namespace FunctionPointers {
238
239 constexpr int Double(int n) { return 2 * n; }
240 constexpr int Triple(int n) { return 3 * n; }
241 constexpr int Twice(int (*F)(int), int n) { return F(F(n)); }
242 constexpr int Quadruple(int n) { return Twice(Double, n); }
243 constexpr auto Select(int n) -> int (*)(int) {
244 return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0;
245 }
246 constexpr int Apply(int (*F)(int), int n) { return F(n); }
247
Richard Smith0765bec2011-12-09 23:00:37 +0000248 static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000249
250 constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}}
251
252}
253
254namespace PointerComparison {
255
256int x, y;
Richard Smith0765bec2011-12-09 23:00:37 +0000257static_assert(&x == &y, "false"); // expected-error {{false}}
258static_assert(&x != &y, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000259constexpr bool g1 = &x == &y;
260constexpr bool g2 = &x != &y;
261constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}}
262constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}}
263constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}}
264constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}}
265
266struct S { int x, y; } s;
Richard Smith0765bec2011-12-09 23:00:37 +0000267static_assert(&s.x == &s.y, "false"); // expected-error {{false}}
268static_assert(&s.x != &s.y, "");
269static_assert(&s.x <= &s.y, "");
270static_assert(&s.x >= &s.y, "false"); // expected-error {{false}}
271static_assert(&s.x < &s.y, "");
272static_assert(&s.x > &s.y, "false"); // expected-error {{false}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000273
Richard Smith0765bec2011-12-09 23:00:37 +0000274static_assert(0 == &y, "false"); // expected-error {{false}}
275static_assert(0 != &y, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000276constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}}
277constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}}
278constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}}
279constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}}
280
Richard Smith0765bec2011-12-09 23:00:37 +0000281static_assert(&x == 0, "false"); // expected-error {{false}}
282static_assert(&x != 0, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000283constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}}
284constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}}
285constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}}
286constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}}
287
Richard Smith0765bec2011-12-09 23:00:37 +0000288static_assert(&x == &x, "");
289static_assert(&x != &x, "false"); // expected-error {{false}}
290static_assert(&x <= &x, "");
291static_assert(&x >= &x, "");
292static_assert(&x < &x, "false"); // expected-error {{false}}
293static_assert(&x > &x, "false"); // expected-error {{false}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000294
295constexpr S* sptr = &s;
296// FIXME: This is not a constant expression; check we reject this and move this
297// test elsewhere.
298constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr);
299
Richard Smith6d6ecc32011-12-12 12:46:16 +0000300struct Str {
301 // FIXME: In C++ mode, we should say 'integral' not 'integer'
302 int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \
303 expected-warning {{not integer constant expression}} \
Richard Smithff07af12011-12-12 19:10:03 +0000304 expected-note {{dynamic_cast is not allowed in a constant expression}}
Richard Smith6d6ecc32011-12-12 12:46:16 +0000305 int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \
306 expected-warning {{not integer constant expression}} \
Richard Smithff07af12011-12-12 19:10:03 +0000307 expected-note {{reinterpret_cast is not allowed in a constant expression}}
Richard Smith6d6ecc32011-12-12 12:46:16 +0000308 int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \
309 expected-warning {{not integer constant expression}} \
Richard Smithdcf1b2c2011-12-12 19:33:27 +0000310 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 +0000311 int d : (S*)(42) == (S*)(42); // \
312 expected-warning {{not integer constant expression}} \
Richard Smithdcf1b2c2011-12-12 19:33:27 +0000313 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 +0000314 int e : (Str*)(sptr) == (Str*)(sptr); // \
315 expected-warning {{not integer constant expression}} \
Richard Smithdcf1b2c2011-12-12 19:33:27 +0000316 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 +0000317 int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \
318 expected-warning {{not integer constant expression}} \
Richard Smithdcf1b2c2011-12-12 19:33:27 +0000319 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 +0000320 int g : (S*)(void*)(sptr) == sptr; // \
321 expected-warning {{not integer constant expression}} \
Richard Smithff07af12011-12-12 19:10:03 +0000322 expected-note {{cast from 'void *' is not allowed in a constant expression}}
Richard Smith6d6ecc32011-12-12 12:46:16 +0000323};
324
Richard Smithe97cbd72011-11-11 04:05:33 +0000325extern char externalvar[];
326// FIXME: This is not a constant expression; check we reject this and move this
327// test elsewhere.
Richard Smith0765bec2011-12-09 23:00:37 +0000328constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000329constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}}
Richard Smith0765bec2011-12-09 23:00:37 +0000330static_assert(0 != "foo", "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000331
332}
333
334namespace MaterializeTemporary {
335
336constexpr int f(const int &r) { return r; }
337constexpr int n = f(1);
338
339constexpr bool same(const int &a, const int &b) { return &a == &b; }
340constexpr bool sameTemporary(const int &n) { return same(n, n); }
341
Richard Smith0765bec2011-12-09 23:00:37 +0000342static_assert(n, "");
343static_assert(!same(4, 4), "");
344static_assert(same(n, n), "");
345static_assert(sameTemporary(9), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000346
347}
348
349constexpr int strcmp_ce(const char *p, const char *q) {
350 return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1);
351}
352
353namespace StringLiteral {
354
355// FIXME: Refactor this once we support constexpr templates.
356constexpr int MangleChars(const char *p) {
357 return *p + 3 * (*p ? MangleChars(p+1) : 0);
358}
359constexpr int MangleChars(const char16_t *p) {
360 return *p + 3 * (*p ? MangleChars(p+1) : 0);
361}
362constexpr int MangleChars(const char32_t *p) {
363 return *p + 3 * (*p ? MangleChars(p+1) : 0);
364}
365
Richard Smith0765bec2011-12-09 23:00:37 +0000366static_assert(MangleChars("constexpr!") == 1768383, "");
367static_assert(MangleChars(u"constexpr!") == 1768383, "");
368static_assert(MangleChars(U"constexpr!") == 1768383, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000369
370constexpr char c0 = "nought index"[0];
371constexpr char c1 = "nice index"[10];
Matt Beaumont-Gay63be1912011-11-24 00:27:38 +0000372constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}}
373constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000374constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}}
375
376constexpr const char *p = "test" + 2;
Richard Smith0765bec2011-12-09 23:00:37 +0000377static_assert(*p == 's', "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000378
379constexpr const char *max_iter(const char *a, const char *b) {
380 return *a < *b ? b : a;
381}
382constexpr const char *max_element(const char *a, const char *b) {
383 return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b));
384}
385
386constexpr const char *begin(const char (&arr)[45]) { return arr; }
387constexpr const char *end(const char (&arr)[45]) { return arr + 45; }
388
389constexpr char str[] = "the quick brown fox jumped over the lazy dog";
390constexpr const char *max = max_element(begin(str), end(str));
Richard Smith0765bec2011-12-09 23:00:37 +0000391static_assert(*max == 'z', "");
392static_assert(max == str + 38, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000393
Richard Smith0765bec2011-12-09 23:00:37 +0000394static_assert(strcmp_ce("hello world", "hello world") == 0, "");
395static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
396static_assert(strcmp_ce("constexpr", "test") < 0, "");
397static_assert(strcmp_ce("", " ") < 0, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000398
399}
400
401namespace Array {
402
403// FIXME: Use templates for these once we support constexpr templates.
404constexpr int Sum(const int *begin, const int *end) {
405 return begin == end ? 0 : *begin + Sum(begin+1, end);
406}
407constexpr const int *begin(const int (&xs)[5]) { return xs; }
408constexpr const int *end(const int (&xs)[5]) { return xs + 5; }
409
410constexpr int xs[] = { 1, 2, 3, 4, 5 };
411constexpr int ys[] = { 5, 4, 3, 2, 1 };
412constexpr int sum_xs = Sum(begin(xs), end(xs));
Richard Smith0765bec2011-12-09 23:00:37 +0000413static_assert(sum_xs == 15, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000414
415constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n,
416 const int *xs, const int *ys, int c) {
Richard Smith902ca212011-12-14 23:32:26 +0000417 return n ? F(
418 *xs, // expected-note {{subexpression not valid}}
419 *ys, ZipFoldR(F, n-1, xs+1, ys+1, c)) : c;
Richard Smithe97cbd72011-11-11 04:05:33 +0000420}
421constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
422constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
Richard Smith0765bec2011-12-09 23:00:37 +0000423static_assert(InnerProduct == 35, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000424
425constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
426constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
Richard Smith0765bec2011-12-09 23:00:37 +0000427static_assert(DiffProd == 8, "");
428static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // expected-error {{constant expression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000429
430constexpr const int *p = xs + 3;
431constexpr int xs4 = p[1]; // ok
432constexpr int xs5 = p[2]; // expected-error {{constant expression}}
433constexpr int xs0 = p[-3]; // ok
434constexpr int xs_1 = p[-4]; // expected-error {{constant expression}}
435
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 Smith902ca212011-12-14 23:32:26 +0000439static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
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 Smith6d6ecc32011-12-12 12:46:16 +0000444constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{extension}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000445constexpr int SumNonzero(const int *p) {
446 return *p + (*p ? SumNonzero(p+1) : 0);
447}
448constexpr int CountZero(const int *p, const int *q) {
449 return p == q ? 0 : (*p == 0) + CountZero(p+1, q);
450}
Richard Smith0765bec2011-12-09 23:00:37 +0000451static_assert(SumNonzero(arr) == 6, "");
452static_assert(CountZero(arr, arr + 40) == 36, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000453
Richard Smith027bf112011-11-17 22:56:20 +0000454struct ArrayElem {
455 constexpr ArrayElem() : n(0) {}
456 int n;
457 constexpr int f() { return n; }
458};
459struct ArrayRVal {
460 constexpr ArrayRVal() {}
461 ArrayElem elems[10];
462};
Richard Smith0765bec2011-12-09 23:00:37 +0000463static_assert(ArrayRVal().elems[3].f() == 0, "");
Richard Smith027bf112011-11-17 22:56:20 +0000464
Richard Smithe97cbd72011-11-11 04:05:33 +0000465}
466
467namespace DependentValues {
468
469struct I { int n; typedef I V[10]; };
470I::V x, y;
471template<bool B> struct S {
472 int k;
473 void f() {
474 I::V &cells = B ? x : y;
475 I &i = cells[k];
476 switch (i.n) {}
477 }
478};
479
480}
481
482namespace Class {
483
484struct A { constexpr A(int a, int b) : k(a + b) {} int k; };
485constexpr int fn(const A &a) { return a.k; }
Richard Smith0765bec2011-12-09 23:00:37 +0000486static_assert(fn(A(4,5)) == 9, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000487
488struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}}
489struct C {
490 constexpr C(C *this_) : m(42), n(this_->m) {} // ok
491 int m, n;
492};
493struct D {
494 C c;
495 constexpr D() : c(&c) {}
496};
Richard Smith0765bec2011-12-09 23:00:37 +0000497static_assert(D().c.n == 42, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000498
499struct E {
500 constexpr E() : p(&p) {}
501 void *p;
502};
503constexpr const E &e1 = E(); // expected-error {{constant expression}}
504// This is a constant expression if we elide the copy constructor call, and
505// is not a constant expression if we don't! But we do, so it is.
506// FIXME: The move constructor is not currently implicitly defined as constexpr.
507// We notice this when evaluating an expression which uses it, but not when
508// checking its initializer.
509constexpr E e2 = E(); // unexpected-error {{constant expression}}
Richard Smith902ca212011-12-14 23:32:26 +0000510static_assert(e2.p == &e2.p, ""); // unexpected-error {{constant expression}} unexpected-note {{subexpression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000511// FIXME: We don't pass through the fact that 'this' is ::e3 when checking the
512// initializer of this declaration.
513constexpr E e3; // unexpected-error {{constant expression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000514static_assert(e3.p == &e3.p, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000515
516extern const class F f;
517struct F {
518 constexpr F() : p(&f.p) {}
519 const void *p;
520};
521constexpr F f = F();
522
523struct G {
524 struct T {
525 constexpr T(T *p) : u1(), u2(p) {}
526 union U1 {
527 constexpr U1() {}
528 int a, b = 42;
529 } u1;
530 union U2 {
531 constexpr U2(T *p) : c(p->u1.b) {}
532 int c, d;
533 } u2;
534 } t;
535 constexpr G() : t(&t) {}
536} constexpr g;
537
Richard Smith902ca212011-12-14 23:32:26 +0000538static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000539static_assert(g.t.u1.b == 42, "");
540static_assert(g.t.u2.c == 42, "");
Richard Smith902ca212011-12-14 23:32:26 +0000541static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000542
543struct S {
544 int a, b;
545 const S *p;
546 double d;
547 const char *q;
548
549 constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {}
550};
551
552S global(43, &global);
553
Richard Smith0765bec2011-12-09 23:00:37 +0000554static_assert(S(15, &global).b == 15, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000555
556constexpr bool CheckS(const S &s) {
557 return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l';
558}
Richard Smith0765bec2011-12-09 23:00:37 +0000559static_assert(CheckS(S(27, &global)), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000560
561struct Arr {
562 char arr[3];
563 constexpr Arr() : arr{'x', 'y', 'z'} {}
564};
565constexpr int hash(Arr &&a) {
566 return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000;
567}
568constexpr int k = hash(Arr());
Richard Smith0765bec2011-12-09 23:00:37 +0000569static_assert(k == 0x007a7978, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000570
571
572struct AggregateInit {
573 const char &c;
574 int n;
575 double d;
576 int arr[5];
577 void *p;
578};
579
580constexpr AggregateInit agg1 = { "hello"[0] };
581
Richard Smith0765bec2011-12-09 23:00:37 +0000582static_assert(strcmp_ce(&agg1.c, "hello") == 0, "");
583static_assert(agg1.n == 0, "");
584static_assert(agg1.d == 0.0, "");
Richard Smith902ca212011-12-14 23:32:26 +0000585static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000586static_assert(agg1.arr[0] == 0, "");
587static_assert(agg1.arr[4] == 0, "");
Richard Smith902ca212011-12-14 23:32:26 +0000588static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000589static_assert(agg1.p == nullptr, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000590
591namespace SimpleDerivedClass {
592
593struct B {
594 constexpr B(int n) : a(n) {}
595 int a;
596};
597struct D : B {
598 constexpr D(int n) : B(n) {}
599};
600constexpr D d(3);
Richard Smith0765bec2011-12-09 23:00:37 +0000601static_assert(d.a == 3, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000602
603}
604
Richard Smith027bf112011-11-17 22:56:20 +0000605struct Bottom { constexpr Bottom() {} };
606struct Base : Bottom {
Richard Smithe97cbd72011-11-11 04:05:33 +0000607 constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
608 int a;
609 const char *b;
610};
Richard Smith027bf112011-11-17 22:56:20 +0000611struct Base2 : Bottom {
Richard Smithe97cbd72011-11-11 04:05:33 +0000612 constexpr Base2(const int &r) : r(r) {}
613 int q = 123;
614 // FIXME: When we track the global for which we are computing the initializer,
615 // use a reference here.
616 //const int &r;
617 int r;
618};
619struct Derived : Base, Base2 {
620 constexpr Derived() : Base(76), Base2(a) {}
621 int c = r + b[1];
622};
623
624constexpr bool operator==(const Base &a, const Base &b) {
625 return a.a == b.a && strcmp_ce(a.b, b.b) == 0;
626}
627
628constexpr Base base;
629constexpr Base base2(76);
630constexpr Derived derived;
Richard Smith0765bec2011-12-09 23:00:37 +0000631static_assert(derived.a == 76, "");
632static_assert(derived.b[2] == 's', "");
633static_assert(derived.c == 76 + 'e', "");
634static_assert(derived.q == 123, "");
635static_assert(derived.r == 76, "");
636static_assert(&derived.r == &derived.a, ""); // expected-error {{}}
Richard Smithe97cbd72011-11-11 04:05:33 +0000637
Richard Smith0765bec2011-12-09 23:00:37 +0000638static_assert(!(derived == base), "");
639static_assert(derived == base2, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000640
Richard Smith027bf112011-11-17 22:56:20 +0000641constexpr Bottom &bot1 = (Base&)derived;
642constexpr Bottom &bot2 = (Base2&)derived;
Richard Smith0765bec2011-12-09 23:00:37 +0000643static_assert(&bot1 != &bot2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000644
645constexpr Bottom *pb1 = (Base*)&derived;
646constexpr Bottom *pb2 = (Base2*)&derived;
Richard Smith0765bec2011-12-09 23:00:37 +0000647static_assert(pb1 != pb2, "");
648static_assert(pb1 == &bot1, "");
649static_assert(pb2 == &bot2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000650
651constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}}
652constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}}
653constexpr Base2 &ok2 = (Base2&)bot2;
Richard Smith0765bec2011-12-09 23:00:37 +0000654static_assert(&ok2 == &derived, "");
Richard Smith027bf112011-11-17 22:56:20 +0000655
656constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}}
657constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}}
658constexpr Base2 *pok2 = (Base2*)pb2;
Richard Smith0765bec2011-12-09 23:00:37 +0000659static_assert(pok2 == &derived, "");
660static_assert(&ok2 == pok2, "");
661static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, "");
662static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000663
664constexpr Base *nullB = 42 - 6 * 7;
Richard Smith0765bec2011-12-09 23:00:37 +0000665static_assert((Bottom*)nullB == 0, "");
666static_assert((Derived*)nullB == 0, "");
667static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, "");
Richard Smith027bf112011-11-17 22:56:20 +0000668
669}
670
671namespace Temporaries {
672
673struct S {
674 constexpr S() {}
675 constexpr int f();
676};
677struct T : S {
678 constexpr T(int n) : S(), n(n) {}
679 int n;
680};
681constexpr int S::f() {
682 // 'this' must be the postfix-expression in a class member access expression,
683 // so we can't just use
684 // return static_cast<T*>(this)->n;
685 return this->*(int(S::*))&T::n;
686}
687// The T temporary is implicitly cast to an S subobject, but we can recover the
688// T full-object via a base-to-derived cast, or a derived-to-base-casted member
689// pointer.
Richard Smith0765bec2011-12-09 23:00:37 +0000690static_assert(T(3).f() == 3, "");
Richard Smith027bf112011-11-17 22:56:20 +0000691
692constexpr int f(const S &s) {
693 return static_cast<const T&>(s).n;
694}
695constexpr int n = f(T(5));
Richard Smith0765bec2011-12-09 23:00:37 +0000696static_assert(f(T(5)) == 5, "");
Richard Smith027bf112011-11-17 22:56:20 +0000697
Richard Smithe97cbd72011-11-11 04:05:33 +0000698}
699
700namespace Union {
701
702union U {
703 int a;
704 int b;
705};
706
Richard Smith6d6ecc32011-12-12 12:46:16 +0000707constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{extension}}
Richard Smith0765bec2011-12-09 23:00:37 +0000708static_assert(u[0].a == 0, "");
709static_assert(u[0].b, ""); // expected-error {{constant expression}}
710static_assert(u[1].b == 1, "");
Richard Smith902ca212011-12-14 23:32:26 +0000711static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
712static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000713static_assert((&(u[1]) + 1 + 1)->b == 3, "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000714
715}
716
Richard Smith027bf112011-11-17 22:56:20 +0000717namespace MemberPointer {
718 struct A {
719 constexpr A(int n) : n(n) {}
720 int n;
721 constexpr int f() { return n + 3; }
722 };
723 constexpr A a(7);
Richard Smith0765bec2011-12-09 23:00:37 +0000724 static_assert(A(5).*&A::n == 5, "");
725 static_assert((&a)->*&A::n == 7, "");
726 static_assert((A(8).*&A::f)() == 11, "");
727 static_assert(((&a)->*&A::f)() == 10, "");
Richard Smith027bf112011-11-17 22:56:20 +0000728
729 struct B : A {
730 constexpr B(int n, int m) : A(n), m(m) {}
731 int m;
732 constexpr int g() { return n + m + 1; }
733 };
734 constexpr B b(9, 13);
Richard Smith0765bec2011-12-09 23:00:37 +0000735 static_assert(B(4, 11).*&A::n == 4, "");
736 static_assert(B(4, 11).*&B::m == 11, "");
737 static_assert(B(4, 11).*(int(A::*))&B::m == 11, "");
738 static_assert((&b)->*&A::n == 9, "");
739 static_assert((&b)->*&B::m == 13, "");
740 static_assert((&b)->*(int(A::*))&B::m == 13, "");
741 static_assert((B(4, 11).*&A::f)() == 7, "");
742 static_assert((B(4, 11).*&B::g)() == 16, "");
743 static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, "");
744 static_assert(((&b)->*&A::f)() == 12, "");
745 static_assert(((&b)->*&B::g)() == 23, "");
746 static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, "");
Richard Smith027bf112011-11-17 22:56:20 +0000747
748 struct S {
749 constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) :
750 m(m), n(n), pf(pf), pn(pn) {}
751 constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {}
752
753 constexpr int f() { return this->*pn; }
754 virtual int g() const;
755
756 int m, n;
757 int (S::*pf)() const;
758 int S::*pn;
759 };
760
761 constexpr int S::*pm = &S::m;
762 constexpr int S::*pn = &S::n;
763 constexpr int (S::*pf)() const = &S::f;
764 constexpr int (S::*pg)() const = &S::g;
765
766 constexpr S s(2, 5, &S::f, &S::m);
767
Richard Smith0765bec2011-12-09 23:00:37 +0000768 static_assert((s.*&S::f)() == 2, "");
769 static_assert((s.*s.pf)() == 2, "");
Richard Smith027bf112011-11-17 22:56:20 +0000770
771 template<int n> struct T : T<n-1> {};
772 template<> struct T<0> { int n; };
773 template<> struct T<30> : T<29> { int m; };
774
775 T<17> t17;
776 T<30> t30;
777
778 constexpr int (T<10>::*deepn) = &T<0>::n;
Richard Smith0765bec2011-12-09 23:00:37 +0000779 static_assert(&(t17.*deepn) == &t17.n, "");
Richard Smith027bf112011-11-17 22:56:20 +0000780
781 constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m;
782 constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000783 static_assert(&(t30.*deepm) == &t30.m, "");
Richard Smith027bf112011-11-17 22:56:20 +0000784
785 constexpr T<5> *p17_5 = &t17;
786 constexpr T<13> *p17_13 = (T<13>*)p17_5;
787 constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}}
Richard Smith0765bec2011-12-09 23:00:37 +0000788 static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, "");
789 static_assert(&(p17_13->*deepn) == &t17.n, "");
Richard Smith027bf112011-11-17 22:56:20 +0000790 constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}}
791
792 constexpr T<5> *p30_5 = &t30;
793 constexpr T<23> *p30_23 = (T<23>*)p30_5;
794 constexpr T<13> *p30_13 = p30_23;
Richard Smith0765bec2011-12-09 23:00:37 +0000795 static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, "");
796 static_assert(&(p30_13->*deepn) == &t30.n, "");
797 static_assert(&(p30_23->*deepn) == &t30.n, "");
798 static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, "");
799 static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, "");
800 static_assert(&(p30_23->*deepm) == &t30.m, "");
Richard Smith027bf112011-11-17 22:56:20 +0000801}
802
803namespace ArrayBaseDerived {
804
805 struct Base {
806 constexpr Base() {}
807 int n = 0;
808 };
809 struct Derived : Base {
810 constexpr Derived() {}
811 constexpr const int *f() { return &n; }
812 };
813
814 constexpr Derived a[10];
815 constexpr Derived *pd3 = const_cast<Derived*>(&a[3]);
816 constexpr Base *pb3 = const_cast<Derived*>(&a[3]);
Richard Smith0765bec2011-12-09 23:00:37 +0000817 static_assert(pb3 == pd3, "");
Richard Smith027bf112011-11-17 22:56:20 +0000818
819 // pb3 does not point to an array element.
820 constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer.
821 constexpr int pb4n = pb4->n; // expected-error {{constant expression}}
822 constexpr Base *err_pb5 = pb3 + 2; // FIXME: reject this.
823 constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}}
824 constexpr Base *err_pb2 = pb3 - 1; // FIXME: reject this.
825 constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}}
826 constexpr Base *pb3a = pb4 - 1;
827
828 // pb4 does not point to a Derived.
829 constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}}
830 constexpr Derived *pd3a = (Derived*)pb3a;
831 constexpr int pd3n = pd3a->n;
832
833 // pd3a still points to the Derived array.
834 constexpr Derived *pd6 = pd3a + 3;
Richard Smith0765bec2011-12-09 23:00:37 +0000835 static_assert(pd6 == &a[6], "");
Richard Smith027bf112011-11-17 22:56:20 +0000836 constexpr Derived *pd9 = pd6 + 3;
837 constexpr Derived *pd10 = pd6 + 4;
838 constexpr int pd9n = pd9->n; // ok
839 constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}}
840 constexpr int pd0n = pd10[-10].n;
841 constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}}
842
843 constexpr Base *pb9 = pd9;
844 constexpr const int *(Base::*pfb)() const =
845 static_cast<const int *(Base::*)() const>(&Derived::f);
Richard Smith0765bec2011-12-09 23:00:37 +0000846 static_assert((pb9->*pfb)() == &a[9].n, "");
Richard Smith027bf112011-11-17 22:56:20 +0000847}
848
Richard Smithe97cbd72011-11-11 04:05:33 +0000849namespace Complex {
850
851class complex {
852 int re, im;
853public:
854 constexpr complex(int re = 0, int im = 0) : re(re), im(im) {}
855 constexpr complex(const complex &o) : re(o.re), im(o.im) {}
856 constexpr complex operator-() const { return complex(-re, -im); }
857 friend constexpr complex operator+(const complex &l, const complex &r) {
858 return complex(l.re + r.re, l.im + r.im);
859 }
860 friend constexpr complex operator-(const complex &l, const complex &r) {
861 return l + -r;
862 }
863 friend constexpr complex operator*(const complex &l, const complex &r) {
864 return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re);
865 }
866 friend constexpr bool operator==(const complex &l, const complex &r) {
867 return l.re == r.re && l.im == r.im;
868 }
869 constexpr bool operator!=(const complex &r) const {
870 return re != r.re || im != r.im;
871 }
872 constexpr int real() const { return re; }
873 constexpr int imag() const { return im; }
874};
875
876constexpr complex i = complex(0, 1);
877constexpr complex k = (3 + 4*i) * (6 - 4*i);
Richard Smith0765bec2011-12-09 23:00:37 +0000878static_assert(complex(1,0).real() == 1, "");
879static_assert(complex(1,0).imag() == 0, "");
880static_assert(((complex)1).imag() == 0, "");
881static_assert(k.real() == 34, "");
882static_assert(k.imag() == 12, "");
883static_assert(k - 34 == 12*i, "");
884static_assert((complex)1 == complex(1), "");
885static_assert((complex)1 != complex(0, 1), "");
886static_assert(complex(1) == complex(1), "");
887static_assert(complex(1) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000888constexpr complex makeComplex(int re, int im) { return complex(re, im); }
Richard Smith0765bec2011-12-09 23:00:37 +0000889static_assert(makeComplex(1,0) == complex(1), "");
890static_assert(makeComplex(1,0) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000891
892class complex_wrap : public complex {
893public:
894 constexpr complex_wrap(int re, int im = 0) : complex(re, im) {}
895 constexpr complex_wrap(const complex_wrap &o) : complex(o) {}
896};
897
Richard Smith0765bec2011-12-09 23:00:37 +0000898static_assert((complex_wrap)1 == complex(1), "");
899static_assert((complex)1 != complex_wrap(0, 1), "");
900static_assert(complex(1) == complex_wrap(1), "");
901static_assert(complex_wrap(1) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000902constexpr complex_wrap makeComplexWrap(int re, int im) {
903 return complex_wrap(re, im);
904}
Richard Smith0765bec2011-12-09 23:00:37 +0000905static_assert(makeComplexWrap(1,0) == complex(1), "");
906static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
Richard Smithe97cbd72011-11-11 04:05:33 +0000907
908}