constant-expression-cxx11.cpp revision c216a01c96d83bd9a90e214af64913e93d39aacc
1// RUN: %clang_cc1 -triple i686-linux -fsyntax-only -verify -std=c++11 -pedantic %s -Wno-comment 2 3namespace StaticAssertFoldTest { 4 5int x; 6static_assert(++x, "test"); // expected-error {{not an integral constant expression}} 7static_assert(false, "test"); // expected-error {{test}} 8 9} 10 11// FIXME: support const T& parameters here. 12//template<typename T> constexpr T id(const T &t) { return t; } 13template<typename T> constexpr T id(T t) { return t; } 14// FIXME: support templates here. 15//template<typename T> constexpr T min(const T &a, const T &b) { 16// return a < b ? a : b; 17//} 18//template<typename T> constexpr T max(const T &a, const T &b) { 19// return a < b ? b : a; 20//} 21constexpr int min(const int &a, const int &b) { return a < b ? a : b; } 22constexpr int max(const int &a, const int &b) { return a < b ? b : a; } 23 24struct MemberZero { 25 constexpr int zero() { return 0; } 26}; 27 28namespace DerivedToVBaseCast { 29 30 struct U { int n; }; 31 struct V : U { int n; }; 32 struct A : virtual V { int n; }; 33 struct Aa { int n; }; 34 struct B : virtual A, Aa {}; 35 struct C : virtual A, Aa {}; 36 struct D : B, C {}; 37 38 D d; 39 constexpr B *p = &d; 40 constexpr C *q = &d; 41 static_assert((void*)p != (void*)q, ""); 42 static_assert((A*)p == (A*)q, ""); 43 static_assert((Aa*)p != (Aa*)q, ""); 44 45 constexpr B &pp = d; 46 constexpr C &qq = d; 47 static_assert((void*)&pp != (void*)&qq, ""); 48 static_assert(&(A&)pp == &(A&)qq, ""); 49 static_assert(&(Aa&)pp != &(Aa&)qq, ""); 50 51 constexpr V *v = p; 52 constexpr V *w = q; 53 constexpr V *x = (A*)p; 54 static_assert(v == w, ""); 55 static_assert(v == x, ""); 56 57 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, ""); 62 63 struct X {}; 64 struct Y1 : virtual X {}; 65 struct Y2 : X {}; 66 struct Z : Y1, Y2 {}; 67 Z z; 68 static_assert((X*)(Y1*)&z != (X*)(Y2*)&z, ""); 69 70} 71 72namespace 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 85namespace TemplateArgumentConversion { 86 template<int n> struct IntParam {}; 87 88 using IntParam0 = IntParam<0>; 89 // FIXME: This should be accepted once we implement the new ICE rules. 90 using IntParam0 = IntParam<id(0)>; // expected-error {{not an integral constant expression}} 91 using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} expected-error {{not an integral constant expression}} 92} 93 94namespace CaseStatements { 95 void f(int n) { 96 switch (n) { 97 // FIXME: Produce the 'add ()' fixit for this. 98 case MemberZero().zero: // desired-error {{did you mean to call it with no arguments?}} expected-error {{not an integer constant expression}} 99 // FIXME: This should be accepted once we implement the new ICE rules. 100 case id(1): // expected-error {{not an integer constant expression}} 101 return; 102 } 103 } 104} 105 106extern int &Recurse1; 107int &Recurse2 = Recurse1, &Recurse1 = Recurse2; 108constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} 109 110namespace MemberEnum { 111 struct WithMemberEnum { 112 enum E { A = 42 }; 113 } wme; 114 115 static_assert(wme.A == 42, ""); 116} 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; 127static_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, ""); 132 133} 134 135namespace Ellipsis { 136 137// Note, values passed through an ellipsis can't actually be used. 138constexpr int F(int a, ...) { return a; } 139static_assert(F(0) == 0, ""); 140static_assert(F(1, 0) == 1, ""); 141static_assert(F(2, "test") == 2, ""); 142static_assert(F(3, &F) == 3, ""); 143int k = 0; 144static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} 145 146} 147 148namespace Recursion { 149 constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; } 150 static_assert(fib(11) == 89, ""); 151 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 159 static_assert(gcd(1749237, 5628959) == 7, ""); 160} 161 162namespace FunctionCast { 163 // When folding, we allow functions to be cast to different types. Such 164 // cast functions cannot be called, even if they're constexpr. 165 constexpr int f() { return 1; } 166 typedef double (*DoubleFn)(); 167 typedef int (*IntFn)(); 168 int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{extension}} 169 int b[(int)IntFn(f)()]; // ok 170} 171 172namespace StaticMemberFunction { 173 struct S { 174 static constexpr int k = 42; 175 static constexpr int f(int n) { return n * k + 2; } 176 } s; 177 178 constexpr int n = s.f(19); 179 static_assert(S::f(19) == 800, ""); 180 static_assert(s.f(19) == 800, ""); 181 static_assert(n == 800, ""); 182 183 constexpr int (*sf1)(int) = &S::f; 184 constexpr int (*sf2)(int) = &s.f; 185 constexpr const int *sk = &s.k; 186} 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 } 195 static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok 196 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 } 203 static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok 204 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 } 216 static_assert(GrabCallersArgument(false, 1, 2) == 1, ""); 217 static_assert(GrabCallersArgument(true, 4, 8) == 8, ""); 218 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; 228 static_assert(f(23, &x, &y, &z) == 788, ""); 229 230 constexpr int g(int n, int a, int b, int c) { 231 return f(n, &a, &b, &c); 232 } 233 static_assert(g(23, x, y, z) == 788, ""); 234 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 248 static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, ""); 249 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; 257static_assert(&x == &y, "false"); // expected-error {{false}} 258static_assert(&x != &y, ""); 259constexpr 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; 267static_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}} 273 274static_assert(0 == &y, "false"); // expected-error {{false}} 275static_assert(0 != &y, ""); 276constexpr 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 281static_assert(&x == 0, "false"); // expected-error {{false}} 282static_assert(&x != 0, ""); 283constexpr 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 288static_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}} 294 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 300struct 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}} \ 304 expected-note {{dynamic_cast not allowed in a constant expression}} 305 int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \ 306 expected-warning {{not integer constant expression}} \ 307 expected-note {{reinterpret_cast not allowed in a constant expression}} 308 int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \ 309 expected-warning {{not integer constant expression}} \ 310 expected-note {{reinterpreting cast not allowed in a constant expression}} 311 int d : (S*)(42) == (S*)(42); // \ 312 expected-warning {{not integer constant expression}} \ 313 expected-note {{reinterpreting cast not allowed in a constant expression}} 314 int e : (Str*)(sptr) == (Str*)(sptr); // \ 315 expected-warning {{not integer constant expression}} \ 316 expected-note {{reinterpreting cast not allowed in a constant expression}} 317 int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \ 318 expected-warning {{not integer constant expression}} \ 319 expected-note {{reinterpreting cast not allowed in a constant expression}} 320 int g : (S*)(void*)(sptr) == sptr; // \ 321 expected-warning {{not integer constant expression}} \ 322 expected-note {{reinterpreting cast not allowed in a constant expression}} 323}; 324 325extern char externalvar[]; 326// FIXME: This is not a constant expression; check we reject this and move this 327// test elsewhere. 328constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}} 329constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}} 330static_assert(0 != "foo", ""); 331 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 342static_assert(n, ""); 343static_assert(!same(4, 4), ""); 344static_assert(same(n, n), ""); 345static_assert(sameTemporary(9), ""); 346 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 366static_assert(MangleChars("constexpr!") == 1768383, ""); 367static_assert(MangleChars(u"constexpr!") == 1768383, ""); 368static_assert(MangleChars(U"constexpr!") == 1768383, ""); 369 370constexpr char c0 = "nought index"[0]; 371constexpr char c1 = "nice index"[10]; 372constexpr 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}} 374constexpr 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; 377static_assert(*p == 's', ""); 378 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)); 391static_assert(*max == 'z', ""); 392static_assert(max == str + 38, ""); 393 394static_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, ""); 398 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)); 413static_assert(sum_xs == 15, ""); 414 415constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n, 416 const int *xs, const int *ys, int c) { 417 return n ? F(*xs, *ys, ZipFoldR(F, n-1, xs+1, ys+1, c)) : c; 418} 419constexpr int MulAdd(int x, int y, int c) { return x * y + c; } 420constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0); 421static_assert(InnerProduct == 35, ""); 422 423constexpr int SubMul(int x, int y, int c) { return (x - y) * c; } 424constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1); 425static_assert(DiffProd == 8, ""); 426static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // expected-error {{constant expression}} 427 428constexpr const int *p = xs + 3; 429constexpr int xs4 = p[1]; // ok 430constexpr int xs5 = p[2]; // expected-error {{constant expression}} 431constexpr int xs0 = p[-3]; // ok 432constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} 433 434constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 435static_assert(zs[0][0][0][0] == 1, ""); 436static_assert(zs[1][1][1][1] == 16, ""); 437static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} 438static_assert((&zs[0][0][0][2])[-1] == 2, ""); 439static_assert(**(**(zs + 1) + 1) == 11, ""); 440static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, ""); 441 442constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{extension}} 443constexpr int SumNonzero(const int *p) { 444 return *p + (*p ? SumNonzero(p+1) : 0); 445} 446constexpr int CountZero(const int *p, const int *q) { 447 return p == q ? 0 : (*p == 0) + CountZero(p+1, q); 448} 449static_assert(SumNonzero(arr) == 6, ""); 450static_assert(CountZero(arr, arr + 40) == 36, ""); 451 452struct ArrayElem { 453 constexpr ArrayElem() : n(0) {} 454 int n; 455 constexpr int f() { return n; } 456}; 457struct ArrayRVal { 458 constexpr ArrayRVal() {} 459 ArrayElem elems[10]; 460}; 461static_assert(ArrayRVal().elems[3].f() == 0, ""); 462 463} 464 465namespace DependentValues { 466 467struct I { int n; typedef I V[10]; }; 468I::V x, y; 469template<bool B> struct S { 470 int k; 471 void f() { 472 I::V &cells = B ? x : y; 473 I &i = cells[k]; 474 switch (i.n) {} 475 } 476}; 477 478} 479 480namespace Class { 481 482struct A { constexpr A(int a, int b) : k(a + b) {} int k; }; 483constexpr int fn(const A &a) { return a.k; } 484static_assert(fn(A(4,5)) == 9, ""); 485 486struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}} 487struct C { 488 constexpr C(C *this_) : m(42), n(this_->m) {} // ok 489 int m, n; 490}; 491struct D { 492 C c; 493 constexpr D() : c(&c) {} 494}; 495static_assert(D().c.n == 42, ""); 496 497struct E { 498 constexpr E() : p(&p) {} 499 void *p; 500}; 501constexpr const E &e1 = E(); // expected-error {{constant expression}} 502// This is a constant expression if we elide the copy constructor call, and 503// is not a constant expression if we don't! But we do, so it is. 504// FIXME: The move constructor is not currently implicitly defined as constexpr. 505// We notice this when evaluating an expression which uses it, but not when 506// checking its initializer. 507constexpr E e2 = E(); // unexpected-error {{constant expression}} 508static_assert(e2.p == &e2.p, ""); // unexpected-error {{constant expression}} 509// FIXME: We don't pass through the fact that 'this' is ::e3 when checking the 510// initializer of this declaration. 511constexpr E e3; // unexpected-error {{constant expression}} 512static_assert(e3.p == &e3.p, ""); 513 514extern const class F f; 515struct F { 516 constexpr F() : p(&f.p) {} 517 const void *p; 518}; 519constexpr F f = F(); 520 521struct G { 522 struct T { 523 constexpr T(T *p) : u1(), u2(p) {} 524 union U1 { 525 constexpr U1() {} 526 int a, b = 42; 527 } u1; 528 union U2 { 529 constexpr U2(T *p) : c(p->u1.b) {} 530 int c, d; 531 } u2; 532 } t; 533 constexpr G() : t(&t) {} 534} constexpr g; 535 536static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} 537static_assert(g.t.u1.b == 42, ""); 538static_assert(g.t.u2.c == 42, ""); 539static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} 540 541struct S { 542 int a, b; 543 const S *p; 544 double d; 545 const char *q; 546 547 constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {} 548}; 549 550S global(43, &global); 551 552static_assert(S(15, &global).b == 15, ""); 553 554constexpr bool CheckS(const S &s) { 555 return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l'; 556} 557static_assert(CheckS(S(27, &global)), ""); 558 559struct Arr { 560 char arr[3]; 561 constexpr Arr() : arr{'x', 'y', 'z'} {} 562}; 563constexpr int hash(Arr &&a) { 564 return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000; 565} 566constexpr int k = hash(Arr()); 567static_assert(k == 0x007a7978, ""); 568 569 570struct AggregateInit { 571 const char &c; 572 int n; 573 double d; 574 int arr[5]; 575 void *p; 576}; 577 578constexpr AggregateInit agg1 = { "hello"[0] }; 579 580static_assert(strcmp_ce(&agg1.c, "hello") == 0, ""); 581static_assert(agg1.n == 0, ""); 582static_assert(agg1.d == 0.0, ""); 583static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} 584static_assert(agg1.arr[0] == 0, ""); 585static_assert(agg1.arr[4] == 0, ""); 586static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} 587static_assert(agg1.p == nullptr, ""); 588 589namespace SimpleDerivedClass { 590 591struct B { 592 constexpr B(int n) : a(n) {} 593 int a; 594}; 595struct D : B { 596 constexpr D(int n) : B(n) {} 597}; 598constexpr D d(3); 599static_assert(d.a == 3, ""); 600 601} 602 603struct Bottom { constexpr Bottom() {} }; 604struct Base : Bottom { 605 constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {} 606 int a; 607 const char *b; 608}; 609struct Base2 : Bottom { 610 constexpr Base2(const int &r) : r(r) {} 611 int q = 123; 612 // FIXME: When we track the global for which we are computing the initializer, 613 // use a reference here. 614 //const int &r; 615 int r; 616}; 617struct Derived : Base, Base2 { 618 constexpr Derived() : Base(76), Base2(a) {} 619 int c = r + b[1]; 620}; 621 622constexpr bool operator==(const Base &a, const Base &b) { 623 return a.a == b.a && strcmp_ce(a.b, b.b) == 0; 624} 625 626constexpr Base base; 627constexpr Base base2(76); 628constexpr Derived derived; 629static_assert(derived.a == 76, ""); 630static_assert(derived.b[2] == 's', ""); 631static_assert(derived.c == 76 + 'e', ""); 632static_assert(derived.q == 123, ""); 633static_assert(derived.r == 76, ""); 634static_assert(&derived.r == &derived.a, ""); // expected-error {{}} 635 636static_assert(!(derived == base), ""); 637static_assert(derived == base2, ""); 638 639constexpr Bottom &bot1 = (Base&)derived; 640constexpr Bottom &bot2 = (Base2&)derived; 641static_assert(&bot1 != &bot2, ""); 642 643constexpr Bottom *pb1 = (Base*)&derived; 644constexpr Bottom *pb2 = (Base2*)&derived; 645static_assert(pb1 != pb2, ""); 646static_assert(pb1 == &bot1, ""); 647static_assert(pb2 == &bot2, ""); 648 649constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} 650constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} 651constexpr Base2 &ok2 = (Base2&)bot2; 652static_assert(&ok2 == &derived, ""); 653 654constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} 655constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} 656constexpr Base2 *pok2 = (Base2*)pb2; 657static_assert(pok2 == &derived, ""); 658static_assert(&ok2 == pok2, ""); 659static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, ""); 660static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, ""); 661 662constexpr Base *nullB = 42 - 6 * 7; 663static_assert((Bottom*)nullB == 0, ""); 664static_assert((Derived*)nullB == 0, ""); 665static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, ""); 666 667} 668 669namespace Temporaries { 670 671struct S { 672 constexpr S() {} 673 constexpr int f(); 674}; 675struct T : S { 676 constexpr T(int n) : S(), n(n) {} 677 int n; 678}; 679constexpr int S::f() { 680 // 'this' must be the postfix-expression in a class member access expression, 681 // so we can't just use 682 // return static_cast<T*>(this)->n; 683 return this->*(int(S::*))&T::n; 684} 685// The T temporary is implicitly cast to an S subobject, but we can recover the 686// T full-object via a base-to-derived cast, or a derived-to-base-casted member 687// pointer. 688static_assert(T(3).f() == 3, ""); 689 690constexpr int f(const S &s) { 691 return static_cast<const T&>(s).n; 692} 693constexpr int n = f(T(5)); 694static_assert(f(T(5)) == 5, ""); 695 696} 697 698namespace Union { 699 700union U { 701 int a; 702 int b; 703}; 704 705constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{extension}} 706static_assert(u[0].a == 0, ""); 707static_assert(u[0].b, ""); // expected-error {{constant expression}} 708static_assert(u[1].b == 1, ""); 709static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} 710static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} 711static_assert((&(u[1]) + 1 + 1)->b == 3, ""); 712 713} 714 715namespace MemberPointer { 716 struct A { 717 constexpr A(int n) : n(n) {} 718 int n; 719 constexpr int f() { return n + 3; } 720 }; 721 constexpr A a(7); 722 static_assert(A(5).*&A::n == 5, ""); 723 static_assert((&a)->*&A::n == 7, ""); 724 static_assert((A(8).*&A::f)() == 11, ""); 725 static_assert(((&a)->*&A::f)() == 10, ""); 726 727 struct B : A { 728 constexpr B(int n, int m) : A(n), m(m) {} 729 int m; 730 constexpr int g() { return n + m + 1; } 731 }; 732 constexpr B b(9, 13); 733 static_assert(B(4, 11).*&A::n == 4, ""); 734 static_assert(B(4, 11).*&B::m == 11, ""); 735 static_assert(B(4, 11).*(int(A::*))&B::m == 11, ""); 736 static_assert((&b)->*&A::n == 9, ""); 737 static_assert((&b)->*&B::m == 13, ""); 738 static_assert((&b)->*(int(A::*))&B::m == 13, ""); 739 static_assert((B(4, 11).*&A::f)() == 7, ""); 740 static_assert((B(4, 11).*&B::g)() == 16, ""); 741 static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, ""); 742 static_assert(((&b)->*&A::f)() == 12, ""); 743 static_assert(((&b)->*&B::g)() == 23, ""); 744 static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, ""); 745 746 struct S { 747 constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) : 748 m(m), n(n), pf(pf), pn(pn) {} 749 constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {} 750 751 constexpr int f() { return this->*pn; } 752 virtual int g() const; 753 754 int m, n; 755 int (S::*pf)() const; 756 int S::*pn; 757 }; 758 759 constexpr int S::*pm = &S::m; 760 constexpr int S::*pn = &S::n; 761 constexpr int (S::*pf)() const = &S::f; 762 constexpr int (S::*pg)() const = &S::g; 763 764 constexpr S s(2, 5, &S::f, &S::m); 765 766 static_assert((s.*&S::f)() == 2, ""); 767 static_assert((s.*s.pf)() == 2, ""); 768 769 template<int n> struct T : T<n-1> {}; 770 template<> struct T<0> { int n; }; 771 template<> struct T<30> : T<29> { int m; }; 772 773 T<17> t17; 774 T<30> t30; 775 776 constexpr int (T<10>::*deepn) = &T<0>::n; 777 static_assert(&(t17.*deepn) == &t17.n, ""); 778 779 constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m; 780 constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}} 781 static_assert(&(t30.*deepm) == &t30.m, ""); 782 783 constexpr T<5> *p17_5 = &t17; 784 constexpr T<13> *p17_13 = (T<13>*)p17_5; 785 constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}} 786 static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, ""); 787 static_assert(&(p17_13->*deepn) == &t17.n, ""); 788 constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}} 789 790 constexpr T<5> *p30_5 = &t30; 791 constexpr T<23> *p30_23 = (T<23>*)p30_5; 792 constexpr T<13> *p30_13 = p30_23; 793 static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, ""); 794 static_assert(&(p30_13->*deepn) == &t30.n, ""); 795 static_assert(&(p30_23->*deepn) == &t30.n, ""); 796 static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, ""); 797 static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, ""); 798 static_assert(&(p30_23->*deepm) == &t30.m, ""); 799} 800 801namespace ArrayBaseDerived { 802 803 struct Base { 804 constexpr Base() {} 805 int n = 0; 806 }; 807 struct Derived : Base { 808 constexpr Derived() {} 809 constexpr const int *f() { return &n; } 810 }; 811 812 constexpr Derived a[10]; 813 constexpr Derived *pd3 = const_cast<Derived*>(&a[3]); 814 constexpr Base *pb3 = const_cast<Derived*>(&a[3]); 815 static_assert(pb3 == pd3, ""); 816 817 // pb3 does not point to an array element. 818 constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer. 819 constexpr int pb4n = pb4->n; // expected-error {{constant expression}} 820 constexpr Base *err_pb5 = pb3 + 2; // FIXME: reject this. 821 constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} 822 constexpr Base *err_pb2 = pb3 - 1; // FIXME: reject this. 823 constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} 824 constexpr Base *pb3a = pb4 - 1; 825 826 // pb4 does not point to a Derived. 827 constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} 828 constexpr Derived *pd3a = (Derived*)pb3a; 829 constexpr int pd3n = pd3a->n; 830 831 // pd3a still points to the Derived array. 832 constexpr Derived *pd6 = pd3a + 3; 833 static_assert(pd6 == &a[6], ""); 834 constexpr Derived *pd9 = pd6 + 3; 835 constexpr Derived *pd10 = pd6 + 4; 836 constexpr int pd9n = pd9->n; // ok 837 constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} 838 constexpr int pd0n = pd10[-10].n; 839 constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} 840 841 constexpr Base *pb9 = pd9; 842 constexpr const int *(Base::*pfb)() const = 843 static_cast<const int *(Base::*)() const>(&Derived::f); 844 static_assert((pb9->*pfb)() == &a[9].n, ""); 845} 846 847namespace Complex { 848 849class complex { 850 int re, im; 851public: 852 constexpr complex(int re = 0, int im = 0) : re(re), im(im) {} 853 constexpr complex(const complex &o) : re(o.re), im(o.im) {} 854 constexpr complex operator-() const { return complex(-re, -im); } 855 friend constexpr complex operator+(const complex &l, const complex &r) { 856 return complex(l.re + r.re, l.im + r.im); 857 } 858 friend constexpr complex operator-(const complex &l, const complex &r) { 859 return l + -r; 860 } 861 friend constexpr complex operator*(const complex &l, const complex &r) { 862 return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re); 863 } 864 friend constexpr bool operator==(const complex &l, const complex &r) { 865 return l.re == r.re && l.im == r.im; 866 } 867 constexpr bool operator!=(const complex &r) const { 868 return re != r.re || im != r.im; 869 } 870 constexpr int real() const { return re; } 871 constexpr int imag() const { return im; } 872}; 873 874constexpr complex i = complex(0, 1); 875constexpr complex k = (3 + 4*i) * (6 - 4*i); 876static_assert(complex(1,0).real() == 1, ""); 877static_assert(complex(1,0).imag() == 0, ""); 878static_assert(((complex)1).imag() == 0, ""); 879static_assert(k.real() == 34, ""); 880static_assert(k.imag() == 12, ""); 881static_assert(k - 34 == 12*i, ""); 882static_assert((complex)1 == complex(1), ""); 883static_assert((complex)1 != complex(0, 1), ""); 884static_assert(complex(1) == complex(1), ""); 885static_assert(complex(1) != complex(0, 1), ""); 886constexpr complex makeComplex(int re, int im) { return complex(re, im); } 887static_assert(makeComplex(1,0) == complex(1), ""); 888static_assert(makeComplex(1,0) != complex(0, 1), ""); 889 890class complex_wrap : public complex { 891public: 892 constexpr complex_wrap(int re, int im = 0) : complex(re, im) {} 893 constexpr complex_wrap(const complex_wrap &o) : complex(o) {} 894}; 895 896static_assert((complex_wrap)1 == complex(1), ""); 897static_assert((complex)1 != complex_wrap(0, 1), ""); 898static_assert(complex(1) == complex_wrap(1), ""); 899static_assert(complex_wrap(1) != complex(0, 1), ""); 900constexpr complex_wrap makeComplexWrap(int re, int im) { 901 return complex_wrap(re, im); 902} 903static_assert(makeComplexWrap(1,0) == complex(1), ""); 904static_assert(makeComplexWrap(1,0) != complex(0, 1), ""); 905 906} 907