1 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify=expected,cxx11 -fcxx-exceptions %s -fconstexpr-depth 128 -triple i686-pc-linux-gnu 2 // RUN: %clang_cc1 -fsyntax-only -std=c++2a -pedantic -verify=expected,cxx20 -fcxx-exceptions %s -fconstexpr-depth 128 -triple i686-pc-linux-gnu 3 4 // A conditional-expression is a core constant expression unless it involves one 5 // of the following as a potentially evaluated subexpression [...]: 6 7 // - this (5.1.1 [expr.prim.general]) [Note: when evaluating a constant 8 // expression, function invocation substitution (7.1.5 [dcl.constexpr]) 9 // replaces each occurrence of this in a constexpr member function with a 10 // pointer to the class object. -end note]; 11 struct This { 12 int this1 : this1; // expected-error {{undeclared}} 13 int this2 : this->this1; // expected-error {{invalid}} 14 void this3() { 15 int n1[this->this1]; // expected-warning {{variable length array}} 16 int n2[this1]; // expected-warning {{variable length array}} 17 (void)n1, (void)n2; 18 } 19 }; 20 21 // - an invocation of a function other than a constexpr constructor for a 22 // literal class or a constexpr function [ Note: Overload resolution (13.3) 23 // is applied as usual - end note ]; 24 struct NonConstexpr1 { 25 static int f() { return 1; } // expected-note {{here}} 26 int n : f(); // expected-error {{constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}} 27 }; 28 struct NonConstexpr2 { 29 constexpr NonConstexpr2(); // expected-note {{here}} 30 int n; 31 }; 32 struct NonConstexpr3 { 33 NonConstexpr3(); 34 int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}} 35 }; 36 struct NonConstexpr4 { 37 NonConstexpr4(); 38 int n; 39 }; 40 struct NonConstexpr5 { 41 int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-literal type 'NonConstexpr4' cannot be used in a constant expression}} 42 }; 43 44 // - an invocation of an undefined constexpr function or an undefined 45 // constexpr constructor; 46 struct UndefinedConstexpr { 47 constexpr UndefinedConstexpr(); 48 static constexpr int undefinedConstexpr1(); // expected-note {{here}} 49 int undefinedConstexpr2 : undefinedConstexpr1(); // expected-error {{constant expression}} expected-note {{undefined function 'undefinedConstexpr1' cannot be used in a constant expression}} 50 }; 51 52 // - an invocation of a constexpr function with arguments that, when substituted 53 // by function invocation substitution (7.1.5), do not produce a core constant 54 // expression; 55 namespace NonConstExprReturn { 56 static constexpr const int &id_ref(const int &n) { 57 return n; 58 } 59 struct NonConstExprFunction { 60 int n : id_ref(16); // ok 61 }; 62 constexpr const int *address_of(const int &a) { 63 return &a; 64 } 65 constexpr const int *return_param(int n) { // expected-note {{declared here}} 66 return address_of(n); 67 } 68 struct S { 69 int n : *return_param(0); // expected-error {{constant expression}} expected-note {{read of variable whose lifetime has ended}} 70 }; 71 } 72 73 // - an invocation of a constexpr constructor with arguments that, when 74 // substituted by function invocation substitution (7.1.5), do not produce all 75 // constant expressions for the constructor calls and full-expressions in the 76 // mem-initializers (including conversions); 77 namespace NonConstExprCtor { 78 struct T { 79 constexpr T(const int &r) : 80 r(r) { 81 } 82 const int &r; 83 }; 84 constexpr int n = 0; 85 constexpr T t1(n); // ok 86 constexpr T t2(0); // expected-error {{must be initialized by a constant expression}} expected-note {{temporary created here}} expected-note {{reference to temporary is not a constant expression}} 87 88 struct S { 89 int n : T(4).r; // ok 90 }; 91 } 92 93 // - an invocation of a constexpr function or a constexpr constructor that would 94 // exceed the implementation-defined recursion limits (see Annex B); 95 namespace RecursionLimits { 96 constexpr int RecurseForever(int n) { 97 return n + RecurseForever(n+1); // expected-note {{constexpr evaluation exceeded maximum depth of 128 calls}} expected-note 9{{in call to 'RecurseForever(}} expected-note {{skipping 118 calls}} 98 } 99 struct AlsoRecurseForever { 100 constexpr AlsoRecurseForever(int n) : 101 n(AlsoRecurseForever(n+1).n) // expected-note {{constexpr evaluation exceeded maximum depth of 128 calls}} expected-note 9{{in call to 'AlsoRecurseForever(}} expected-note {{skipping 118 calls}} 102 {} 103 int n; 104 }; 105 struct S { 106 int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}} 107 int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}} 108 }; 109 } 110 111 // DR1458: taking the address of an object of incomplete class type 112 namespace IncompleteClassTypeAddr { 113 struct S; 114 extern S s; 115 constexpr S *p = &s; // ok 116 static_assert(p, ""); 117 118 extern S sArr[]; 119 constexpr S (*p2)[] = &sArr; // ok 120 121 struct S { 122 constexpr S *operator&() const { return nullptr; } 123 }; 124 constexpr S *q = &s; // ok 125 static_assert(!q, ""); 126 } 127 128 // - an operation that would have undefined behavior [Note: including, for 129 // example, signed integer overflow (Clause 5 [expr]), certain pointer 130 // arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain 131 // shift operations (5.8 [expr.shift]) -end note]; 132 namespace UndefinedBehavior { 133 void f(int n) { 134 switch (n) { 135 case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}} 136 case (int)0x80000000u: // ok 137 case (int)10000000000ll: // expected-note {{here}} 138 case (unsigned int)10000000000ll: // expected-error {{duplicate case value}} 139 case (int)(unsigned)(long long)4.4e9: // ok 140 case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value +Inf is outside the range of representable values of type 'int'}} 141 case (int)((float)1e37 / 1e30): // ok 142 case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value +Inf is outside the range of representable values of type 'int'}} 143 break; 144 } 145 } 146 147 constexpr int int_min = ~0x7fffffff; 148 constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 149 constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} 150 constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} 151 constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 152 constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 153 154 constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} 155 constexpr int shl_0 = 0 << 0; // ok 156 constexpr int shl_31 = 0 << 31; // ok 157 constexpr int shl_32 = 0 << 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type 'int' (32}} 158 constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok 159 constexpr int shl_unsigned_into_sign = 1u << 31; // ok 160 constexpr int shl_unsigned_overflow = 1024u << 31; // ok 161 constexpr int shl_signed_negative = (-3) << 1; // cxx11-error {{constant expression}} cxx11-note {{left shift of negative value -3}} 162 constexpr int shl_signed_ok = 1 << 30; // ok 163 constexpr int shl_signed_into_sign = 1 << 31; // ok (DR1457) 164 constexpr int shl_signed_into_sign_2 = 0x7fffffff << 1; // ok (DR1457) 165 constexpr int shl_signed_off_end = 2 << 31; // cxx11-error {{constant expression}} cxx11-note {{signed left shift discards bits}} expected-warning {{signed shift result (0x100000000) requires 34 bits to represent, but 'int' only has 32 bits}} 166 constexpr int shl_signed_off_end_2 = 0x7fffffff << 2; // cxx11-error {{constant expression}} cxx11-note {{signed left shift discards bits}} expected-warning {{signed shift result (0x1FFFFFFFC) requires 34 bits to represent, but 'int' only has 32 bits}} 167 constexpr int shl_signed_overflow = 1024 << 31; // cxx11-error {{constant expression}} cxx11-note {{signed left shift discards bits}} expected-warning {{requires 43 bits to represent}} 168 constexpr int shl_signed_ok2 = 1024 << 20; // ok 169 170 constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} 171 constexpr int shr_0 = 0 >> 0; // ok 172 constexpr int shr_31 = 0 >> 31; // ok 173 constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} 174 175 struct S { 176 int m; 177 }; 178 constexpr S s = { 5 }; 179 constexpr const int *p = &s.m + 1; 180 constexpr const int &f(const int *q) { 181 return q[0]; 182 } 183 constexpr int n = (f(p), 0); // ok 184 struct T { 185 int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} 186 }; 187 188 namespace Ptr { 189 struct A {}; 190 struct B : A { int n; }; 191 B a[3][3]; 192 constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}} 193 B b = {}; 194 constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}} 195 constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}} 196 constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}} 197 constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}} 198 199 constexpr A *na = nullptr; 200 constexpr B *nb = nullptr; 201 constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}} 202 constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}} 203 static_assert((A*)nb == 0, ""); 204 static_assert((B*)na == 0, ""); 205 constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}} 206 constexpr const int *np1 = (int*)nullptr + 0; // ok 207 constexpr const int *np2 = &(*(int(*)[4])nullptr)[0]; // ok 208 constexpr const int *np3 = &(*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot perform pointer arithmetic on null pointer}} 209 210 struct C { 211 constexpr int f() const { return 0; } 212 } constexpr c = C(); 213 constexpr int k1 = c.f(); // ok 214 constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced null pointer}} 215 constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}} 216 C c2; 217 constexpr int k4 = c2.f(); // ok! 218 219 constexpr int diff1 = &a[2] - &a[0]; 220 constexpr int diff2 = &a[1][3] - &a[1][0]; 221 constexpr int diff3 = &a[2][0] - &a[1][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}} 222 static_assert(&a[2][0] == &a[1][3], ""); 223 constexpr int diff4 = (&b + 1) - &b; 224 constexpr int diff5 = &a[1][2].n - &a[1][0].n; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}} 225 constexpr int diff6 = &a[1][2].n - &a[1][2].n; 226 constexpr int diff7 = (A*)&a[0][1] - (A*)&a[0][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}} 227 } 228 229 namespace Overflow { 230 // Signed int overflow. 231 constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok 232 constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }} 233 constexpr int n3 = n1 + 1; // ok 234 constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }} 235 constexpr int n5 = -65536 * 32768; // ok 236 constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }} 237 constexpr int n7 = -n3 + -1; // ok 238 constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }} 239 constexpr int n9 = n3 - 0; // ok 240 constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }} 241 constexpr int n11 = -1 - n3; // ok 242 constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }} 243 constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }} 244 constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }} 245 constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }} 246 constexpr signed char c1 = 100 * 2; // ok expected-warning{{changes value}} 247 constexpr signed char c2 = '\x64' * '\2'; // also ok expected-warning{{changes value}} 248 constexpr long long ll1 = 0x7fffffffffffffff; // ok 249 constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }} 250 constexpr long long ll3 = -ll1 - 1; // ok 251 constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }} 252 constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }} 253 254 // Yikes. 255 char melchizedek[2200000000]; 256 typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t; 257 constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0]; // ok 258 constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }} 259 constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u]; // ok 260 constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // expected-error {{constant expression}} expected-note {{ -2147483649 }} 261 262 // Unsigned int overflow. 263 static_assert(65536u * 65536u == 0u, ""); // ok 264 static_assert(4294967295u + 1u == 0u, ""); // ok 265 static_assert(0u - 1u == 4294967295u, ""); // ok 266 static_assert(~0u * ~0u == 1u, ""); // ok 267 268 template<typename T> constexpr bool isinf(T v) { return v && v / 2 == v; } 269 270 // Floating-point overflow and NaN. 271 constexpr float f1 = 1e38f * 3.4028f; // ok 272 constexpr float f2 = 1e38f * 3.4029f; // ok, +inf is in range of representable values 273 constexpr float f3 = 1e38f / -.2939f; // ok 274 constexpr float f4 = 1e38f / -.2938f; // ok, -inf is in range of representable values 275 constexpr float f5 = 2e38f + 2e38f; // ok, +inf is in range of representable values 276 constexpr float f6 = -2e38f - 2e38f; // ok, -inf is in range of representable values 277 constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{division by zero}} 278 constexpr float f8 = 1.f / 0.f; // expected-error {{constant expression}} expected-note {{division by zero}} 279 constexpr float f9 = 1e308 / 1e-308; // ok, +inf 280 constexpr float f10 = f2 - f2; // expected-error {{constant expression}} expected-note {{produces a NaN}} 281 constexpr float f11 = f2 + f4; // expected-error {{constant expression}} expected-note {{produces a NaN}} 282 constexpr float f12 = f2 / f2; // expected-error {{constant expression}} expected-note {{produces a NaN}} 283 static_assert(!isinf(f1), ""); 284 static_assert(isinf(f2), ""); 285 static_assert(!isinf(f3), ""); 286 static_assert(isinf(f4), ""); 287 static_assert(isinf(f5), ""); 288 static_assert(isinf(f6), ""); 289 static_assert(isinf(f9), ""); 290 } 291 } 292 293 // - a lambda-expression (5.1.2); 294 struct Lambda { 295 int n : []{ return 1; }(); // cxx11-error {{constant expression}} cxx11-error {{integral constant expression}} cxx11-note {{non-literal type}} 296 }; 297 298 // - an lvalue-to-rvalue conversion (4.1) unless it is applied to 299 namespace LValueToRValue { 300 // - a non-volatile glvalue of integral or enumeration type that refers to a 301 // non-volatile const object with a preceding initialization, initialized 302 // with a constant expression [Note: a string literal (2.14.5 [lex.string]) 303 // corresponds to an array of such objects. -end note], or 304 volatile const int vi = 1; // expected-note 2{{here}} 305 const int ci = 1; 306 volatile const int &vrci = ci; 307 static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 308 static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}} 309 static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 310 311 // - a non-volatile glvalue of literal type that refers to a non-volatile 312 // object defined with constexpr, or that refers to a sub-object of such an 313 // object, or 314 struct V { 315 constexpr V() : v(1) {} 316 volatile int v; // expected-note {{not literal because}} 317 }; 318 constexpr V v; // expected-error {{non-literal type}} 319 struct S { 320 constexpr S(int=0) : i(1), v(const_cast<volatile int&>(vi)) {} 321 constexpr S(const S &s) : i(2), v(const_cast<volatile int&>(vi)) {} 322 int i; 323 volatile int &v; 324 }; 325 constexpr S s; // ok 326 constexpr volatile S vs; // expected-note {{here}} 327 constexpr const volatile S &vrs = s; // ok 328 static_assert(s.i, ""); 329 static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 330 static_assert(const_cast<int&>(s.v), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}} 331 static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 332 static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}} 333 static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 334 335 // - a non-volatile glvalue of literal type that refers to a non-volatile 336 // temporary object whose lifetime has not ended, initialized with a 337 // constant expression; 338 constexpr volatile S f() { return S(); } 339 static_assert(f().i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 340 static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 341 } 342 343 // DR1312: The proposed wording for this defect has issues, so we ignore this 344 // bullet and instead prohibit casts from pointers to cv void (see core-20842 345 // and core-20845). 346 // 347 // - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a 348 // glvalue of type cv1 T that refers to an object of type cv2 U, where T and U 349 // are neither the same type nor similar types (4.4 [conv.qual]); 350 351 // - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that 352 // refers to a non-active member of a union or a subobject thereof; 353 namespace LValueToRValueUnion { 354 // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing 355 // of this. 356 union U { int a, b; } constexpr u = U(); 357 static_assert(u.a == 0, ""); 358 constexpr const int *bp = &u.b; 359 constexpr int b = *bp; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}} 360 361 extern const U pu; 362 constexpr const int *pua = &pu.a; 363 constexpr const int *pub = &pu.b; 364 constexpr U pu = { .b = 1 }; // cxx11-warning {{C++20 extension}} 365 constexpr const int a2 = *pua; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}} 366 constexpr const int b2 = *pub; // ok 367 } 368 369 // - an id-expression that refers to a variable or data member of reference type 370 // unless the reference has a preceding initialization, initialized with a 371 // constant expression; 372 namespace References { 373 const int a = 2; 374 int &b = *const_cast<int*>(&a); 375 int c = 10; // expected-note 2 {{here}} 376 int &d = c; 377 constexpr int e = 42; 378 int &f = const_cast<int&>(e); 379 extern int &g; // expected-note {{here}} 380 constexpr int &h(); // expected-note {{here}} 381 int &i = h(); // expected-note {{here}} 382 constexpr int &j() { return b; } 383 int &k = j(); 384 385 struct S { 386 int A : a; 387 int B : b; 388 int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 389 int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 390 int D2 : &d - &c + 1; 391 int E : e / 2; 392 int F : f - 11; 393 int G : g; // expected-error {{constant expression}} expected-note {{initializer of 'g' is unknown}} 394 int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}} 395 int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}} 396 int J : j(); 397 int K : k; 398 }; 399 } 400 401 // - a dynamic_cast (5.2.7); 402 namespace DynamicCast { 403 struct S { int n; }; 404 constexpr S s { 16 }; 405 struct T { 406 int n : dynamic_cast<const S*>(&s)->n; // cxx11-warning {{constant expression}} cxx11-note {{dynamic_cast}} 407 }; 408 } 409 410 // - a reinterpret_cast (5.2.10); 411 namespace ReinterpretCast { 412 struct S { int n; }; 413 constexpr S s { 16 }; 414 struct T { 415 int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}} 416 }; 417 struct U { 418 int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}} 419 }; 420 } 421 422 // - a pseudo-destructor call (5.2.4); 423 namespace PseudoDtor { 424 int k; 425 typedef int I; 426 struct T { 427 int n : (k.~I(), 1); // expected-error {{constant expression}} expected-note {{visible outside that expression}} 428 }; 429 430 // FIXME: It's unclear whether this should be accepted in C++20 mode. The parameter is destroyed twice here. 431 constexpr int f(int a = 1) { // cxx11-error {{constant expression}} 432 return ( 433 a.~I(), // cxx11-note 2{{pseudo-destructor}} 434 0); 435 } 436 static_assert(f() == 0, ""); // cxx11-error {{constant expression}} cxx11-note {{in call}} 437 438 // This is OK in C++20: the union has no active member after the 439 // pseudo-destructor call, so the union destructor has no effect. 440 union U { int x; }; 441 constexpr int g(U u = {1}) { // cxx11-error {{constant expression}} 442 return ( 443 u.x.~I(), // cxx11-note 2{{pseudo-destructor}} 444 0); 445 } 446 static_assert(g() == 0, ""); // cxx11-error {{constant expression}} cxx11-note {{in call}} 447 } 448 449 // - increment or decrement operations (5.2.6, 5.3.2); 450 namespace IncDec { 451 int k = 2; 452 struct T { 453 int n : ++k; // expected-error {{constant expression}} cxx20-note {{visible outside}} 454 int m : --k; // expected-error {{constant expression}} cxx20-note {{visible outside}} 455 }; 456 } 457 458 // - a typeid expression (5.2.8) whose operand is of a polymorphic class type; 459 namespace std { 460 struct type_info { 461 virtual ~type_info(); 462 const char *name; 463 }; 464 } 465 namespace TypeId { 466 struct S { virtual void f(); }; 467 constexpr S *p = 0; 468 constexpr const std::type_info &ti1 = typeid(*p); // expected-error {{must be initialized by a constant expression}} cxx11-note {{typeid applied to expression of polymorphic type 'TypeId::S'}} cxx20-note {{dereferenced null pointer}} 469 470 struct T {} t; 471 constexpr const std::type_info &ti2 = typeid(t); 472 } 473 474 // - a new-expression (5.3.4); 475 // - a delete-expression (5.3.5); 476 namespace NewDelete { 477 constexpr int *p = 0; 478 struct T { 479 int n : *new int(4); // expected-warning {{constant expression}} cxx11-note {{until C++20}} cxx20-note {{was not deallocated}} 480 int m : (delete p, 2); // cxx11-warning {{constant expression}} cxx11-note {{until C++20}} 481 }; 482 } 483 484 // - a relational (5.9) or equality (5.10) operator where the result is 485 // unspecified; 486 namespace UnspecifiedRelations { 487 int a, b; 488 constexpr int *p = &a, *q = &b; 489 // C++11 [expr.rel]p2: If two pointers p and q of the same type point to 490 // different objects that are not members of the same array or to different 491 // functions, or if only one of them is null, the results of p<q, p>q, p<=q, 492 // and p>=q are unspecified. 493 constexpr bool u1 = p < q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 494 constexpr bool u2 = p > q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 495 constexpr bool u3 = p <= q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 496 constexpr bool u4 = p >= q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 497 constexpr bool u5 = p < (int*)0; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 498 constexpr bool u6 = p <= (int*)0; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 499 constexpr bool u7 = p > (int*)0; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 500 constexpr bool u8 = p >= (int*)0; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 501 constexpr bool u9 = (int*)0 < q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 502 constexpr bool u10 = (int*)0 <= q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 503 constexpr bool u11 = (int*)0 > q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 504 constexpr bool u12 = (int*)0 >= q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 505 void f(), g(); 506 507 constexpr void (*pf)() = &f, (*pg)() = &g; 508 constexpr bool u13 = pf < pg; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 509 constexpr bool u14 = pf == pg; 510 511 // If two pointers point to non-static data members of the same object with 512 // different access control, the result is unspecified. 513 struct A { 514 public: 515 constexpr A() : a(0), b(0) {} 516 int a; 517 constexpr bool cmp() const { return &a < &b; } // expected-note {{comparison of address of fields 'a' and 'b' of 'A' with differing access specifiers (public vs private) has unspecified value}} 518 private: 519 int b; 520 }; 521 static_assert(A().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}} 522 class B { 523 public: 524 A a; 525 constexpr bool cmp() const { return &a.a < &b.a; } // expected-note {{comparison of address of fields 'a' and 'b' of 'B' with differing access specifiers (public vs protected) has unspecified value}} 526 protected: 527 A b; 528 }; 529 static_assert(B().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}} 530 531 // If two pointers point to different base sub-objects of the same object, or 532 // one points to a base subobject and the other points to a member, the result 533 // of the comparison is unspecified. This is not explicitly called out by 534 // [expr.rel]p2, but is covered by 'Other pointer comparisons are 535 // unspecified'. 536 struct C { 537 int c[2]; 538 }; 539 struct D { 540 int d; 541 }; 542 struct E : C, D { 543 struct Inner { 544 int f; 545 } e; 546 } e; 547 constexpr bool base1 = &e.c[0] < &e.d; // expected-error {{constant expression}} expected-note {{comparison of addresses of subobjects of different base classes has unspecified value}} 548 constexpr bool base2 = &e.c[1] < &e.e.f; // expected-error {{constant expression}} expected-note {{comparison of address of base class subobject 'C' of class 'E' to field 'e' has unspecified value}} 549 constexpr bool base3 = &e.e.f < &e.d; // expected-error {{constant expression}} expected-note {{comparison of address of base class subobject 'D' of class 'E' to field 'e' has unspecified value}} 550 551 // [expr.rel]p3: Pointers to void can be compared [...] if both pointers 552 // represent the same address or are both the null pointer [...]; otherwise 553 // the result is unspecified. 554 struct S { int a, b; } s; 555 constexpr void *null = 0; 556 constexpr void *pv = (void*)&s.a; 557 constexpr void *qv = (void*)&s.b; 558 constexpr bool v1 = null < (int*)0; 559 constexpr bool v2 = null < pv; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 560 constexpr bool v3 = null == pv; // ok 561 constexpr bool v4 = qv == pv; // ok 562 constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}} 563 constexpr bool v6 = qv > null; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}} 564 constexpr bool v7 = qv <= (void*)&s.b; // ok 565 constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}} 566 } 567 568 // - an assignment or a compound assignment (5.17); or 569 namespace Assignment { 570 int k; 571 struct T { 572 int n : (k = 9); // expected-error {{constant expression}} cxx20-note {{visible outside}} 573 int m : (k *= 2); // expected-error {{constant expression}} cxx20-note {{visible outside}} 574 }; 575 576 struct Literal { 577 constexpr Literal(const char *name) : name(name) {} 578 const char *name; 579 }; 580 struct Expr { 581 constexpr Expr(Literal l) : IsLiteral(true), l(l) {} 582 bool IsLiteral; 583 union { 584 Literal l; 585 // ... 586 }; 587 }; 588 struct MulEq { 589 constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {} 590 Expr LHS; 591 Expr RHS; 592 }; 593 constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); } 594 Literal a("a"); 595 Literal b("b"); 596 MulEq c = a *= b; // ok 597 } 598 599 // - a throw-expression (15.1) 600 namespace Throw { 601 struct S { 602 int n : (throw "hello", 10); // expected-error {{constant expression}} 603 }; 604 } 605 606 // PR9999 607 template<unsigned int v> 608 class bitWidthHolding { 609 public: 610 static const 611 unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1); 612 }; 613 614 static const int width=bitWidthHolding<255>::width; 615 616 template<bool b> 617 struct always_false { 618 static const bool value = false; 619 }; 620 621 template<bool b> 622 struct and_or { 623 static const bool and_value = b && and_or<always_false<b>::value>::and_value; 624 static const bool or_value = !b || and_or<always_false<b>::value>::or_value; 625 }; 626 627 static const bool and_value = and_or<true>::and_value; 628 static const bool or_value = and_or<true>::or_value; 629 630 static_assert(and_value == false, ""); 631 static_assert(or_value == true, ""); 632 633 namespace rdar13090123 { 634 typedef __INTPTR_TYPE__ intptr_t; 635 636 constexpr intptr_t f(intptr_t x) { 637 return (((x) >> 21) * 8); 638 } 639 640 extern "C" int foo; 641 642 constexpr intptr_t i = f((intptr_t)&foo - 10); // expected-error{{constexpr variable 'i' must be initialized by a constant expression}} \ 643 // expected-note{{reinterpret_cast}} 644 } 645