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