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