constant-expression-cxx11.cpp revision f48fdb0937e67f691393f9ffdf75653e5128ea13
1// RUN: %clang_cc1 -triple i686-linux -fsyntax-only -verify -std=c++11 %s 2 3#define static_assert_fold static_assert 4 5namespace StaticAssertFoldTest { 6 7int x; 8static_assert_fold(++x, "test"); // expected-error {{not an integral constant expression}} 9static_assert_fold(false, "test"); // expected-error {{test}} 10 11} 12 13// FIXME: support const T& parameters here. 14//template<typename T> constexpr T id(const T &t) { return t; } 15template<typename T> constexpr T id(T t) { return t; } 16// FIXME: support templates here. 17//template<typename T> constexpr T min(const T &a, const T &b) { 18// return a < b ? a : b; 19//} 20//template<typename T> constexpr T max(const T &a, const T &b) { 21// return a < b ? b : a; 22//} 23constexpr int min(const int &a, const int &b) { return a < b ? a : b; } 24constexpr int max(const int &a, const int &b) { return a < b ? b : a; } 25 26struct MemberZero { 27 constexpr int zero() { return 0; } 28}; 29 30namespace DerivedToVBaseCast { 31 32 struct U { int n; }; 33 struct V : U { int n; }; 34 struct A : virtual V { int n; }; 35 struct Aa { int n; }; 36 struct B : virtual A, Aa {}; 37 struct C : virtual A, Aa {}; 38 struct D : B, C {}; 39 40 D d; 41 constexpr B *p = &d; 42 constexpr C *q = &d; 43 static_assert_fold((void*)p != (void*)q, ""); 44 static_assert_fold((A*)p == (A*)q, ""); 45 static_assert_fold((Aa*)p != (Aa*)q, ""); 46 47 constexpr B &pp = d; 48 constexpr C &qq = d; 49 static_assert_fold((void*)&pp != (void*)&qq, ""); 50 static_assert_fold(&(A&)pp == &(A&)qq, ""); 51 static_assert_fold(&(Aa&)pp != &(Aa&)qq, ""); 52 53 constexpr V *v = p; 54 constexpr V *w = q; 55 constexpr V *x = (A*)p; 56 static_assert_fold(v == w, ""); 57 static_assert_fold(v == x, ""); 58 59 static_assert_fold((U*)&d == p, ""); 60 static_assert_fold((U*)&d == q, ""); 61 static_assert_fold((U*)&d == v, ""); 62 static_assert_fold((U*)&d == w, ""); 63 static_assert_fold((U*)&d == x, ""); 64 65 struct X {}; 66 struct Y1 : virtual X {}; 67 struct Y2 : X {}; 68 struct Z : Y1, Y2 {}; 69 Z z; 70 static_assert_fold((X*)(Y1*)&z != (X*)(Y2*)&z, ""); 71 72} 73 74namespace ConstCast { 75 76constexpr int n1 = 0; 77constexpr int n2 = const_cast<int&>(n1); 78constexpr int *n3 = const_cast<int*>(&n1); 79constexpr int n4 = *const_cast<int*>(&n1); 80constexpr const int * const *n5 = const_cast<const int* const*>(&n3); 81constexpr int **n6 = const_cast<int**>(&n3); 82constexpr int n7 = **n5; 83constexpr int n8 = **n6; 84 85} 86 87namespace TemplateArgumentConversion { 88 template<int n> struct IntParam {}; 89 90 using IntParam0 = IntParam<0>; 91 // FIXME: This should be accepted once we implement the new ICE rules. 92 using IntParam0 = IntParam<id(0)>; // expected-error {{not an integral constant expression}} 93 using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} expected-error {{not an integral constant expression}} 94} 95 96namespace CaseStatements { 97 void f(int n) { 98 switch (n) { 99 // FIXME: Produce the 'add ()' fixit for this. 100 case MemberZero().zero: // desired-error {{did you mean to call it with no arguments?}} expected-error {{not an integer constant expression}} 101 // FIXME: This should be accepted once we implement the new ICE rules. 102 case id(1): // expected-error {{not an integer constant expression}} 103 return; 104 } 105 } 106} 107 108extern int &Recurse1; 109int &Recurse2 = Recurse1, &Recurse1 = Recurse2; 110constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} 111 112namespace MemberEnum { 113 struct WithMemberEnum { 114 enum E { A = 42 }; 115 } wme; 116 117 static_assert_fold(wme.A == 42, ""); 118} 119 120namespace DefaultArguments { 121 122const int z = int(); 123constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) { 124 return a + b + *c + d; 125} 126const int four = 4; 127constexpr int eight = 8; 128constexpr const int twentyseven = 27; 129static_assert_fold(Sum() == 0, ""); 130static_assert_fold(Sum(1) == 1, ""); 131static_assert_fold(Sum(1, four) == 5, ""); 132static_assert_fold(Sum(1, eight, &twentyseven) == 36, ""); 133static_assert_fold(Sum(1, 2, &four, eight) == 15, ""); 134 135} 136 137namespace Ellipsis { 138 139// Note, values passed through an ellipsis can't actually be used. 140constexpr int F(int a, ...) { return a; } 141static_assert_fold(F(0) == 0, ""); 142static_assert_fold(F(1, 0) == 1, ""); 143static_assert_fold(F(2, "test") == 2, ""); 144static_assert_fold(F(3, &F) == 3, ""); 145int k = 0; 146static_assert_fold(F(4, k) == 3, ""); // expected-error {{constant expression}} 147 148} 149 150namespace Recursion { 151 constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; } 152 static_assert_fold(fib(11) == 89, ""); 153 154 constexpr int gcd_inner(int a, int b) { 155 return b == 0 ? a : gcd_inner(b, a % b); 156 } 157 constexpr int gcd(int a, int b) { 158 return gcd_inner(max(a, b), min(a, b)); 159 } 160 161 static_assert_fold(gcd(1749237, 5628959) == 7, ""); 162} 163 164namespace FunctionCast { 165 // When folding, we allow functions to be cast to different types. Such 166 // cast functions cannot be called, even if they're constexpr. 167 constexpr int f() { return 1; } 168 typedef double (*DoubleFn)(); 169 typedef int (*IntFn)(); 170 int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} 171 int b[(int)IntFn(f)()]; // ok 172} 173 174namespace StaticMemberFunction { 175 struct S { 176 static constexpr int k = 42; 177 static constexpr int f(int n) { return n * k + 2; } 178 } s; 179 180 constexpr int n = s.f(19); 181 static_assert_fold(S::f(19) == 800, ""); 182 static_assert_fold(s.f(19) == 800, ""); 183 static_assert_fold(n == 800, ""); 184 185 constexpr int (*sf1)(int) = &S::f; 186 constexpr int (*sf2)(int) = &s.f; 187 constexpr const int *sk = &s.k; 188} 189 190namespace ParameterScopes { 191 192 const int k = 42; 193 constexpr const int &ObscureTheTruth(const int &a) { return a; } 194 constexpr const int &MaybeReturnJunk(bool b, const int a) { 195 return ObscureTheTruth(b ? a : k); 196 } 197 static_assert_fold(MaybeReturnJunk(false, 0) == 42, ""); // ok 198 constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} 199 200 constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { 201 // If ObscureTheTruth returns a reference to 'a', the result is not a 202 // constant expression even though 'a' is still in scope. 203 return ObscureTheTruth(b ? a : k); 204 } 205 static_assert_fold(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok 206 constexpr int b = MaybeReturnNonstaticRef(true, 0); // expected-error {{constant expression}} 207 208 constexpr int InternalReturnJunk(int n) { 209 // FIXME: We should reject this: it never produces a constant expression. 210 return MaybeReturnJunk(true, n); 211 } 212 constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} 213 214 constexpr int LToR(int &n) { return n; } 215 constexpr int GrabCallersArgument(bool which, int a, int b) { 216 return LToR(which ? b : a); 217 } 218 static_assert_fold(GrabCallersArgument(false, 1, 2) == 1, ""); 219 static_assert_fold(GrabCallersArgument(true, 4, 8) == 8, ""); 220 221} 222 223namespace Pointers { 224 225 constexpr int f(int n, const int *a, const int *b, const int *c) { 226 return n == 0 ? 0 : *a + f(n-1, b, c, a); 227 } 228 229 const int x = 1, y = 10, z = 100; 230 static_assert_fold(f(23, &x, &y, &z) == 788, ""); 231 232 constexpr int g(int n, int a, int b, int c) { 233 return f(n, &a, &b, &c); 234 } 235 static_assert_fold(g(23, x, y, z) == 788, ""); 236 237} 238 239namespace FunctionPointers { 240 241 constexpr int Double(int n) { return 2 * n; } 242 constexpr int Triple(int n) { return 3 * n; } 243 constexpr int Twice(int (*F)(int), int n) { return F(F(n)); } 244 constexpr int Quadruple(int n) { return Twice(Double, n); } 245 constexpr auto Select(int n) -> int (*)(int) { 246 return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0; 247 } 248 constexpr int Apply(int (*F)(int), int n) { return F(n); } 249 250 static_assert_fold(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, ""); 251 252 constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} 253 254} 255 256namespace PointerComparison { 257 258int x, y; 259static_assert_fold(&x == &y, "false"); // expected-error {{false}} 260static_assert_fold(&x != &y, ""); 261constexpr bool g1 = &x == &y; 262constexpr bool g2 = &x != &y; 263constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}} 264constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}} 265constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}} 266constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}} 267 268struct S { int x, y; } s; 269static_assert_fold(&s.x == &s.y, "false"); // expected-error {{false}} 270static_assert_fold(&s.x != &s.y, ""); 271static_assert_fold(&s.x <= &s.y, ""); 272static_assert_fold(&s.x >= &s.y, "false"); // expected-error {{false}} 273static_assert_fold(&s.x < &s.y, ""); 274static_assert_fold(&s.x > &s.y, "false"); // expected-error {{false}} 275 276static_assert_fold(0 == &y, "false"); // expected-error {{false}} 277static_assert_fold(0 != &y, ""); 278constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}} 279constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}} 280constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}} 281constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}} 282 283static_assert_fold(&x == 0, "false"); // expected-error {{false}} 284static_assert_fold(&x != 0, ""); 285constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}} 286constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}} 287constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}} 288constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}} 289 290static_assert_fold(&x == &x, ""); 291static_assert_fold(&x != &x, "false"); // expected-error {{false}} 292static_assert_fold(&x <= &x, ""); 293static_assert_fold(&x >= &x, ""); 294static_assert_fold(&x < &x, "false"); // expected-error {{false}} 295static_assert_fold(&x > &x, "false"); // expected-error {{false}} 296 297constexpr S* sptr = &s; 298// FIXME: This is not a constant expression; check we reject this and move this 299// test elsewhere. 300constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); 301 302extern char externalvar[]; 303// FIXME: This is not a constant expression; check we reject this and move this 304// test elsewhere. 305constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}} 306constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}} 307static_assert_fold(0 != "foo", ""); 308 309} 310 311namespace MaterializeTemporary { 312 313constexpr int f(const int &r) { return r; } 314constexpr int n = f(1); 315 316constexpr bool same(const int &a, const int &b) { return &a == &b; } 317constexpr bool sameTemporary(const int &n) { return same(n, n); } 318 319static_assert_fold(n, ""); 320static_assert_fold(!same(4, 4), ""); 321static_assert_fold(same(n, n), ""); 322static_assert_fold(sameTemporary(9), ""); 323 324} 325 326constexpr int strcmp_ce(const char *p, const char *q) { 327 return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1); 328} 329 330namespace StringLiteral { 331 332// FIXME: Refactor this once we support constexpr templates. 333constexpr int MangleChars(const char *p) { 334 return *p + 3 * (*p ? MangleChars(p+1) : 0); 335} 336constexpr int MangleChars(const char16_t *p) { 337 return *p + 3 * (*p ? MangleChars(p+1) : 0); 338} 339constexpr int MangleChars(const char32_t *p) { 340 return *p + 3 * (*p ? MangleChars(p+1) : 0); 341} 342 343static_assert_fold(MangleChars("constexpr!") == 1768383, ""); 344static_assert_fold(MangleChars(u"constexpr!") == 1768383, ""); 345static_assert_fold(MangleChars(U"constexpr!") == 1768383, ""); 346 347constexpr char c0 = "nought index"[0]; 348constexpr char c1 = "nice index"[10]; 349constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}} 350constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} 351constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} 352 353constexpr const char *p = "test" + 2; 354static_assert_fold(*p == 's', ""); 355 356constexpr const char *max_iter(const char *a, const char *b) { 357 return *a < *b ? b : a; 358} 359constexpr const char *max_element(const char *a, const char *b) { 360 return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b)); 361} 362 363constexpr const char *begin(const char (&arr)[45]) { return arr; } 364constexpr const char *end(const char (&arr)[45]) { return arr + 45; } 365 366constexpr char str[] = "the quick brown fox jumped over the lazy dog"; 367constexpr const char *max = max_element(begin(str), end(str)); 368static_assert_fold(*max == 'z', ""); 369static_assert_fold(max == str + 38, ""); 370 371static_assert_fold(strcmp_ce("hello world", "hello world") == 0, ""); 372static_assert_fold(strcmp_ce("hello world", "hello clang") > 0, ""); 373static_assert_fold(strcmp_ce("constexpr", "test") < 0, ""); 374static_assert_fold(strcmp_ce("", " ") < 0, ""); 375 376} 377 378namespace Array { 379 380// FIXME: Use templates for these once we support constexpr templates. 381constexpr int Sum(const int *begin, const int *end) { 382 return begin == end ? 0 : *begin + Sum(begin+1, end); 383} 384constexpr const int *begin(const int (&xs)[5]) { return xs; } 385constexpr const int *end(const int (&xs)[5]) { return xs + 5; } 386 387constexpr int xs[] = { 1, 2, 3, 4, 5 }; 388constexpr int ys[] = { 5, 4, 3, 2, 1 }; 389constexpr int sum_xs = Sum(begin(xs), end(xs)); 390static_assert_fold(sum_xs == 15, ""); 391 392constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n, 393 const int *xs, const int *ys, int c) { 394 return n ? F(*xs, *ys, ZipFoldR(F, n-1, xs+1, ys+1, c)) : c; 395} 396constexpr int MulAdd(int x, int y, int c) { return x * y + c; } 397constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0); 398static_assert_fold(InnerProduct == 35, ""); 399 400constexpr int SubMul(int x, int y, int c) { return (x - y) * c; } 401constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1); 402static_assert_fold(DiffProd == 8, ""); 403static_assert_fold(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // expected-error {{constant expression}} 404 405constexpr const int *p = xs + 3; 406constexpr int xs4 = p[1]; // ok 407constexpr int xs5 = p[2]; // expected-error {{constant expression}} 408constexpr int xs0 = p[-3]; // ok 409constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} 410 411constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 412static_assert_fold(zs[0][0][0][0] == 1, ""); 413static_assert_fold(zs[1][1][1][1] == 16, ""); 414static_assert_fold(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} 415static_assert_fold((&zs[0][0][0][2])[-1] == 2, ""); 416static_assert_fold(**(**(zs + 1) + 1) == 11, ""); 417static_assert_fold(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, ""); 418 419constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; 420constexpr int SumNonzero(const int *p) { 421 return *p + (*p ? SumNonzero(p+1) : 0); 422} 423constexpr int CountZero(const int *p, const int *q) { 424 return p == q ? 0 : (*p == 0) + CountZero(p+1, q); 425} 426static_assert_fold(SumNonzero(arr) == 6, ""); 427static_assert_fold(CountZero(arr, arr + 40) == 36, ""); 428 429struct ArrayElem { 430 constexpr ArrayElem() : n(0) {} 431 int n; 432 constexpr int f() { return n; } 433}; 434struct ArrayRVal { 435 constexpr ArrayRVal() {} 436 ArrayElem elems[10]; 437}; 438static_assert_fold(ArrayRVal().elems[3].f() == 0, ""); 439 440} 441 442namespace DependentValues { 443 444struct I { int n; typedef I V[10]; }; 445I::V x, y; 446template<bool B> struct S { 447 int k; 448 void f() { 449 I::V &cells = B ? x : y; 450 I &i = cells[k]; 451 switch (i.n) {} 452 } 453}; 454 455} 456 457namespace Class { 458 459struct A { constexpr A(int a, int b) : k(a + b) {} int k; }; 460constexpr int fn(const A &a) { return a.k; } 461static_assert_fold(fn(A(4,5)) == 9, ""); 462 463struct B { int n; int m; } constexpr b = { 0, b.n }; // expected-warning {{uninitialized}} 464struct C { 465 constexpr C(C *this_) : m(42), n(this_->m) {} // ok 466 int m, n; 467}; 468struct D { 469 C c; 470 constexpr D() : c(&c) {} 471}; 472static_assert_fold(D().c.n == 42, ""); 473 474struct E { 475 constexpr E() : p(&p) {} 476 void *p; 477}; 478constexpr const E &e1 = E(); // expected-error {{constant expression}} 479// This is a constant expression if we elide the copy constructor call, and 480// is not a constant expression if we don't! But we do, so it is. 481// FIXME: The move constructor is not currently implicitly defined as constexpr. 482// We notice this when evaluating an expression which uses it, but not when 483// checking its initializer. 484constexpr E e2 = E(); // unexpected-error {{constant expression}} 485static_assert_fold(e2.p == &e2.p, ""); // unexpected-error {{constant expression}} 486// FIXME: We don't pass through the fact that 'this' is ::e3 when checking the 487// initializer of this declaration. 488constexpr E e3; // unexpected-error {{constant expression}} 489static_assert_fold(e3.p == &e3.p, ""); 490 491extern const class F f; 492struct F { 493 constexpr F() : p(&f.p) {} 494 const void *p; 495}; 496constexpr F f = F(); 497 498struct G { 499 struct T { 500 constexpr T(T *p) : u1(), u2(p) {} 501 union U1 { 502 constexpr U1() {} 503 int a, b = 42; 504 } u1; 505 union U2 { 506 constexpr U2(T *p) : c(p->u1.b) {} 507 int c, d; 508 } u2; 509 } t; 510 constexpr G() : t(&t) {} 511} constexpr g; 512 513static_assert_fold(g.t.u1.a == 42, ""); // expected-error {{constant expression}} 514static_assert_fold(g.t.u1.b == 42, ""); 515static_assert_fold(g.t.u2.c == 42, ""); 516static_assert_fold(g.t.u2.d == 42, ""); // expected-error {{constant expression}} 517 518struct S { 519 int a, b; 520 const S *p; 521 double d; 522 const char *q; 523 524 constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {} 525}; 526 527S global(43, &global); 528 529static_assert_fold(S(15, &global).b == 15, ""); 530 531constexpr bool CheckS(const S &s) { 532 return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l'; 533} 534static_assert_fold(CheckS(S(27, &global)), ""); 535 536struct Arr { 537 char arr[3]; 538 constexpr Arr() : arr{'x', 'y', 'z'} {} 539}; 540constexpr int hash(Arr &&a) { 541 return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000; 542} 543constexpr int k = hash(Arr()); 544static_assert_fold(k == 0x007a7978, ""); 545 546 547struct AggregateInit { 548 const char &c; 549 int n; 550 double d; 551 int arr[5]; 552 void *p; 553}; 554 555constexpr AggregateInit agg1 = { "hello"[0] }; 556 557static_assert_fold(strcmp_ce(&agg1.c, "hello") == 0, ""); 558static_assert_fold(agg1.n == 0, ""); 559static_assert_fold(agg1.d == 0.0, ""); 560static_assert_fold(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} 561static_assert_fold(agg1.arr[0] == 0, ""); 562static_assert_fold(agg1.arr[4] == 0, ""); 563static_assert_fold(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} 564static_assert_fold(agg1.p == nullptr, ""); 565 566namespace SimpleDerivedClass { 567 568struct B { 569 constexpr B(int n) : a(n) {} 570 int a; 571}; 572struct D : B { 573 constexpr D(int n) : B(n) {} 574}; 575constexpr D d(3); 576static_assert_fold(d.a == 3, ""); 577 578} 579 580struct Bottom { constexpr Bottom() {} }; 581struct Base : Bottom { 582 constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {} 583 int a; 584 const char *b; 585}; 586struct Base2 : Bottom { 587 constexpr Base2(const int &r) : r(r) {} 588 int q = 123; 589 // FIXME: When we track the global for which we are computing the initializer, 590 // use a reference here. 591 //const int &r; 592 int r; 593}; 594struct Derived : Base, Base2 { 595 constexpr Derived() : Base(76), Base2(a) {} 596 int c = r + b[1]; 597}; 598 599constexpr bool operator==(const Base &a, const Base &b) { 600 return a.a == b.a && strcmp_ce(a.b, b.b) == 0; 601} 602 603constexpr Base base; 604constexpr Base base2(76); 605constexpr Derived derived; 606static_assert_fold(derived.a == 76, ""); 607static_assert_fold(derived.b[2] == 's', ""); 608static_assert_fold(derived.c == 76 + 'e', ""); 609static_assert_fold(derived.q == 123, ""); 610static_assert_fold(derived.r == 76, ""); 611static_assert_fold(&derived.r == &derived.a, ""); // expected-error {{}} 612 613static_assert_fold(!(derived == base), ""); 614static_assert_fold(derived == base2, ""); 615 616constexpr Bottom &bot1 = (Base&)derived; 617constexpr Bottom &bot2 = (Base2&)derived; 618static_assert_fold(&bot1 != &bot2, ""); 619 620constexpr Bottom *pb1 = (Base*)&derived; 621constexpr Bottom *pb2 = (Base2*)&derived; 622static_assert_fold(pb1 != pb2, ""); 623static_assert_fold(pb1 == &bot1, ""); 624static_assert_fold(pb2 == &bot2, ""); 625 626constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} 627constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} 628constexpr Base2 &ok2 = (Base2&)bot2; 629static_assert_fold(&ok2 == &derived, ""); 630 631constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} 632constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} 633constexpr Base2 *pok2 = (Base2*)pb2; 634static_assert_fold(pok2 == &derived, ""); 635static_assert_fold(&ok2 == pok2, ""); 636static_assert_fold((Base2*)(Derived*)(Base*)pb1 == pok2, ""); 637static_assert_fold((Derived*)(Base*)pb1 == (Derived*)pok2, ""); 638 639constexpr Base *nullB = 42 - 6 * 7; 640static_assert_fold((Bottom*)nullB == 0, ""); 641static_assert_fold((Derived*)nullB == 0, ""); 642static_assert_fold((void*)(Bottom*)nullB == (void*)(Derived*)nullB, ""); 643 644} 645 646namespace Temporaries { 647 648struct S { 649 constexpr S() {} 650 constexpr int f(); 651}; 652struct T : S { 653 constexpr T(int n) : S(), n(n) {} 654 int n; 655}; 656constexpr int S::f() { 657 // 'this' must be the postfix-expression in a class member access expression, 658 // so we can't just use 659 // return static_cast<T*>(this)->n; 660 return this->*(int(S::*))&T::n; 661} 662// The T temporary is implicitly cast to an S subobject, but we can recover the 663// T full-object via a base-to-derived cast, or a derived-to-base-casted member 664// pointer. 665static_assert_fold(T(3).f() == 3, ""); 666 667constexpr int f(const S &s) { 668 return static_cast<const T&>(s).n; 669} 670constexpr int n = f(T(5)); 671static_assert_fold(f(T(5)) == 5, ""); 672 673} 674 675namespace Union { 676 677union U { 678 int a; 679 int b; 680}; 681 682constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; 683static_assert_fold(u[0].a == 0, ""); 684static_assert_fold(u[0].b, ""); // expected-error {{constant expression}} 685static_assert_fold(u[1].b == 1, ""); 686static_assert_fold((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} 687static_assert_fold(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} 688static_assert_fold((&(u[1]) + 1 + 1)->b == 3, ""); 689 690} 691 692namespace MemberPointer { 693 struct A { 694 constexpr A(int n) : n(n) {} 695 int n; 696 constexpr int f() { return n + 3; } 697 }; 698 constexpr A a(7); 699 static_assert_fold(A(5).*&A::n == 5, ""); 700 static_assert_fold((&a)->*&A::n == 7, ""); 701 static_assert_fold((A(8).*&A::f)() == 11, ""); 702 static_assert_fold(((&a)->*&A::f)() == 10, ""); 703 704 struct B : A { 705 constexpr B(int n, int m) : A(n), m(m) {} 706 int m; 707 constexpr int g() { return n + m + 1; } 708 }; 709 constexpr B b(9, 13); 710 static_assert_fold(B(4, 11).*&A::n == 4, ""); 711 static_assert_fold(B(4, 11).*&B::m == 11, ""); 712 static_assert_fold(B(4, 11).*(int(A::*))&B::m == 11, ""); 713 static_assert_fold((&b)->*&A::n == 9, ""); 714 static_assert_fold((&b)->*&B::m == 13, ""); 715 static_assert_fold((&b)->*(int(A::*))&B::m == 13, ""); 716 static_assert_fold((B(4, 11).*&A::f)() == 7, ""); 717 static_assert_fold((B(4, 11).*&B::g)() == 16, ""); 718 static_assert_fold((B(4, 11).*(int(A::*)()const)&B::g)() == 16, ""); 719 static_assert_fold(((&b)->*&A::f)() == 12, ""); 720 static_assert_fold(((&b)->*&B::g)() == 23, ""); 721 static_assert_fold(((&b)->*(int(A::*)()const)&B::g)() == 23, ""); 722 723 struct S { 724 constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) : 725 m(m), n(n), pf(pf), pn(pn) {} 726 constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {} 727 728 constexpr int f() { return this->*pn; } 729 virtual int g() const; 730 731 int m, n; 732 int (S::*pf)() const; 733 int S::*pn; 734 }; 735 736 constexpr int S::*pm = &S::m; 737 constexpr int S::*pn = &S::n; 738 constexpr int (S::*pf)() const = &S::f; 739 constexpr int (S::*pg)() const = &S::g; 740 741 constexpr S s(2, 5, &S::f, &S::m); 742 743 static_assert_fold((s.*&S::f)() == 2, ""); 744 static_assert_fold((s.*s.pf)() == 2, ""); 745 746 template<int n> struct T : T<n-1> {}; 747 template<> struct T<0> { int n; }; 748 template<> struct T<30> : T<29> { int m; }; 749 750 T<17> t17; 751 T<30> t30; 752 753 constexpr int (T<10>::*deepn) = &T<0>::n; 754 static_assert_fold(&(t17.*deepn) == &t17.n, ""); 755 756 constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m; 757 constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}} 758 static_assert_fold(&(t30.*deepm) == &t30.m, ""); 759 760 constexpr T<5> *p17_5 = &t17; 761 constexpr T<13> *p17_13 = (T<13>*)p17_5; 762 constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}} 763 static_assert_fold(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, ""); 764 static_assert_fold(&(p17_13->*deepn) == &t17.n, ""); 765 constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}} 766 767 constexpr T<5> *p30_5 = &t30; 768 constexpr T<23> *p30_23 = (T<23>*)p30_5; 769 constexpr T<13> *p30_13 = p30_23; 770 static_assert_fold(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, ""); 771 static_assert_fold(&(p30_13->*deepn) == &t30.n, ""); 772 static_assert_fold(&(p30_23->*deepn) == &t30.n, ""); 773 static_assert_fold(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, ""); 774 static_assert_fold(&(((T<17>*)p30_13)->*deepm) == &t30.m, ""); 775 static_assert_fold(&(p30_23->*deepm) == &t30.m, ""); 776} 777 778namespace ArrayBaseDerived { 779 780 struct Base { 781 constexpr Base() {} 782 int n = 0; 783 }; 784 struct Derived : Base { 785 constexpr Derived() {} 786 constexpr const int *f() { return &n; } 787 }; 788 789 constexpr Derived a[10]; 790 constexpr Derived *pd3 = const_cast<Derived*>(&a[3]); 791 constexpr Base *pb3 = const_cast<Derived*>(&a[3]); 792 static_assert_fold(pb3 == pd3, ""); 793 794 // pb3 does not point to an array element. 795 constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer. 796 constexpr int pb4n = pb4->n; // expected-error {{constant expression}} 797 constexpr Base *err_pb5 = pb3 + 2; // FIXME: reject this. 798 constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} 799 constexpr Base *err_pb2 = pb3 - 1; // FIXME: reject this. 800 constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} 801 constexpr Base *pb3a = pb4 - 1; 802 803 // pb4 does not point to a Derived. 804 constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} 805 constexpr Derived *pd3a = (Derived*)pb3a; 806 constexpr int pd3n = pd3a->n; 807 808 // pd3a still points to the Derived array. 809 constexpr Derived *pd6 = pd3a + 3; 810 static_assert_fold(pd6 == &a[6], ""); 811 constexpr Derived *pd9 = pd6 + 3; 812 constexpr Derived *pd10 = pd6 + 4; 813 constexpr int pd9n = pd9->n; // ok 814 constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} 815 constexpr int pd0n = pd10[-10].n; 816 constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} 817 818 constexpr Base *pb9 = pd9; 819 constexpr const int *(Base::*pfb)() const = 820 static_cast<const int *(Base::*)() const>(&Derived::f); 821 static_assert_fold((pb9->*pfb)() == &a[9].n, ""); 822} 823 824namespace Complex { 825 826class complex { 827 int re, im; 828public: 829 constexpr complex(int re = 0, int im = 0) : re(re), im(im) {} 830 constexpr complex(const complex &o) : re(o.re), im(o.im) {} 831 constexpr complex operator-() const { return complex(-re, -im); } 832 friend constexpr complex operator+(const complex &l, const complex &r) { 833 return complex(l.re + r.re, l.im + r.im); 834 } 835 friend constexpr complex operator-(const complex &l, const complex &r) { 836 return l + -r; 837 } 838 friend constexpr complex operator*(const complex &l, const complex &r) { 839 return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re); 840 } 841 friend constexpr bool operator==(const complex &l, const complex &r) { 842 return l.re == r.re && l.im == r.im; 843 } 844 constexpr bool operator!=(const complex &r) const { 845 return re != r.re || im != r.im; 846 } 847 constexpr int real() const { return re; } 848 constexpr int imag() const { return im; } 849}; 850 851constexpr complex i = complex(0, 1); 852constexpr complex k = (3 + 4*i) * (6 - 4*i); 853static_assert_fold(complex(1,0).real() == 1, ""); 854static_assert_fold(complex(1,0).imag() == 0, ""); 855static_assert_fold(((complex)1).imag() == 0, ""); 856static_assert_fold(k.real() == 34, ""); 857static_assert_fold(k.imag() == 12, ""); 858static_assert_fold(k - 34 == 12*i, ""); 859static_assert_fold((complex)1 == complex(1), ""); 860static_assert_fold((complex)1 != complex(0, 1), ""); 861static_assert_fold(complex(1) == complex(1), ""); 862static_assert_fold(complex(1) != complex(0, 1), ""); 863constexpr complex makeComplex(int re, int im) { return complex(re, im); } 864static_assert_fold(makeComplex(1,0) == complex(1), ""); 865static_assert_fold(makeComplex(1,0) != complex(0, 1), ""); 866 867class complex_wrap : public complex { 868public: 869 constexpr complex_wrap(int re, int im = 0) : complex(re, im) {} 870 constexpr complex_wrap(const complex_wrap &o) : complex(o) {} 871}; 872 873static_assert_fold((complex_wrap)1 == complex(1), ""); 874static_assert_fold((complex)1 != complex_wrap(0, 1), ""); 875static_assert_fold(complex(1) == complex_wrap(1), ""); 876static_assert_fold(complex_wrap(1) != complex(0, 1), ""); 877constexpr complex_wrap makeComplexWrap(int re, int im) { 878 return complex_wrap(re, im); 879} 880static_assert_fold(makeComplexWrap(1,0) == complex(1), ""); 881static_assert_fold(makeComplexWrap(1,0) != complex(0, 1), ""); 882 883} 884