1// RUN: %clang_cc1 -triple i686-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -fsyntax-only -fcxx-exceptions -verify -std=c++11 -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion 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 11typedef decltype(sizeof(char)) size_t; 12 13template<typename T> constexpr T id(const T &t) { return t; } 14template<typename T> constexpr T min(const T &a, const T &b) { 15 return a < b ? a : b; 16} 17template<typename T> constexpr T max(const T &a, const T &b) { 18 return a < b ? b : a; 19} 20template<typename T, size_t N> constexpr T *begin(T (&xs)[N]) { return xs; } 21template<typename T, size_t N> constexpr T *end(T (&xs)[N]) { return xs + N; } 22 23struct MemberZero { 24 constexpr int zero() const { return 0; } 25}; 26 27namespace DerivedToVBaseCast { 28 29 struct U { int n; }; 30 struct V : U { int n; }; 31 struct A : virtual V { int n; }; 32 struct Aa { int n; }; 33 struct B : virtual A, Aa {}; 34 struct C : virtual A, Aa {}; 35 struct D : B, C {}; 36 37 D d; 38 constexpr B *p = &d; 39 constexpr C *q = &d; 40 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 71namespace ConstCast { 72 73constexpr int n1 = 0; 74constexpr int n2 = const_cast<int&>(n1); 75constexpr int *n3 = const_cast<int*>(&n1); 76constexpr int n4 = *const_cast<int*>(&n1); 77constexpr const int * const *n5 = const_cast<const int* const*>(&n3); 78constexpr int **n6 = const_cast<int**>(&n3); 79constexpr int n7 = **n5; 80constexpr int n8 = **n6; 81 82// const_cast from prvalue to xvalue. 83struct A { int n; }; 84constexpr int n9 = (const_cast<A&&>(A{123})).n; 85static_assert(n9 == 123, ""); 86 87} 88 89namespace TemplateArgumentConversion { 90 template<int n> struct IntParam {}; 91 92 using IntParam0 = IntParam<0>; 93 using IntParam0 = IntParam<id(0)>; 94 using IntParam0 = IntParam<MemberZero().zero>; // expected-error {{did you mean to call it with no arguments?}} 95} 96 97namespace CaseStatements { 98 int x; 99 void f(int n) { 100 switch (n) { 101 case MemberZero().zero: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}} 102 case id(0): // expected-error {{duplicate case value '0'}} 103 return; 104 case __builtin_constant_p(true) ? (__SIZE_TYPE__)&x : 0:; // expected-error {{constant}} 105 } 106 } 107} 108 109extern int &Recurse1; 110int &Recurse2 = Recurse1; // expected-note {{declared here}} 111int &Recurse1 = Recurse2; 112constexpr int &Recurse3 = Recurse2; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}} 113 114extern const int RecurseA; 115const int RecurseB = RecurseA; // expected-note {{declared here}} 116const int RecurseA = 10; 117constexpr int RecurseC = RecurseB; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}} 118 119namespace MemberEnum { 120 struct WithMemberEnum { 121 enum E { A = 42 }; 122 } wme; 123 124 static_assert(wme.A == 42, ""); 125} 126 127namespace DefaultArguments { 128 129const int z = int(); 130constexpr int Sum(int a = 0, const int &b = 0, const int *c = &z, char d = 0) { 131 return a + b + *c + d; 132} 133const int four = 4; 134constexpr int eight = 8; 135constexpr const int twentyseven = 27; 136static_assert(Sum() == 0, ""); 137static_assert(Sum(1) == 1, ""); 138static_assert(Sum(1, four) == 5, ""); 139static_assert(Sum(1, eight, &twentyseven) == 36, ""); 140static_assert(Sum(1, 2, &four, eight) == 15, ""); 141 142} 143 144namespace Ellipsis { 145 146// Note, values passed through an ellipsis can't actually be used. 147constexpr int F(int a, ...) { return a; } 148static_assert(F(0) == 0, ""); 149static_assert(F(1, 0) == 1, ""); 150static_assert(F(2, "test") == 2, ""); 151static_assert(F(3, &F) == 3, ""); 152int k = 0; // expected-note {{here}} 153static_assert(F(4, k) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}} 154 155} 156 157namespace Recursion { 158 constexpr int fib(int n) { return n > 1 ? fib(n-1) + fib(n-2) : n; } 159 static_assert(fib(11) == 89, ""); 160 161 constexpr int gcd_inner(int a, int b) { 162 return b == 0 ? a : gcd_inner(b, a % b); 163 } 164 constexpr int gcd(int a, int b) { 165 return gcd_inner(max(a, b), min(a, b)); 166 } 167 168 static_assert(gcd(1749237, 5628959) == 7, ""); 169} 170 171namespace FunctionCast { 172 // When folding, we allow functions to be cast to different types. Such 173 // cast functions cannot be called, even if they're constexpr. 174 constexpr int f() { return 1; } 175 typedef double (*DoubleFn)(); 176 typedef int (*IntFn)(); 177 int a[(int)DoubleFn(f)()]; // expected-error {{variable length array}} expected-warning{{C99 feature}} 178 int b[(int)IntFn(f)()]; // ok 179} 180 181namespace StaticMemberFunction { 182 struct S { 183 static constexpr int k = 42; 184 static constexpr int f(int n) { return n * k + 2; } 185 } s; 186 187 constexpr int n = s.f(19); 188 static_assert(S::f(19) == 800, ""); 189 static_assert(s.f(19) == 800, ""); 190 static_assert(n == 800, ""); 191 192 constexpr int (*sf1)(int) = &S::f; 193 constexpr int (*sf2)(int) = &s.f; 194 constexpr const int *sk = &s.k; 195} 196 197namespace ParameterScopes { 198 199 const int k = 42; 200 constexpr const int &ObscureTheTruth(const int &a) { return a; } 201 constexpr const int &MaybeReturnJunk(bool b, const int a) { // expected-note 2{{declared here}} 202 return ObscureTheTruth(b ? a : k); 203 } 204 static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok 205 constexpr int a = MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}} 206 207 constexpr const int MaybeReturnNonstaticRef(bool b, const int a) { 208 return ObscureTheTruth(b ? a : k); 209 } 210 static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok 211 constexpr int b = MaybeReturnNonstaticRef(true, 0); // ok 212 213 constexpr int InternalReturnJunk(int n) { 214 return MaybeReturnJunk(true, n); // expected-note {{read of variable whose lifetime has ended}} 215 } 216 constexpr int n3 = InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}} 217 218 constexpr int LToR(int &n) { return n; } 219 constexpr int GrabCallersArgument(bool which, int a, int b) { 220 return LToR(which ? b : a); 221 } 222 static_assert(GrabCallersArgument(false, 1, 2) == 1, ""); 223 static_assert(GrabCallersArgument(true, 4, 8) == 8, ""); 224 225} 226 227namespace Pointers { 228 229 constexpr int f(int n, const int *a, const int *b, const int *c) { 230 return n == 0 ? 0 : *a + f(n-1, b, c, a); 231 } 232 233 const int x = 1, y = 10, z = 100; 234 static_assert(f(23, &x, &y, &z) == 788, ""); 235 236 constexpr int g(int n, int a, int b, int c) { 237 return f(n, &a, &b, &c); 238 } 239 static_assert(g(23, x, y, z) == 788, ""); 240 241} 242 243namespace FunctionPointers { 244 245 constexpr int Double(int n) { return 2 * n; } 246 constexpr int Triple(int n) { return 3 * n; } 247 constexpr int Twice(int (*F)(int), int n) { return F(F(n)); } 248 constexpr int Quadruple(int n) { return Twice(Double, n); } 249 constexpr auto Select(int n) -> int (*)(int) { 250 return n == 2 ? &Double : n == 3 ? &Triple : n == 4 ? &Quadruple : 0; 251 } 252 constexpr int Apply(int (*F)(int), int n) { return F(n); } // expected-note {{subexpression}} 253 254 static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, ""); 255 256 constexpr int Invalid = Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(0, 0)'}} 257 258} 259 260namespace PointerComparison { 261 262int x, y; 263static_assert(&x == &y, "false"); // expected-error {{false}} 264static_assert(&x != &y, ""); 265constexpr bool g1 = &x == &y; 266constexpr bool g2 = &x != &y; 267constexpr bool g3 = &x <= &y; // expected-error {{must be initialized by a constant expression}} 268constexpr bool g4 = &x >= &y; // expected-error {{must be initialized by a constant expression}} 269constexpr bool g5 = &x < &y; // expected-error {{must be initialized by a constant expression}} 270constexpr bool g6 = &x > &y; // expected-error {{must be initialized by a constant expression}} 271 272struct S { int x, y; } s; 273static_assert(&s.x == &s.y, "false"); // expected-error {{false}} 274static_assert(&s.x != &s.y, ""); 275static_assert(&s.x <= &s.y, ""); 276static_assert(&s.x >= &s.y, "false"); // expected-error {{false}} 277static_assert(&s.x < &s.y, ""); 278static_assert(&s.x > &s.y, "false"); // expected-error {{false}} 279 280static_assert(0 == &y, "false"); // expected-error {{false}} 281static_assert(0 != &y, ""); 282constexpr bool n3 = 0 <= &y; // expected-error {{must be initialized by a constant expression}} 283constexpr bool n4 = 0 >= &y; // expected-error {{must be initialized by a constant expression}} 284constexpr bool n5 = 0 < &y; // expected-error {{must be initialized by a constant expression}} 285constexpr bool n6 = 0 > &y; // expected-error {{must be initialized by a constant expression}} 286 287static_assert(&x == 0, "false"); // expected-error {{false}} 288static_assert(&x != 0, ""); 289constexpr bool n9 = &x <= 0; // expected-error {{must be initialized by a constant expression}} 290constexpr bool n10 = &x >= 0; // expected-error {{must be initialized by a constant expression}} 291constexpr bool n11 = &x < 0; // expected-error {{must be initialized by a constant expression}} 292constexpr bool n12 = &x > 0; // expected-error {{must be initialized by a constant expression}} 293 294static_assert(&x == &x, ""); 295static_assert(&x != &x, "false"); // expected-error {{false}} 296static_assert(&x <= &x, ""); 297static_assert(&x >= &x, ""); 298static_assert(&x < &x, "false"); // expected-error {{false}} 299static_assert(&x > &x, "false"); // expected-error {{false}} 300 301constexpr S* sptr = &s; 302constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr); // expected-error {{constant expression}} expected-note {{dynamic_cast}} 303 304struct U {}; 305struct Str { 306 int a : dynamic_cast<S*>(sptr) == dynamic_cast<S*>(sptr); // \ 307 expected-warning {{not an integral constant expression}} \ 308 expected-note {{dynamic_cast is not allowed in a constant expression}} 309 int b : reinterpret_cast<S*>(sptr) == reinterpret_cast<S*>(sptr); // \ 310 expected-warning {{not an integral constant expression}} \ 311 expected-note {{reinterpret_cast is not allowed in a constant expression}} 312 int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \ 313 expected-warning {{not an integral constant expression}} \ 314 expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 315 int d : (S*)(42) == (S*)(42); // \ 316 expected-warning {{not an integral constant expression}} \ 317 expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 318 int e : (Str*)(sptr) == (Str*)(sptr); // \ 319 expected-warning {{not an integral constant expression}} \ 320 expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 321 int f : &(U&)(*sptr) == &(U&)(*sptr); // \ 322 expected-warning {{not an integral constant expression}} \ 323 expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} 324 int g : (S*)(void*)(sptr) == sptr; // \ 325 expected-warning {{not an integral constant expression}} \ 326 expected-note {{cast from 'void *' is not allowed in a constant expression}} 327}; 328 329extern char externalvar[]; 330constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}} expected-note {{reinterpret_cast}} 331constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}} expected-warning {{unspecified}} 332static_assert(0 != "foo", ""); 333 334} 335 336namespace MaterializeTemporary { 337 338constexpr int f(const int &r) { return r; } 339constexpr int n = f(1); 340 341constexpr bool same(const int &a, const int &b) { return &a == &b; } 342constexpr bool sameTemporary(const int &n) { return same(n, n); } 343 344static_assert(n, ""); 345static_assert(!same(4, 4), ""); 346static_assert(same(n, n), ""); 347static_assert(sameTemporary(9), ""); 348 349struct A { int &&r; }; 350struct B { A &&a1; A &&a2; }; 351 352constexpr B b1 { { 1 }, { 2 } }; // expected-note {{temporary created here}} 353static_assert(&b1.a1 != &b1.a2, ""); 354static_assert(&b1.a1.r != &b1.a2.r, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}} 355 356constexpr B &&b2 { { 3 }, { 4 } }; // expected-note {{temporary created here}} 357static_assert(&b1 != &b2, ""); 358static_assert(&b1.a1 != &b2.a1, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}} 359 360constexpr thread_local B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} 361void foo() { 362 constexpr static B b1 { { 1 }, { 2 } }; // ok 363 constexpr thread_local B b2 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} 364 constexpr B b3 { { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} 365} 366 367constexpr B &&b4 = ((1, 2), 3, 4, B { {10}, {{20}} }); // expected-warning 4{{unused}} 368static_assert(&b4 != &b2, ""); 369 370// Proposed DR: copy-elision doesn't trigger lifetime extension. 371constexpr B b5 = B{ {0}, {0} }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} 372 373namespace NestedNonStatic { 374 // Proposed DR: for a reference constant expression to refer to a static 375 // storage duration temporary, that temporary must itself be initialized 376 // by a constant expression (a core constant expression is not enough). 377 struct A { int &&r; }; 378 struct B { A &&a; }; 379 constexpr B a = { A{0} }; // ok 380 constexpr B b = { A(A{0}) }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}} 381} 382 383namespace FakeInitList { 384 struct init_list_3_ints { const int (&x)[3]; }; 385 struct init_list_2_init_list_3_ints { const init_list_3_ints (&x)[2]; }; 386 constexpr init_list_2_init_list_3_ints ils = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } }; 387} 388 389} 390 391constexpr int strcmp_ce(const char *p, const char *q) { 392 return (!*p || *p != *q) ? *p - *q : strcmp_ce(p+1, q+1); 393} 394 395namespace StringLiteral { 396 397template<typename Char> 398constexpr int MangleChars(const Char *p) { 399 return *p + 3 * (*p ? MangleChars(p+1) : 0); 400} 401 402static_assert(MangleChars("constexpr!") == 1768383, ""); 403static_assert(MangleChars(u8"constexpr!") == 1768383, ""); 404static_assert(MangleChars(L"constexpr!") == 1768383, ""); 405static_assert(MangleChars(u"constexpr!") == 1768383, ""); 406static_assert(MangleChars(U"constexpr!") == 1768383, ""); 407 408constexpr char c0 = "nought index"[0]; 409constexpr char c1 = "nice index"[10]; 410constexpr char c2 = "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is past the end}} expected-note {{read of dereferenced one-past-the-end pointer}} 411constexpr char c3 = "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-warning {{is before the beginning}} expected-note {{cannot refer to element -1 of array of 15 elements}} 412constexpr char c4 = ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} expected-note {{cast that performs the conversions of a reinterpret_cast}} 413 414constexpr const char *p = "test" + 2; 415static_assert(*p == 's', ""); 416 417constexpr const char *max_iter(const char *a, const char *b) { 418 return *a < *b ? b : a; 419} 420constexpr const char *max_element(const char *a, const char *b) { 421 return (a+1 >= b) ? a : max_iter(a, max_element(a+1, b)); 422} 423 424constexpr char str[] = "the quick brown fox jumped over the lazy dog"; 425constexpr const char *max = max_element(begin(str), end(str)); 426static_assert(*max == 'z', ""); 427static_assert(max == str + 38, ""); 428 429static_assert(strcmp_ce("hello world", "hello world") == 0, ""); 430static_assert(strcmp_ce("hello world", "hello clang") > 0, ""); 431static_assert(strcmp_ce("constexpr", "test") < 0, ""); 432static_assert(strcmp_ce("", " ") < 0, ""); 433 434struct S { 435 int n : "foo"[4]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}} 436}; 437 438struct T { 439 char c[6]; 440 constexpr T() : c{"foo"} {} 441}; 442constexpr T t; 443 444static_assert(t.c[0] == 'f', ""); 445static_assert(t.c[1] == 'o', ""); 446static_assert(t.c[2] == 'o', ""); 447static_assert(t.c[3] == 0, ""); 448static_assert(t.c[4] == 0, ""); 449static_assert(t.c[5] == 0, ""); 450static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 451 452struct U { 453 wchar_t chars[6]; 454 int n; 455} constexpr u = { { L"test" }, 0 }; 456static_assert(u.chars[2] == L's', ""); 457 458struct V { 459 char c[4]; 460 constexpr V() : c("hi!") {} 461}; 462static_assert(V().c[1] == "i"[0], ""); 463 464namespace Parens { 465 constexpr unsigned char a[] = ("foo"), b[] = {"foo"}, c[] = {("foo")}, 466 d[4] = ("foo"), e[5] = {"foo"}, f[6] = {("foo")}; 467 static_assert(a[0] == 'f', ""); 468 static_assert(b[1] == 'o', ""); 469 static_assert(c[2] == 'o', ""); 470 static_assert(d[0] == 'f', ""); 471 static_assert(e[1] == 'o', ""); 472 static_assert(f[2] == 'o', ""); 473 static_assert(f[5] == 0, ""); 474 static_assert(f[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 475} 476 477} 478 479namespace Array { 480 481template<typename Iter> 482constexpr auto Sum(Iter begin, Iter end) -> decltype(+*begin) { 483 return begin == end ? 0 : *begin + Sum(begin+1, end); 484} 485 486constexpr int xs[] = { 1, 2, 3, 4, 5 }; 487constexpr int ys[] = { 5, 4, 3, 2, 1 }; 488constexpr int sum_xs = Sum(begin(xs), end(xs)); 489static_assert(sum_xs == 15, ""); 490 491constexpr int ZipFoldR(int (*F)(int x, int y, int c), int n, 492 const int *xs, const int *ys, int c) { 493 return n ? F( 494 *xs, // expected-note {{read of dereferenced one-past-the-end pointer}} 495 *ys, 496 ZipFoldR(F, n-1, xs+1, ys+1, c)) // \ 497 expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \ 498 expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}} 499 : c; 500} 501constexpr int MulAdd(int x, int y, int c) { return x * y + c; } 502constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0); 503static_assert(InnerProduct == 35, ""); 504 505constexpr int SubMul(int x, int y, int c) { return (x - y) * c; } 506constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1); 507static_assert(DiffProd == 8, ""); 508static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \ 509 expected-error {{constant expression}} \ 510 expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}} 511 512constexpr const int *p = xs + 3; 513constexpr int xs4 = p[1]; // ok 514constexpr int xs5 = p[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} 515constexpr int xs6 = p[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}} 516constexpr int xs0 = p[-3]; // ok 517constexpr int xs_1 = p[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} 518 519constexpr int zs[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; 520static_assert(zs[0][0][0][0] == 1, ""); 521static_assert(zs[1][1][1][1] == 16, ""); 522static_assert(zs[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} 523static_assert((&zs[0][0][0][2])[-1] == 2, ""); 524static_assert(**(**(zs + 1) + 1) == 11, ""); 525static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of array of 2 elements in a constant expression}} 526static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, ""); 527constexpr int err_zs_1_2_0_0 = zs[1][2][0][0]; // expected-error {{constant expression}} expected-note {{cannot access array element of pointer past the end}} 528 529constexpr int fail(const int &p) { 530 return (&p)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}} 531} 532static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \ 533expected-error {{static_assert expression is not an integral constant expression}} \ 534expected-note {{in call to 'fail(zs[1][0][1][0])'}} 535 536constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{C99 feature}} 537constexpr int SumNonzero(const int *p) { 538 return *p + (*p ? SumNonzero(p+1) : 0); 539} 540constexpr int CountZero(const int *p, const int *q) { 541 return p == q ? 0 : (*p == 0) + CountZero(p+1, q); 542} 543static_assert(SumNonzero(arr) == 6, ""); 544static_assert(CountZero(arr, arr + 40) == 36, ""); 545 546struct ArrayElem { 547 constexpr ArrayElem() : n(0) {} 548 int n; 549 constexpr int f() const { return n; } 550}; 551struct ArrayRVal { 552 constexpr ArrayRVal() {} 553 ArrayElem elems[10]; 554}; 555static_assert(ArrayRVal().elems[3].f() == 0, ""); 556 557constexpr int selfref[2][2][2] = { 558 selfref[1][1][1] + 1, selfref[0][0][0] + 1, 559 selfref[1][0][1] + 1, selfref[0][1][0] + 1, 560 selfref[1][0][0] + 1, selfref[0][1][1] + 1 }; 561static_assert(selfref[0][0][0] == 1, ""); 562static_assert(selfref[0][0][1] == 2, ""); 563static_assert(selfref[0][1][0] == 1, ""); 564static_assert(selfref[0][1][1] == 2, ""); 565static_assert(selfref[1][0][0] == 1, ""); 566static_assert(selfref[1][0][1] == 3, ""); 567static_assert(selfref[1][1][0] == 0, ""); 568static_assert(selfref[1][1][1] == 0, ""); 569 570struct TrivialDefCtor { int n; }; 571typedef TrivialDefCtor TDCArray[2][2]; 572static_assert(TDCArray{}[1][1].n == 0, ""); 573 574struct NonAggregateTDC : TrivialDefCtor {}; 575typedef NonAggregateTDC NATDCArray[2][2]; 576static_assert(NATDCArray{}[1][1].n == 0, ""); 577 578} 579 580namespace DependentValues { 581 582struct I { int n; typedef I V[10]; }; 583I::V x, y; 584int g(); 585template<bool B, typename T> struct S : T { 586 int k; 587 void f() { 588 I::V &cells = B ? x : y; 589 I &i = cells[k]; 590 switch (i.n) {} 591 592 // FIXME: We should be able to diagnose this. 593 constexpr int n = g(); 594 595 constexpr int m = this->g(); // ok, could be constexpr 596 } 597}; 598 599} 600 601namespace Class { 602 603struct A { constexpr A(int a, int b) : k(a + b) {} int k; }; 604constexpr int fn(const A &a) { return a.k; } 605static_assert(fn(A(4,5)) == 9, ""); 606 607struct B { int n; int m; } constexpr b = { 0, b.n }; 608struct C { 609 constexpr C(C *this_) : m(42), n(this_->m) {} // ok 610 int m, n; 611}; 612struct D { 613 C c; 614 constexpr D() : c(&c) {} 615}; 616static_assert(D().c.n == 42, ""); 617 618struct E { 619 constexpr E() : p(&p) {} 620 void *p; 621}; 622constexpr const E &e1 = E(); 623// This is a constant expression if we elide the copy constructor call, and 624// is not a constant expression if we don't! But we do, so it is. 625constexpr E e2 = E(); 626static_assert(e2.p == &e2.p, ""); 627constexpr E e3; 628static_assert(e3.p == &e3.p, ""); 629 630extern const class F f; 631struct F { 632 constexpr F() : p(&f.p) {} 633 const void *p; 634}; 635constexpr F f; 636 637struct G { 638 struct T { 639 constexpr T(T *p) : u1(), u2(p) {} 640 union U1 { 641 constexpr U1() {} 642 int a, b = 42; 643 } u1; 644 union U2 { 645 constexpr U2(T *p) : c(p->u1.b) {} 646 int c, d; 647 } u2; 648 } t; 649 constexpr G() : t(&t) {} 650} constexpr g; 651 652static_assert(g.t.u1.a == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}} 653static_assert(g.t.u1.b == 42, ""); 654static_assert(g.t.u2.c == 42, ""); 655static_assert(g.t.u2.d == 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}} 656 657struct S { 658 int a, b; 659 const S *p; 660 double d; 661 const char *q; 662 663 constexpr S(int n, const S *p) : a(5), b(n), p(p), d(n), q("hello") {} 664}; 665 666S global(43, &global); 667 668static_assert(S(15, &global).b == 15, ""); 669 670constexpr bool CheckS(const S &s) { 671 return s.a == 5 && s.b == 27 && s.p == &global && s.d == 27. && s.q[3] == 'l'; 672} 673static_assert(CheckS(S(27, &global)), ""); 674 675struct Arr { 676 char arr[3]; 677 constexpr Arr() : arr{'x', 'y', 'z'} {} 678}; 679constexpr int hash(Arr &&a) { 680 return a.arr[0] + a.arr[1] * 0x100 + a.arr[2] * 0x10000; 681} 682constexpr int k = hash(Arr()); 683static_assert(k == 0x007a7978, ""); 684 685 686struct AggregateInit { 687 const char &c; 688 int n; 689 double d; 690 int arr[5]; 691 void *p; 692}; 693 694constexpr AggregateInit agg1 = { "hello"[0] }; 695 696static_assert(strcmp_ce(&agg1.c, "hello") == 0, ""); 697static_assert(agg1.n == 0, ""); 698static_assert(agg1.d == 0.0, ""); 699static_assert(agg1.arr[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} 700static_assert(agg1.arr[0] == 0, ""); 701static_assert(agg1.arr[4] == 0, ""); 702static_assert(agg1.arr[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}} 703static_assert(agg1.p == nullptr, ""); 704 705static constexpr const unsigned char uc[] = { "foo" }; 706static_assert(uc[0] == 'f', ""); 707static_assert(uc[3] == 0, ""); 708 709namespace SimpleDerivedClass { 710 711struct B { 712 constexpr B(int n) : a(n) {} 713 int a; 714}; 715struct D : B { 716 constexpr D(int n) : B(n) {} 717}; 718constexpr D d(3); 719static_assert(d.a == 3, ""); 720 721} 722 723struct Bottom { constexpr Bottom() {} }; 724struct Base : Bottom { 725 constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {} 726 int a; 727 const char *b; 728}; 729struct Base2 : Bottom { 730 constexpr Base2(const int &r) : r(r) {} 731 int q = 123; 732 const int &r; 733}; 734struct Derived : Base, Base2 { 735 constexpr Derived() : Base(76), Base2(a) {} 736 int c = r + b[1]; 737}; 738 739constexpr bool operator==(const Base &a, const Base &b) { 740 return a.a == b.a && strcmp_ce(a.b, b.b) == 0; 741} 742 743constexpr Base base; 744constexpr Base base2(76); 745constexpr Derived derived; 746static_assert(derived.a == 76, ""); 747static_assert(derived.b[2] == 's', ""); 748static_assert(derived.c == 76 + 'e', ""); 749static_assert(derived.q == 123, ""); 750static_assert(derived.r == 76, ""); 751static_assert(&derived.r == &derived.a, ""); 752 753static_assert(!(derived == base), ""); 754static_assert(derived == base2, ""); 755 756constexpr Bottom &bot1 = (Base&)derived; 757constexpr Bottom &bot2 = (Base2&)derived; 758static_assert(&bot1 != &bot2, ""); 759 760constexpr Bottom *pb1 = (Base*)&derived; 761constexpr Bottom *pb2 = (Base2*)&derived; 762static_assert(&pb1 != &pb2, ""); 763static_assert(pb1 == &bot1, ""); 764static_assert(pb2 == &bot2, ""); 765 766constexpr Base2 &fail = (Base2&)bot1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}} 767constexpr Base &fail2 = (Base&)*pb2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}} 768constexpr Base2 &ok2 = (Base2&)bot2; 769static_assert(&ok2 == &derived, ""); 770 771constexpr Base2 *pfail = (Base2*)pb1; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base2'}} 772constexpr Base *pfail2 = (Base*)&bot2; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Class::Derived' to type 'Class::Base'}} 773constexpr Base2 *pok2 = (Base2*)pb2; 774static_assert(pok2 == &derived, ""); 775static_assert(&ok2 == pok2, ""); 776static_assert((Base2*)(Derived*)(Base*)pb1 == pok2, ""); 777static_assert((Derived*)(Base*)pb1 == (Derived*)pok2, ""); 778 779// Core issue 903: we do not perform constant evaluation when checking for a 780// null pointer in C++11. Just check for an integer literal with value 0. 781constexpr Base *nullB = 42 - 6 * 7; // expected-error {{cannot initialize a variable of type 'Class::Base *const' with an rvalue of type 'int'}} 782constexpr Base *nullB1 = 0; 783static_assert((Bottom*)nullB == 0, ""); 784static_assert((Derived*)nullB == 0, ""); 785static_assert((void*)(Bottom*)nullB == (void*)(Derived*)nullB, ""); 786Base *nullB2 = '\0'; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'char'}} 787Base *nullB3 = (0); 788Base *nullB4 = false; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'bool'}} 789Base *nullB5 = ((0ULL)); 790Base *nullB6 = 0.; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'double'}} 791enum Null { kNull }; 792Base *nullB7 = kNull; // expected-error {{cannot initialize a variable of type 'Class::Base *' with an rvalue of type 'Class::Null'}} 793static_assert(nullB1 == (1 - 1), ""); // expected-error {{comparison between pointer and integer}} 794 795 796 797namespace ConversionOperators { 798 799struct T { 800 constexpr T(int n) : k(5*n - 3) {} 801 constexpr operator int() const { return k; } 802 int k; 803}; 804 805struct S { 806 constexpr S(int n) : k(2*n + 1) {} 807 constexpr operator int() const { return k; } 808 constexpr operator T() const { return T(k); } 809 int k; 810}; 811 812constexpr bool check(T a, T b) { return a == b.k; } 813 814static_assert(S(5) == 11, ""); 815static_assert(check(S(5), 11), ""); 816 817namespace PR14171 { 818 819struct X { 820 constexpr (operator int)() const { return 0; } 821}; 822static_assert(X() == 0, ""); 823 824} 825 826} 827 828struct This { 829 constexpr int f() const { return 0; } 830 static constexpr int g() { return 0; } 831 void h() { 832 constexpr int x = f(); // expected-error {{must be initialized by a constant}} 833 // expected-note@-1 {{implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}} 834 constexpr int y = this->f(); // expected-error {{must be initialized by a constant}} 835 // expected-note-re@-1 {{{{^}}use of 'this' pointer}} 836 constexpr int z = g(); 837 static_assert(z == 0, ""); 838 } 839}; 840 841} 842 843namespace Temporaries { 844 845struct S { 846 constexpr S() {} 847 constexpr int f() const; 848 constexpr int g() const; 849}; 850struct T : S { 851 constexpr T(int n) : S(), n(n) {} 852 int n; 853}; 854constexpr int S::f() const { 855 return static_cast<const T*>(this)->n; // expected-note {{cannot cast}} 856} 857constexpr int S::g() const { 858 // FIXME: Better diagnostic for this. 859 return this->*(int(S::*))&T::n; // expected-note {{subexpression}} 860} 861// The T temporary is implicitly cast to an S subobject, but we can recover the 862// T full-object via a base-to-derived cast, or a derived-to-base-casted member 863// pointer. 864static_assert(S().f(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->f()'}} 865static_assert(S().g(), ""); // expected-error {{constant expression}} expected-note {{in call to '&Temporaries::S()->g()'}} 866static_assert(T(3).f() == 3, ""); 867static_assert(T(4).g() == 4, ""); 868 869constexpr int f(const S &s) { 870 return static_cast<const T&>(s).n; 871} 872constexpr int n = f(T(5)); 873static_assert(f(T(5)) == 5, ""); 874 875constexpr bool b(int n) { return &n; } 876static_assert(b(0), ""); 877 878struct NonLiteral { 879 NonLiteral(); 880 int f(); 881}; 882constexpr int k = NonLiteral().f(); // expected-error {{constant expression}} expected-note {{non-literal type 'Temporaries::NonLiteral'}} 883 884} 885 886namespace Union { 887 888union U { 889 int a; 890 int b; 891}; 892 893constexpr U u[4] = { { .a = 0 }, { .b = 1 }, { .a = 2 }, { .b = 3 } }; // expected-warning 4{{C99 feature}} 894static_assert(u[0].a == 0, ""); 895static_assert(u[0].b, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}} 896static_assert(u[1].b == 1, ""); 897static_assert((&u[1].b)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} 898static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}} 899static_assert((&(u[1]) + 1 + 1)->b == 3, ""); 900 901constexpr U v = {}; 902static_assert(v.a == 0, ""); 903 904union Empty {}; 905constexpr Empty e = {}; 906 907// Make sure we handle trivial copy constructors for unions. 908constexpr U x = {42}; 909constexpr U y = x; 910static_assert(y.a == 42, ""); 911static_assert(y.b == 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}} 912 913} 914 915namespace MemberPointer { 916 struct A { 917 constexpr A(int n) : n(n) {} 918 int n; 919 constexpr int f() const { return n + 3; } 920 }; 921 constexpr A a(7); 922 static_assert(A(5).*&A::n == 5, ""); 923 static_assert((&a)->*&A::n == 7, ""); 924 static_assert((A(8).*&A::f)() == 11, ""); 925 static_assert(((&a)->*&A::f)() == 10, ""); 926 927 struct B : A { 928 constexpr B(int n, int m) : A(n), m(m) {} 929 int m; 930 constexpr int g() const { return n + m + 1; } 931 }; 932 constexpr B b(9, 13); 933 static_assert(B(4, 11).*&A::n == 4, ""); 934 static_assert(B(4, 11).*&B::m == 11, ""); 935 static_assert(B(4, 11).*(int(A::*))&B::m == 11, ""); 936 static_assert((&b)->*&A::n == 9, ""); 937 static_assert((&b)->*&B::m == 13, ""); 938 static_assert((&b)->*(int(A::*))&B::m == 13, ""); 939 static_assert((B(4, 11).*&A::f)() == 7, ""); 940 static_assert((B(4, 11).*&B::g)() == 16, ""); 941 static_assert((B(4, 11).*(int(A::*)()const)&B::g)() == 16, ""); 942 static_assert(((&b)->*&A::f)() == 12, ""); 943 static_assert(((&b)->*&B::g)() == 23, ""); 944 static_assert(((&b)->*(int(A::*)()const)&B::g)() == 23, ""); 945 946 struct S { 947 constexpr S(int m, int n, int (S::*pf)() const, int S::*pn) : 948 m(m), n(n), pf(pf), pn(pn) {} 949 constexpr S() : m(), n(), pf(&S::f), pn(&S::n) {} 950 951 constexpr int f() const { return this->*pn; } 952 virtual int g() const; 953 954 int m, n; 955 int (S::*pf)() const; 956 int S::*pn; 957 }; 958 959 constexpr int S::*pm = &S::m; 960 constexpr int S::*pn = &S::n; 961 constexpr int (S::*pf)() const = &S::f; 962 constexpr int (S::*pg)() const = &S::g; 963 964 constexpr S s(2, 5, &S::f, &S::m); 965 966 static_assert((s.*&S::f)() == 2, ""); 967 static_assert((s.*s.pf)() == 2, ""); 968 969 static_assert(pf == &S::f, ""); 970 static_assert(pf == s.*&S::pf, ""); 971 static_assert(pm == &S::m, ""); 972 static_assert(pm != pn, ""); 973 static_assert(s.pn != pn, ""); 974 static_assert(s.pn == pm, ""); 975 static_assert(pg != nullptr, ""); 976 static_assert(pf != nullptr, ""); 977 static_assert((int S::*)nullptr == nullptr, ""); 978 static_assert(pg == pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}} 979 static_assert(pf != pg, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}} 980 981 template<int n> struct T : T<n-1> {}; 982 template<> struct T<0> { int n; }; 983 template<> struct T<30> : T<29> { int m; }; 984 985 T<17> t17; 986 T<30> t30; 987 988 constexpr int (T<10>::*deepn) = &T<0>::n; 989 static_assert(&(t17.*deepn) == &t17.n, ""); 990 static_assert(deepn == &T<2>::n, ""); 991 992 constexpr int (T<15>::*deepm) = (int(T<10>::*))&T<30>::m; 993 constexpr int *pbad = &(t17.*deepm); // expected-error {{constant expression}} 994 static_assert(&(t30.*deepm) == &t30.m, ""); 995 static_assert(deepm == &T<50>::m, ""); 996 static_assert(deepm != deepn, ""); 997 998 constexpr T<5> *p17_5 = &t17; 999 constexpr T<13> *p17_13 = (T<13>*)p17_5; 1000 constexpr T<23> *p17_23 = (T<23>*)p17_13; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'T<17>' to type 'T<23>'}} 1001 static_assert(&(p17_5->*(int(T<3>::*))deepn) == &t17.n, ""); 1002 static_assert(&(p17_13->*deepn) == &t17.n, ""); 1003 constexpr int *pbad2 = &(p17_13->*(int(T<9>::*))deepm); // expected-error {{constant expression}} 1004 1005 constexpr T<5> *p30_5 = &t30; 1006 constexpr T<23> *p30_23 = (T<23>*)p30_5; 1007 constexpr T<13> *p30_13 = p30_23; 1008 static_assert(&(p30_5->*(int(T<3>::*))deepn) == &t30.n, ""); 1009 static_assert(&(p30_13->*deepn) == &t30.n, ""); 1010 static_assert(&(p30_23->*deepn) == &t30.n, ""); 1011 static_assert(&(p30_5->*(int(T<2>::*))deepm) == &t30.m, ""); 1012 static_assert(&(((T<17>*)p30_13)->*deepm) == &t30.m, ""); 1013 static_assert(&(p30_23->*deepm) == &t30.m, ""); 1014 1015 struct Base { int n; }; 1016 template<int N> struct Mid : Base {}; 1017 struct Derived : Mid<0>, Mid<1> {}; 1018 static_assert(&Mid<0>::n == &Mid<1>::n, ""); 1019 static_assert((int Derived::*)(int Mid<0>::*)&Mid<0>::n != 1020 (int Derived::*)(int Mid<1>::*)&Mid<1>::n, ""); 1021 static_assert(&Mid<0>::n == (int Mid<0>::*)&Base::n, ""); 1022} 1023 1024namespace ArrayBaseDerived { 1025 1026 struct Base { 1027 constexpr Base() {} 1028 int n = 0; 1029 }; 1030 struct Derived : Base { 1031 constexpr Derived() {} 1032 constexpr const int *f() const { return &n; } 1033 }; 1034 1035 constexpr Derived a[10]; 1036 constexpr Derived *pd3 = const_cast<Derived*>(&a[3]); 1037 constexpr Base *pb3 = const_cast<Derived*>(&a[3]); 1038 static_assert(pb3 == pd3, ""); 1039 1040 // pb3 does not point to an array element. 1041 constexpr Base *pb4 = pb3 + 1; // ok, one-past-the-end pointer. 1042 constexpr int pb4n = pb4->n; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}} 1043 constexpr Base *err_pb5 = pb3 + 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}} 1044 constexpr int err_pb5n = err_pb5->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}} 1045 constexpr Base *err_pb2 = pb3 - 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}} 1046 constexpr int err_pb2n = err_pb2->n; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}} 1047 constexpr Base *pb3a = pb4 - 1; 1048 1049 // pb4 does not point to a Derived. 1050 constexpr Derived *err_pd4 = (Derived*)pb4; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}} 1051 constexpr Derived *pd3a = (Derived*)pb3a; 1052 constexpr int pd3n = pd3a->n; 1053 1054 // pd3a still points to the Derived array. 1055 constexpr Derived *pd6 = pd3a + 3; 1056 static_assert(pd6 == &a[6], ""); 1057 constexpr Derived *pd9 = pd6 + 3; 1058 constexpr Derived *pd10 = pd6 + 4; 1059 constexpr int pd9n = pd9->n; // ok 1060 constexpr int err_pd10n = pd10->n; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}} 1061 constexpr int pd0n = pd10[-10].n; 1062 constexpr int err_pdminus1n = pd10[-11].n; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}} 1063 1064 constexpr Base *pb9 = pd9; 1065 constexpr const int *(Base::*pfb)() const = 1066 static_cast<const int *(Base::*)() const>(&Derived::f); 1067 static_assert((pb9->*pfb)() == &a[9].n, ""); 1068} 1069 1070namespace Complex { 1071 1072class complex { 1073 int re, im; 1074public: 1075 constexpr complex(int re = 0, int im = 0) : re(re), im(im) {} 1076 constexpr complex(const complex &o) : re(o.re), im(o.im) {} 1077 constexpr complex operator-() const { return complex(-re, -im); } 1078 friend constexpr complex operator+(const complex &l, const complex &r) { 1079 return complex(l.re + r.re, l.im + r.im); 1080 } 1081 friend constexpr complex operator-(const complex &l, const complex &r) { 1082 return l + -r; 1083 } 1084 friend constexpr complex operator*(const complex &l, const complex &r) { 1085 return complex(l.re * r.re - l.im * r.im, l.re * r.im + l.im * r.re); 1086 } 1087 friend constexpr bool operator==(const complex &l, const complex &r) { 1088 return l.re == r.re && l.im == r.im; 1089 } 1090 constexpr bool operator!=(const complex &r) const { 1091 return re != r.re || im != r.im; 1092 } 1093 constexpr int real() const { return re; } 1094 constexpr int imag() const { return im; } 1095}; 1096 1097constexpr complex i = complex(0, 1); 1098constexpr complex k = (3 + 4*i) * (6 - 4*i); 1099static_assert(complex(1,0).real() == 1, ""); 1100static_assert(complex(1,0).imag() == 0, ""); 1101static_assert(((complex)1).imag() == 0, ""); 1102static_assert(k.real() == 34, ""); 1103static_assert(k.imag() == 12, ""); 1104static_assert(k - 34 == 12*i, ""); 1105static_assert((complex)1 == complex(1), ""); 1106static_assert((complex)1 != complex(0, 1), ""); 1107static_assert(complex(1) == complex(1), ""); 1108static_assert(complex(1) != complex(0, 1), ""); 1109constexpr complex makeComplex(int re, int im) { return complex(re, im); } 1110static_assert(makeComplex(1,0) == complex(1), ""); 1111static_assert(makeComplex(1,0) != complex(0, 1), ""); 1112 1113class complex_wrap : public complex { 1114public: 1115 constexpr complex_wrap(int re, int im = 0) : complex(re, im) {} 1116 constexpr complex_wrap(const complex_wrap &o) : complex(o) {} 1117}; 1118 1119static_assert((complex_wrap)1 == complex(1), ""); 1120static_assert((complex)1 != complex_wrap(0, 1), ""); 1121static_assert(complex(1) == complex_wrap(1), ""); 1122static_assert(complex_wrap(1) != complex(0, 1), ""); 1123constexpr complex_wrap makeComplexWrap(int re, int im) { 1124 return complex_wrap(re, im); 1125} 1126static_assert(makeComplexWrap(1,0) == complex(1), ""); 1127static_assert(makeComplexWrap(1,0) != complex(0, 1), ""); 1128 1129} 1130 1131namespace PR11595 { 1132 struct A { constexpr bool operator==(int x) const { return true; } }; 1133 struct B { B(); A& x; }; 1134 static_assert(B().x == 3, ""); // expected-error {{constant expression}} expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}} 1135 1136 constexpr bool f(int k) { // expected-error {{constexpr function never produces a constant expression}} 1137 return B().x == k; // expected-note {{non-literal type 'PR11595::B' cannot be used in a constant expression}} 1138 } 1139} 1140 1141namespace ExprWithCleanups { 1142 struct A { A(); ~A(); int get(); }; 1143 constexpr int get(bool FromA) { return FromA ? A().get() : 1; } 1144 constexpr int n = get(false); 1145} 1146 1147namespace Volatile { 1148 1149volatile constexpr int n1 = 0; // expected-note {{here}} 1150volatile const int n2 = 0; // expected-note {{here}} 1151int n3 = 37; // expected-note {{declared here}} 1152 1153constexpr int m1 = n1; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}} 1154constexpr int m2 = n2; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}} 1155constexpr int m1b = const_cast<const int&>(n1); // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}} 1156constexpr int m2b = const_cast<const int&>(n2); // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}} 1157 1158struct T { int n; }; 1159const T t = { 42 }; // expected-note {{declared here}} 1160 1161constexpr int f(volatile int &&r) { 1162 return r; // expected-note {{read of volatile-qualified type 'volatile int'}} 1163} 1164constexpr int g(volatile int &&r) { 1165 return const_cast<int&>(r); // expected-note {{read of volatile temporary is not allowed in a constant expression}} 1166} 1167struct S { 1168 int j : f(0); // expected-error {{constant expression}} expected-note {{in call to 'f(0)'}} 1169 int k : g(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'g(0)'}} 1170 int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}} 1171 int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}} 1172}; 1173 1174} 1175 1176namespace ExternConstexpr { 1177 extern constexpr int n = 0; 1178 extern constexpr int m; // expected-error {{constexpr variable declaration must be a definition}} 1179 void f() { 1180 extern constexpr int i; // expected-error {{constexpr variable declaration must be a definition}} 1181 constexpr int j = 0; 1182 constexpr int k; // expected-error {{default initialization of an object of const type}} 1183 } 1184} 1185 1186namespace ComplexConstexpr { 1187 constexpr _Complex float test1 = {}; 1188 constexpr _Complex float test2 = {1}; 1189 constexpr _Complex double test3 = {1,2}; 1190 constexpr _Complex int test4 = {4}; 1191 constexpr _Complex int test5 = 4; 1192 constexpr _Complex int test6 = {5,6}; 1193 typedef _Complex float fcomplex; 1194 constexpr fcomplex test7 = fcomplex(); 1195 1196 constexpr const double &t2r = __real test3; 1197 constexpr const double &t2i = __imag test3; 1198 static_assert(&t2r + 1 == &t2i, ""); 1199 static_assert(t2r == 1.0, ""); 1200 static_assert(t2i == 2.0, ""); 1201 constexpr const double *t2p = &t2r; 1202 static_assert(t2p[-1] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element -1 of array of 2 elements}} 1203 static_assert(t2p[0] == 1.0, ""); 1204 static_assert(t2p[1] == 2.0, ""); 1205 static_assert(t2p[2] == 0.0, ""); // expected-error {{constant expr}} expected-note {{one-past-the-end pointer}} 1206 static_assert(t2p[3] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element 3 of array of 2 elements}} 1207 constexpr _Complex float *p = 0; 1208 constexpr float pr = __real *p; // expected-error {{constant expr}} expected-note {{cannot access real component of null}} 1209 constexpr float pi = __imag *p; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of null}} 1210 constexpr const _Complex double *q = &test3 + 1; 1211 constexpr double qr = __real *q; // expected-error {{constant expr}} expected-note {{cannot access real component of pointer past the end}} 1212 constexpr double qi = __imag *q; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of pointer past the end}} 1213 1214 static_assert(__real test6 == 5, ""); 1215 static_assert(__imag test6 == 6, ""); 1216 static_assert(&__imag test6 == &__real test6 + 1, ""); 1217} 1218 1219// _Atomic(T) is exactly like T for the purposes of constant expression 1220// evaluation.. 1221namespace Atomic { 1222 constexpr _Atomic int n = 3; 1223 1224 struct S { _Atomic(double) d; }; 1225 constexpr S s = { 0.5 }; 1226 constexpr double d1 = s.d; 1227 constexpr double d2 = n; 1228 constexpr _Atomic double d3 = n; 1229 1230 constexpr _Atomic(int) n2 = d3; 1231 static_assert(d1 == 0.5, ""); 1232 static_assert(d3 == 3.0, ""); 1233 1234 namespace PR16056 { 1235 struct TestVar { 1236 _Atomic(int) value; 1237 constexpr TestVar(int value) : value(value) {} 1238 }; 1239 constexpr TestVar testVar{-1}; 1240 static_assert(testVar.value == -1, ""); 1241 } 1242} 1243 1244namespace InstantiateCaseStmt { 1245 template<int x> constexpr int f() { return x; } 1246 template<int x> int g(int c) { switch(c) { case f<x>(): return 1; } return 0; } 1247 int gg(int c) { return g<4>(c); } 1248} 1249 1250namespace ConvertedConstantExpr { 1251 extern int &m; 1252 extern int &n; 1253 1254 constexpr int k = 4; 1255 int &m = const_cast<int&>(k); 1256 1257 // If we have nothing more interesting to say, ensure we don't produce a 1258 // useless note and instead just point to the non-constant subexpression. 1259 enum class E { 1260 em = m, 1261 en = n, // expected-error {{not a constant expression}} 1262 eo = (m + 1263 n // expected-error {{not a constant expression}} 1264 ), 1265 eq = reinterpret_cast<int>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}} 1266 }; 1267} 1268 1269namespace IndirectField { 1270 struct S { 1271 struct { // expected-warning {{GNU extension}} 1272 union { // expected-warning {{declared in an anonymous struct}} 1273 struct { // expected-warning {{GNU extension}} expected-warning {{declared in an anonymous union}} 1274 int a; 1275 int b; 1276 }; 1277 int c; 1278 }; 1279 int d; 1280 }; 1281 union { 1282 int e; 1283 int f; 1284 }; 1285 constexpr S(int a, int b, int d, int e) : a(a), b(b), d(d), e(e) {} 1286 constexpr S(int c, int d, int f) : c(c), d(d), f(f) {} 1287 }; 1288 1289 constexpr S s1(1, 2, 3, 4); 1290 constexpr S s2(5, 6, 7); 1291 1292 // FIXME: The diagnostics here do a very poor job of explaining which unnamed 1293 // member is active and which is requested. 1294 static_assert(s1.a == 1, ""); 1295 static_assert(s1.b == 2, ""); 1296 static_assert(s1.c == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} 1297 static_assert(s1.d == 3, ""); 1298 static_assert(s1.e == 4, ""); 1299 static_assert(s1.f == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} 1300 1301 static_assert(s2.a == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} 1302 static_assert(s2.b == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} 1303 static_assert(s2.c == 5, ""); 1304 static_assert(s2.d == 6, ""); 1305 static_assert(s2.e == 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}} 1306 static_assert(s2.f == 7, ""); 1307} 1308 1309// DR1405: don't allow reading mutable members in constant expressions. 1310namespace MutableMembers { 1311 struct MM { 1312 mutable int n; // expected-note 3{{declared here}} 1313 } constexpr mm = { 4 }; 1314 constexpr int mmn = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}} 1315 int x = (mm.n = 1, 3); 1316 constexpr int mmn2 = mm.n; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}} 1317 1318 // Here's one reason why allowing this would be a disaster... 1319 template<int n> struct Id { int k = n; }; 1320 int f() { 1321 constexpr MM m = { 0 }; 1322 ++m.n; 1323 return Id<m.n>().k; // expected-error {{not a constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}} 1324 } 1325 1326 struct A { int n; }; 1327 struct B { mutable A a; }; // expected-note {{here}} 1328 struct C { B b; }; 1329 constexpr C c[3] = {}; 1330 constexpr int k = c[1].b.a.n; // expected-error {{constant expression}} expected-note {{mutable}} 1331 1332 struct D { int x; mutable int y; }; // expected-note {{here}} 1333 constexpr D d1 = { 1, 2 }; 1334 int l = ++d1.y; 1335 constexpr D d2 = d1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}} 1336 1337 struct E { 1338 union { 1339 int a; 1340 mutable int b; // expected-note {{here}} 1341 }; 1342 }; 1343 constexpr E e1 = {{1}}; 1344 constexpr E e2 = e1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}} 1345 1346 struct F { 1347 union U { }; 1348 mutable U u; 1349 struct X { }; 1350 mutable X x; 1351 struct Y : X { X x; U u; }; 1352 mutable Y y; 1353 int n; 1354 }; 1355 // This is OK; we don't actually read any mutable state here. 1356 constexpr F f1 = {}; 1357 constexpr F f2 = f1; 1358 1359 struct G { 1360 struct X {}; 1361 union U { X a; }; 1362 mutable U u; // expected-note {{here}} 1363 }; 1364 constexpr G g1 = {}; 1365 constexpr G g2 = g1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}} 1366 constexpr G::U gu1 = {}; 1367 constexpr G::U gu2 = gu1; 1368 1369 union H { 1370 mutable G::X gx; // expected-note {{here}} 1371 }; 1372 constexpr H h1 = {}; 1373 constexpr H h2 = h1; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}} 1374} 1375 1376namespace Fold { 1377 1378 // This macro forces its argument to be constant-folded, even if it's not 1379 // otherwise a constant expression. 1380 #define fold(x) (__builtin_constant_p(x) ? (x) : (x)) 1381 1382 constexpr int n = (int)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}} 1383 constexpr int m = fold((int)(char*)123); // ok 1384 static_assert(m == 123, ""); 1385 1386 #undef fold 1387 1388} 1389 1390namespace DR1454 { 1391 1392constexpr const int &f(const int &n) { return n; } 1393constexpr int k1 = f(0); // ok 1394 1395struct Wrap { 1396 const int &value; 1397}; 1398constexpr const Wrap &g(const Wrap &w) { return w; } 1399constexpr int k2 = g({0}).value; // ok 1400 1401// The temporary here has static storage duration, so we can bind a constexpr 1402// reference to it. 1403constexpr const int &i = 1; 1404constexpr const int j = i; 1405static_assert(j == 1, ""); 1406 1407// The temporary here is not const, so it can't be read outside the expression 1408// in which it was created (per the C++14 rules, which we use to avoid a C++11 1409// defect). 1410constexpr int &&k = 1; // expected-note {{temporary created here}} 1411constexpr const int l = k; // expected-error {{constant expression}} expected-note {{read of temporary}} 1412 1413void f() { 1414 // The temporary here has automatic storage duration, so we can't bind a 1415 // constexpr reference to it. 1416 constexpr const int &i = 1; // expected-error {{constant expression}} expected-note 2{{temporary}} 1417} 1418 1419} 1420 1421namespace RecursiveOpaqueExpr { 1422 template<typename Iter> 1423 constexpr auto LastNonzero(Iter p, Iter q) -> decltype(+*p) { 1424 return p != q ? (LastNonzero(p+1, q) ?: *p) : 0; // expected-warning {{GNU}} 1425 } 1426 1427 constexpr int arr1[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 0 }; 1428 static_assert(LastNonzero(begin(arr1), end(arr1)) == 4, ""); 1429 1430 constexpr int arr2[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 5 }; 1431 static_assert(LastNonzero(begin(arr2), end(arr2)) == 5, ""); 1432 1433 constexpr int arr3[] = { 1434 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1435 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1436 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1437 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1438 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1439 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1440 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1441 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 1442 static_assert(LastNonzero(begin(arr3), end(arr3)) == 2, ""); 1443} 1444 1445namespace VLASizeof { 1446 1447 void f(int k) { 1448 int arr[k]; // expected-warning {{C99}} 1449 constexpr int n = 1 + 1450 sizeof(arr) // expected-error {{constant expression}} 1451 * 3; 1452 } 1453} 1454 1455namespace CompoundLiteral { 1456 // FIXME: 1457 // We don't model the semantics of this correctly: the compound literal is 1458 // represented as a prvalue in the AST, but actually behaves like an lvalue. 1459 // We treat the compound literal as a temporary and refuse to produce a 1460 // pointer to it. This is OK: we're not required to treat this as a constant 1461 // in C++, and in C we model compound literals as lvalues. 1462 constexpr int *p = (int*)(int[1]){0}; // expected-warning {{C99}} expected-error {{constant expression}} expected-note 2{{temporary}} 1463} 1464 1465namespace Vector { 1466 typedef int __attribute__((vector_size(16))) VI4; 1467 constexpr VI4 f(int n) { 1468 return VI4 { n * 3, n + 4, n - 5, n / 6 }; 1469 } 1470 constexpr auto v1 = f(10); 1471 1472 typedef double __attribute__((vector_size(32))) VD4; 1473 constexpr VD4 g(int n) { 1474 return (VD4) { n / 2.0, n + 1.5, n - 5.4, n * 0.9 }; // expected-warning {{C99}} 1475 } 1476 constexpr auto v2 = g(4); 1477} 1478 1479// PR12626, redux 1480namespace InvalidClasses { 1481 void test0() { 1482 struct X; // expected-note {{forward declaration}} 1483 struct Y { bool b; X x; }; // expected-error {{field has incomplete type}} 1484 Y y; 1485 auto& b = y.b; 1486 } 1487} 1488 1489namespace NamespaceAlias { 1490 constexpr int f() { 1491 namespace NS = NamespaceAlias; // expected-warning {{use of this statement in a constexpr function is a C++14 extension}} 1492 return &NS::f != nullptr; 1493 } 1494} 1495 1496// Constructors can be implicitly constexpr, even for a non-literal type. 1497namespace ImplicitConstexpr { 1498 struct Q { Q() = default; Q(const Q&) = default; Q(Q&&) = default; ~Q(); }; // expected-note 3{{here}} 1499 struct R { constexpr R() noexcept; constexpr R(const R&) noexcept; constexpr R(R&&) noexcept; ~R() noexcept; }; 1500 struct S { R r; }; // expected-note 3{{here}} 1501 struct T { T(const T&) noexcept; T(T &&) noexcept; ~T() noexcept; }; 1502 struct U { T t; }; // expected-note 3{{here}} 1503 static_assert(!__is_literal_type(Q), ""); 1504 static_assert(!__is_literal_type(R), ""); 1505 static_assert(!__is_literal_type(S), ""); 1506 static_assert(!__is_literal_type(T), ""); 1507 static_assert(!__is_literal_type(U), ""); 1508 struct Test { 1509 friend Q::Q() noexcept; // expected-error {{follows constexpr}} 1510 friend Q::Q(Q&&) noexcept; // expected-error {{follows constexpr}} 1511 friend Q::Q(const Q&) noexcept; // expected-error {{follows constexpr}} 1512 friend S::S() noexcept; // expected-error {{follows constexpr}} 1513 friend S::S(S&&) noexcept; // expected-error {{follows constexpr}} 1514 friend S::S(const S&) noexcept; // expected-error {{follows constexpr}} 1515 friend constexpr U::U() noexcept; // expected-error {{follows non-constexpr}} 1516 friend constexpr U::U(U&&) noexcept; // expected-error {{follows non-constexpr}} 1517 friend constexpr U::U(const U&) noexcept; // expected-error {{follows non-constexpr}} 1518 }; 1519} 1520 1521// Indirectly test that an implicit lvalue to xvalue conversion performed for 1522// an NRVO move operation isn't implemented as CK_LValueToRValue. 1523namespace PR12826 { 1524 struct Foo {}; 1525 constexpr Foo id(Foo x) { return x; } 1526 constexpr Foo res(id(Foo())); 1527} 1528 1529namespace PR13273 { 1530 struct U { 1531 int t; 1532 U() = default; 1533 }; 1534 1535 struct S : U { 1536 S() = default; 1537 }; 1538 1539 // S's default constructor isn't constexpr, because U's default constructor 1540 // doesn't initialize 't', but it's trivial, so value-initialization doesn't 1541 // actually call it. 1542 static_assert(S{}.t == 0, ""); 1543} 1544 1545namespace PR12670 { 1546 struct S { 1547 constexpr S(int a0) : m(a0) {} 1548 constexpr S() : m(6) {} 1549 int m; 1550 }; 1551 constexpr S x[3] = { {4}, 5 }; 1552 static_assert(x[0].m == 4, ""); 1553 static_assert(x[1].m == 5, ""); 1554 static_assert(x[2].m == 6, ""); 1555} 1556 1557// Indirectly test that an implicit lvalue-to-rvalue conversion is performed 1558// when a conditional operator has one argument of type void and where the other 1559// is a glvalue of class type. 1560namespace ConditionalLValToRVal { 1561 struct A { 1562 constexpr A(int a) : v(a) {} 1563 int v; 1564 }; 1565 1566 constexpr A f(const A &a) { 1567 return a.v == 0 ? throw a : a; 1568 } 1569 1570 constexpr A a(4); 1571 static_assert(f(a).v == 4, ""); 1572} 1573 1574namespace TLS { 1575 __thread int n; 1576 int m; 1577 1578 constexpr bool b = &n == &n; 1579 1580 constexpr int *p = &n; // expected-error{{constexpr variable 'p' must be initialized by a constant expression}} 1581 1582 constexpr int *f() { return &n; } 1583 constexpr int *q = f(); // expected-error{{constexpr variable 'q' must be initialized by a constant expression}} 1584 constexpr bool c = f() == f(); 1585 1586 constexpr int *g() { return &m; } 1587 constexpr int *r = g(); 1588} 1589 1590namespace Void { 1591 constexpr void f() { return; } // expected-error{{constexpr function's return type 'void' is not a literal type}} 1592 1593 void assert_failed(const char *msg, const char *file, int line); // expected-note {{declared here}} 1594#define ASSERT(expr) ((expr) ? static_cast<void>(0) : assert_failed(#expr, __FILE__, __LINE__)) 1595 template<typename T, size_t S> 1596 constexpr T get(T (&a)[S], size_t k) { 1597 return ASSERT(k > 0 && k < S), a[k]; // expected-note{{non-constexpr function 'assert_failed'}} 1598 } 1599#undef ASSERT 1600 template int get(int (&a)[4], size_t); 1601 constexpr int arr[] = { 4, 1, 2, 3, 4 }; 1602 static_assert(get(arr, 1) == 1, ""); 1603 static_assert(get(arr, 4) == 4, ""); 1604 static_assert(get(arr, 0) == 4, ""); // expected-error{{not an integral constant expression}} \ 1605 // expected-note{{in call to 'get(arr, 0)'}} 1606} 1607 1608namespace std { struct type_info; } 1609 1610namespace TypeId { 1611 struct A { virtual ~A(); }; 1612 A f(); 1613 A &g(); 1614 constexpr auto &x = typeid(f()); 1615 constexpr auto &y = typeid(g()); // expected-error{{constant expression}} \ 1616 // expected-note{{typeid applied to expression of polymorphic type 'TypeId::A' is not allowed in a constant expression}} \ 1617 // expected-warning {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}} 1618} 1619 1620namespace PR14203 { 1621 struct duration { 1622 constexpr duration() {} 1623 constexpr operator int() const { return 0; } 1624 }; 1625 template<typename T> void f() { 1626 // If we want to evaluate this at the point of the template definition, we 1627 // need to trigger the implicit definition of the move constructor at that 1628 // point. 1629 // FIXME: C++ does not permit us to implicitly define it at the appropriate 1630 // times, since it is only allowed to be implicitly defined when it is 1631 // odr-used. 1632 constexpr duration d = duration(); 1633 } 1634 // FIXME: It's unclear whether this is valid. On the one hand, we're not 1635 // allowed to generate a move constructor. On the other hand, if we did, 1636 // this would be a constant expression. For now, we generate a move 1637 // constructor here. 1638 int n = sizeof(short{duration(duration())}); 1639} 1640 1641namespace ArrayEltInit { 1642 struct A { 1643 constexpr A() : p(&p) {} 1644 void *p; 1645 }; 1646 constexpr A a[10]; 1647 static_assert(a[0].p == &a[0].p, ""); 1648 static_assert(a[9].p == &a[9].p, ""); 1649 static_assert(a[0].p != &a[9].p, ""); 1650 static_assert(a[9].p != &a[0].p, ""); 1651 1652 constexpr A b[10] = {}; 1653 static_assert(b[0].p == &b[0].p, ""); 1654 static_assert(b[9].p == &b[9].p, ""); 1655 static_assert(b[0].p != &b[9].p, ""); 1656 static_assert(b[9].p != &b[0].p, ""); 1657} 1658 1659namespace PR15884 { 1660 struct S {}; 1661 constexpr S f() { return {}; } 1662 constexpr S *p = &f(); 1663 // expected-error@-1 {{taking the address of a temporary}} 1664 // expected-error@-2 {{constexpr variable 'p' must be initialized by a constant expression}} 1665 // expected-note@-3 {{pointer to temporary is not a constant expression}} 1666 // expected-note@-4 {{temporary created here}} 1667} 1668 1669namespace AfterError { 1670 // FIXME: Suppress the 'no return statements' diagnostic if the body is invalid. 1671 constexpr int error() { // expected-error {{no return statement}} 1672 return foobar; // expected-error {{undeclared identifier}} 1673 } 1674 constexpr int k = error(); // expected-error {{must be initialized by a constant expression}} 1675} 1676 1677namespace std { 1678 typedef decltype(sizeof(int)) size_t; 1679 1680 template <class _E> 1681 class initializer_list 1682 { 1683 const _E* __begin_; 1684 size_t __size_; 1685 1686 constexpr initializer_list(const _E* __b, size_t __s) 1687 : __begin_(__b), 1688 __size_(__s) 1689 {} 1690 1691 public: 1692 typedef _E value_type; 1693 typedef const _E& reference; 1694 typedef const _E& const_reference; 1695 typedef size_t size_type; 1696 1697 typedef const _E* iterator; 1698 typedef const _E* const_iterator; 1699 1700 constexpr initializer_list() : __begin_(nullptr), __size_(0) {} 1701 1702 constexpr size_t size() const {return __size_;} 1703 constexpr const _E* begin() const {return __begin_;} 1704 constexpr const _E* end() const {return __begin_ + __size_;} 1705 }; 1706} 1707 1708namespace InitializerList { 1709 constexpr int sum(const int *b, const int *e) { 1710 return b != e ? *b + sum(b+1, e) : 0; 1711 } 1712 constexpr int sum(std::initializer_list<int> ints) { 1713 return sum(ints.begin(), ints.end()); 1714 } 1715 static_assert(sum({1, 2, 3, 4, 5}) == 15, ""); 1716 1717 static_assert(*std::initializer_list<int>{1, 2, 3}.begin() == 1, ""); 1718 static_assert(std::initializer_list<int>{1, 2, 3}.begin()[2] == 3, ""); 1719} 1720 1721namespace StmtExpr { 1722 struct A { int k; }; 1723 void f() { 1724 static_assert(({ const int x = 5; x * 3; }) == 15, ""); // expected-warning {{extension}} 1725 constexpr auto a = ({ A(); }); // expected-warning {{extension}} 1726 } 1727 constexpr int g(int k) { 1728 return ({ // expected-warning {{extension}} 1729 const int x = k; 1730 x * x; 1731 }); 1732 } 1733 static_assert(g(123) == 15129, ""); 1734 constexpr int h() { // expected-error {{never produces a constant}} 1735 return ({ // expected-warning {{extension}} 1736 return 0; // expected-note {{not supported}} 1737 1; 1738 }); 1739 } 1740} 1741 1742namespace VirtualFromBase { 1743 struct S1 { 1744 virtual int f() const; 1745 }; 1746 struct S2 { 1747 virtual int f(); 1748 }; 1749 template <typename T> struct X : T { 1750 constexpr X() {} 1751 double d = 0.0; 1752 constexpr int f() { return sizeof(T); } // expected-warning {{will not be implicitly 'const' in C++14}} 1753 }; 1754 1755 // Virtual f(), not OK. 1756 constexpr X<X<S1>> xxs1; 1757 constexpr X<S1> *p = const_cast<X<X<S1>>*>(&xxs1); 1758 static_assert(p->f() == sizeof(X<S1>), ""); // expected-error {{constant expression}} expected-note {{virtual function call}} 1759 1760 // Non-virtual f(), OK. 1761 constexpr X<X<S2>> xxs2; 1762 constexpr X<S2> *q = const_cast<X<X<S2>>*>(&xxs2); 1763 static_assert(q->f() == sizeof(S2), ""); 1764} 1765 1766namespace ConstexprConstructorRecovery { 1767 class X { 1768 public: 1769 enum E : short { 1770 headers = 0x1, 1771 middlefile = 0x2, 1772 choices = 0x4 1773 }; 1774 constexpr X() noexcept {}; 1775 protected: 1776 E val{0}; // expected-error {{cannot initialize a member subobject of type 'ConstexprConstructorRecovery::X::E' with an rvalue of type 'int'}} 1777 }; 1778 constexpr X x{}; 1779} 1780 1781namespace Lifetime { 1782 void f() { 1783 constexpr int &n = n; // expected-error {{constant expression}} expected-note {{use of reference outside its lifetime}} expected-warning {{not yet bound to a value}} 1784 constexpr int m = m; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}} 1785 } 1786 1787 constexpr int &get(int &&n) { return n; } 1788 struct S { 1789 int &&r; // expected-note 2{{declared here}} 1790 int &s; 1791 int t; 1792 constexpr S() : r(0), s(get(0)), t(r) {} // expected-warning {{temporary}} 1793 constexpr S(int) : r(0), s(get(0)), t(s) {} // expected-warning {{temporary}} expected-note {{read of object outside its lifetime}} 1794 }; 1795 constexpr int k1 = S().t; // ok, int is lifetime-extended to end of constructor 1796 constexpr int k2 = S(0).t; // expected-error {{constant expression}} expected-note {{in call}} 1797} 1798 1799namespace Bitfields { 1800 struct A { 1801 bool b : 1; 1802 unsigned u : 5; 1803 int n : 5; 1804 bool b2 : 3; 1805 unsigned u2 : 74; // expected-warning {{exceeds the width of its type}} 1806 int n2 : 81; // expected-warning {{exceeds the width of its type}} 1807 }; 1808 1809 constexpr A a = { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}} 1810 static_assert(a.b == 0 && a.u == 1 && a.n == -1 && a.b2 == 0 && 1811 a.u2 + 1 == 0 && a.n2 == 0x7fffffff, 1812 "bad truncation of bitfield values"); 1813 1814 struct B { 1815 int n : 3; 1816 constexpr B(int k) : n(k) {} 1817 }; 1818 static_assert(B(3).n == 3, ""); 1819 static_assert(B(4).n == -4, ""); 1820 static_assert(B(7).n == -1, ""); 1821 static_assert(B(8).n == 0, ""); 1822 static_assert(B(-1).n == -1, ""); 1823 static_assert(B(-8889).n == -1, ""); 1824 1825 namespace PR16755 { 1826 struct X { 1827 int x : 1; 1828 constexpr static int f(int x) { 1829 return X{x}.x; 1830 } 1831 }; 1832 static_assert(X::f(3) == -1, "3 should truncate to -1"); 1833 } 1834} 1835 1836namespace ZeroSizeTypes { 1837 constexpr int (*p1)[0] = 0, (*p2)[0] = 0; 1838 constexpr int k = p2 - p1; 1839 // expected-error@-1 {{constexpr variable 'k' must be initialized by a constant expression}} 1840 // expected-note@-2 {{subtraction of pointers to type 'int [0]' of zero size}} 1841 1842 int arr[5][0]; 1843 constexpr int f() { // expected-error {{never produces a constant expression}} 1844 return &arr[3] - &arr[0]; // expected-note {{subtraction of pointers to type 'int [0]' of zero size}} 1845 } 1846} 1847 1848namespace BadDefaultInit { 1849 template<int N> struct X { static const int n = N; }; 1850 1851 struct A { 1852 int k = // expected-error {{cannot use defaulted default constructor of 'A' within the class outside of member functions because 'k' has an initializer}} 1853 X<A().k>::n; // expected-error {{not a constant expression}} expected-note {{implicit default constructor for 'BadDefaultInit::A' first required here}} 1854 }; 1855 1856 // FIXME: The "constexpr constructor must initialize all members" diagnostic 1857 // here is bogus (we discard the k(k) initializer because the parameter 'k' 1858 // has been marked invalid). 1859 struct B { // expected-note 2{{candidate}} 1860 constexpr B( // expected-error {{must initialize all members}} expected-note {{candidate}} 1861 int k = X<B().k>::n) : // expected-error {{no matching constructor}} 1862 k(k) {} 1863 int k; // expected-note {{not initialized}} 1864 }; 1865} 1866 1867namespace NeverConstantTwoWays { 1868 // If we see something non-constant but foldable followed by something 1869 // non-constant and not foldable, we want the first diagnostic, not the 1870 // second. 1871 constexpr int f(int n) { // expected-error {{never produces a constant expression}} 1872 return (int *)(long)&n == &n ? // expected-note {{reinterpret_cast}} 1873 1 / 0 : // expected-warning {{division by zero}} 1874 0; 1875 } 1876 1877 constexpr int n = // expected-error {{must be initialized by a constant expression}} 1878 (int *)(long)&n == &n ? // expected-note {{reinterpret_cast}} 1879 1 / 0 : // expected-warning {{division by zero}} 1880 0; 1881} 1882 1883namespace PR17800 { 1884 struct A { 1885 constexpr int operator()() const { return 0; } 1886 }; 1887 template <typename ...T> constexpr int sink(T ...) { 1888 return 0; 1889 } 1890 template <int ...N> constexpr int run() { 1891 return sink(A()() + N ...); 1892 } 1893 constexpr int k = run<1, 2, 3>(); 1894} 1895 1896namespace BuiltinStrlen { 1897 constexpr const char *a = "foo\0quux"; 1898 constexpr char b[] = "foo\0quux"; 1899 constexpr int f() { return 'u'; } 1900 constexpr char c[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 }; 1901 1902 static_assert(__builtin_strlen("foo") == 3, ""); 1903 static_assert(__builtin_strlen("foo\0quux") == 3, ""); 1904 static_assert(__builtin_strlen("foo\0quux" + 4) == 4, ""); 1905 1906 constexpr bool check(const char *p) { 1907 return __builtin_strlen(p) == 3 && 1908 __builtin_strlen(p + 1) == 2 && 1909 __builtin_strlen(p + 2) == 1 && 1910 __builtin_strlen(p + 3) == 0 && 1911 __builtin_strlen(p + 4) == 4 && 1912 __builtin_strlen(p + 5) == 3 && 1913 __builtin_strlen(p + 6) == 2 && 1914 __builtin_strlen(p + 7) == 1 && 1915 __builtin_strlen(p + 8) == 0; 1916 } 1917 1918 static_assert(check(a), ""); 1919 static_assert(check(b), ""); 1920 static_assert(check(c), ""); 1921 1922 constexpr int over1 = __builtin_strlen(a + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 1923 constexpr int over2 = __builtin_strlen(b + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 1924 constexpr int over3 = __builtin_strlen(c + 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 1925 1926 constexpr int under1 = __builtin_strlen(a - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} 1927 constexpr int under2 = __builtin_strlen(b - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} 1928 constexpr int under3 = __builtin_strlen(c - 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} 1929 1930 // FIXME: The diagnostic here could be better. 1931 constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator. 1932 constexpr int bad = __builtin_strlen(d); // expected-error {{constant expression}} expected-note {{one-past-the-end}} 1933} 1934 1935namespace PR19010 { 1936 struct Empty {}; 1937 struct Empty2 : Empty {}; 1938 struct Test : Empty2 { 1939 constexpr Test() {} 1940 Empty2 array[2]; 1941 }; 1942 void test() { constexpr Test t; } 1943} 1944 1945void PR21327(int a, int b) { 1946 static_assert(&a + 1 != &b, ""); // expected-error {{constant expression}} 1947} 1948 1949namespace EmptyClass { 1950 struct E1 {} e1; 1951 union E2 {} e2; // expected-note {{here}} 1952 struct E3 : E1 {} e3; 1953 1954 // The defaulted copy constructor for an empty class does not read any 1955 // members. The defaulted copy constructor for an empty union reads the 1956 // object representation. 1957 constexpr E1 e1b(e1); 1958 constexpr E2 e2b(e2); // expected-error {{constant expression}} expected-note{{read of non-const}} expected-note {{in call}} 1959 constexpr E3 e3b(e3); 1960} 1961 1962namespace PR21786 { 1963 extern void (*start[])(); 1964 extern void (*end[])(); 1965 static_assert(&start != &end, ""); // expected-error {{constant expression}} 1966 static_assert(&start != nullptr, ""); 1967 1968 struct Foo; 1969 struct Bar { 1970 static const Foo x; 1971 static const Foo y; 1972 }; 1973 static_assert(&Bar::x != nullptr, ""); 1974 static_assert(&Bar::x != &Bar::y, ""); 1975} 1976 1977namespace PR21859 { 1978 constexpr int Fun() { return; } // expected-error {{non-void constexpr function 'Fun' should return a value}} 1979 constexpr int Var = Fun(); // expected-error {{constexpr variable 'Var' must be initialized by a constant expression}} 1980} 1981 1982struct InvalidRedef { 1983 int f; // expected-note{{previous definition is here}} 1984 constexpr int f(void); // expected-error{{redefinition of 'f'}} expected-warning{{will not be implicitly 'const'}} 1985}; 1986 1987namespace PR17938 { 1988 template <typename T> constexpr T const &f(T const &x) { return x; } 1989 1990 struct X {}; 1991 struct Y : X {}; 1992 struct Z : Y { constexpr Z() {} }; 1993 1994 static constexpr auto z = f(Z()); 1995} 1996 1997namespace PR24597 { 1998 struct A { 1999 int x, *p; 2000 constexpr A() : x(0), p(&x) {} 2001 constexpr A(const A &a) : x(a.x), p(&x) {} 2002 }; 2003 constexpr A f() { return A(); } 2004 constexpr A g() { return f(); } 2005 constexpr int a = *f().p; 2006 constexpr int b = *g().p; 2007} 2008