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