p2-0x.cpp revision 789f9b6be5df6e5151ac35e68416cdf550db1196
1// RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128 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// FIXME: 113// - an operation that would have undefined behavior [Note: including, for 114// example, signed integer overflow (Clause 5 [expr]), certain pointer 115// arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain 116// shift operations (5.8 [expr.shift]) -end note]; 117namespace UndefinedBehavior { 118 void f(int n) { 119 switch (n) { 120 case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}} 121 case (int)0x80000000u: // ok 122 case (int)10000000000ll: // expected-note {{here}} 123 case (unsigned int)10000000000ll: // expected-error {{duplicate case value}} 124 case (int)(unsigned)(long long)4.4e9: // ok 125 case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}} 126 case (int)((float)1e37 / 1e30): // ok 127 case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}} 128 break; 129 } 130 } 131 132 constexpr int int_min = ~0x7fffffff; 133 constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 134 constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}} 135 constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}} 136 constexpr int int_min_div_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; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 148 constexpr int shl_signed_overflow = 1024 << 31; // expected-error {{constant expression}} expected-note {{value 2199023255552 is outside the range}} expected-warning {{requires 43 bits to represent}} 149 constexpr int shl_signed_ok2 = 1024 << 20; // ok 150 151 constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}} 152 constexpr int shr_0 = 0 >> 0; // ok 153 constexpr int shr_31 = 0 >> 31; // ok 154 constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}} 155 156 struct S { 157 int m; 158 }; 159 constexpr S s = { 5 }; // expected-note {{declared here}} 160 constexpr const int *p = &s.m + 1; 161 constexpr const int &f(const int *q) { 162 return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}} 163 } 164 constexpr int n = (f(p), 0); // expected-error {{constant expression}} expected-note {{in call to 'f(&s.m + 1)'}} 165 struct T { 166 int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} 167 }; 168 169 namespace Ptr { 170 struct A {}; 171 struct B : A { int n; }; 172 B a[3][3]; 173 constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}} 174 B b = {}; 175 constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}} 176 constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}} 177 constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}} 178 constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}} 179 180 constexpr A *na = nullptr; 181 constexpr B *nb = nullptr; 182 constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}} 183 constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}} 184 static_assert((A*)nb == 0, ""); 185 static_assert((B*)na == 0, ""); 186 constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}} 187 constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}} 188 } 189} 190 191// - a lambda-expression (5.1.2); 192struct Lambda { 193 // FIXME: clang crashes when trying to parse this! Revisit this check once 194 // lambdas are fully implemented. 195 //int n : []{ return 1; }(); 196}; 197 198// - an lvalue-to-rvalue conversion (4.1) unless it is applied to 199namespace LValueToRValue { 200 // - a non-volatile glvalue of integral or enumeration type that refers to a 201 // non-volatile const object with a preceding initialization, initialized 202 // with a constant expression [Note: a string literal (2.14.5 [lex.string]) 203 // corresponds to an array of such objects. -end note], or 204 volatile const int vi = 1; // expected-note {{here}} 205 const int ci = 1; 206 volatile const int &vrci = ci; 207 static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}} 208 static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}} 209 static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 210 211 // - a non-volatile glvalue of literal type that refers to a non-volatile 212 // object defined with constexpr, or that refers to a sub-object of such an 213 // object, or 214 struct S { 215 constexpr S(int=0) : i(1), v(1) {} 216 constexpr S(const S &s) : i(2), v(2) {} 217 int i; 218 volatile int v; 219 }; 220 constexpr S s; 221 constexpr volatile S vs; // expected-note {{here}} 222 constexpr const volatile S &vrs = s; 223 static_assert(s.i, ""); 224 static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 225 static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 226 static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}} 227 static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 228 229 // - a non-volatile glvalue of literal type that refers to a non-volatile 230 // temporary object whose lifetime has not ended, initialized with a 231 // constant expression; 232 constexpr volatile S f() { return S(); } 233 static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here! 234 static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{subexpression}} 235} 236 237// FIXME: 238// 239// DR1312: The proposed wording for this defect has issues, so we ignore this 240// bullet and instead prohibit casts from pointers to cv void (see core-20842 241// and core-20845). 242// 243// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a 244// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U 245// are neither the same type nor similar types (4.4 [conv.qual]); 246 247// FIXME: 248// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that 249// refers to a non-active member of a union or a subobject thereof; 250 251// - an id-expression that refers to a variable or data member of reference type 252// unless the reference has a preceding initialization, initialized with a 253// constant expression; 254namespace References { 255 const int a = 2; 256 int &b = *const_cast<int*>(&a); 257 int c = 10; // expected-note 2 {{here}} 258 int &d = c; 259 constexpr int e = 42; 260 int &f = const_cast<int&>(e); 261 extern int &g; 262 constexpr int &h(); // expected-note 2{{here}} 263 int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}} 264 constexpr int &j() { return b; } 265 int &k = j(); 266 267 struct S { 268 int A : a; 269 int B : b; 270 int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 271 int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 272 int D2 : &d - &c + 1; 273 int E : e / 2; 274 int F : f - 11; 275 int G : g; // expected-error {{constant expression}} 276 int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}} 277 int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}} 278 int J : j(); 279 int K : k; 280 }; 281} 282 283// - a dynamic_cast (5.2.7); 284namespace DynamicCast { 285 struct S { int n; }; 286 constexpr S s { 16 }; 287 struct T { 288 int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}} 289 }; 290} 291 292// - a reinterpret_cast (5.2.10); 293namespace ReinterpretCast { 294 struct S { int n; }; 295 constexpr S s { 16 }; 296 struct T { 297 int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}} 298 }; 299 struct U { 300 int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}} 301 }; 302} 303 304// - a pseudo-destructor call (5.2.4); 305namespace PseudoDtor { 306 int k; 307 typedef int I; 308 struct T { 309 int n : (k.~I(), 0); // expected-error {{constant expression}} expected-note{{subexpression}} 310 }; 311} 312 313// - increment or decrement operations (5.2.6, 5.3.2); 314namespace IncDec { 315 int k = 2; 316 struct T { 317 int n : ++k; // expected-error {{constant expression}} 318 int m : --k; // expected-error {{constant expression}} 319 }; 320} 321 322// - a typeid expression (5.2.8) whose operand is of a polymorphic class type; 323namespace std { 324 struct type_info { 325 virtual ~type_info(); 326 const char *name; 327 }; 328} 329namespace TypeId { 330 struct S { virtual void f(); }; 331 constexpr S *p = 0; 332 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'}} 333 334 struct T {} t; 335 constexpr const std::type_info &ti2 = typeid(t); 336} 337 338// - a new-expression (5.3.4); 339// - a delete-expression (5.3.5); 340namespace NewDelete { 341 int *p = 0; 342 struct T { 343 int n : *new int(4); // expected-error {{constant expression}} expected-note {{subexpression}} 344 int m : (delete p, 2); // expected-error {{constant expression}} expected-note {{subexpression}} 345 }; 346} 347 348// - a relational (5.9) or equality (5.10) operator where the result is 349// unspecified; 350namespace UnspecifiedRelations { 351 int a, b; 352 constexpr int *p = &a, *q = &b; 353 // C++11 [expr.rel]p2: If two pointers p and q of the same type point to 354 // different objects that are not members of the same array or to different 355 // functions, or if only one of them is null, the results of p<q, p>q, p<=q, 356 // and p>=q are unspecified. 357 constexpr bool u1 = p < q; // expected-error {{constant expression}} 358 constexpr bool u2 = p > q; // expected-error {{constant expression}} 359 constexpr bool u3 = p <= q; // expected-error {{constant expression}} 360 constexpr bool u4 = p >= q; // expected-error {{constant expression}} 361 constexpr bool u5 = p < 0; // expected-error {{constant expression}} 362 constexpr bool u6 = p <= 0; // expected-error {{constant expression}} 363 constexpr bool u7 = p > 0; // expected-error {{constant expression}} 364 constexpr bool u8 = p >= 0; // expected-error {{constant expression}} 365 constexpr bool u9 = 0 < q; // expected-error {{constant expression}} 366 constexpr bool u10 = 0 <= q; // expected-error {{constant expression}} 367 constexpr bool u11 = 0 > q; // expected-error {{constant expression}} 368 constexpr bool u12 = 0 >= q; // expected-error {{constant expression}} 369 void f(), g(); 370 371 constexpr void (*pf)() = &f, (*pg)() = &g; 372 constexpr bool u13 = pf < pg; // expected-error {{constant expression}} 373 constexpr bool u14 = pf == pg; 374 375 // FIXME: 376 // If two pointers point to non-static data members of the same object with 377 // different access control, the result is unspecified. 378 379 // FIXME: 380 // [expr.rel]p3: Pointers to void can be compared [...] if both pointers 381 // represent the same address or are both the null pointer [...]; otherwise 382 // the result is unspecified. 383 384 // FIXME: Implement comparisons of pointers to members. 385 // [expr.eq]p2: If either is a pointer to a virtual member function and 386 // neither is null, the result is unspecified. 387} 388 389// - an assignment or a compound assignment (5.17); or 390namespace Assignment { 391 int k; 392 struct T { 393 int n : (k = 9); // expected-error {{constant expression}} 394 int m : (k *= 2); // expected-error {{constant expression}} 395 }; 396 397 struct Literal { 398 constexpr Literal(const char *name) : name(name) {} 399 const char *name; 400 }; 401 struct Expr { 402 constexpr Expr(Literal l) : IsLiteral(true), l(l) {} 403 bool IsLiteral; 404 union { 405 Literal l; 406 // ... 407 }; 408 }; 409 struct MulEq { 410 constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {} 411 Expr LHS; 412 Expr RHS; 413 }; 414 constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); } 415 Literal a("a"); 416 Literal b("b"); 417 MulEq c = a *= b; // ok 418} 419 420// - a throw-expression (15.1) 421namespace Throw { 422 struct S { 423 int n : (throw "hello", 10); // expected-error {{constant expression}} expected-note {{subexpression}} 424 }; 425} 426 427// PR9999 428template<unsigned int v> 429class bitWidthHolding { 430public: 431 static const 432 unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1); 433}; 434 435static const int width=bitWidthHolding<255>::width; 436 437template<bool b> 438struct always_false { 439 static const bool value = false; 440}; 441 442template<bool b> 443struct and_or { 444 static const bool and_value = b && and_or<always_false<b>::value>::and_value; 445 static const bool or_value = !b || and_or<always_false<b>::value>::or_value; 446}; 447 448static const bool and_value = and_or<true>::and_value; 449static const bool or_value = and_or<true>::or_value; 450 451static_assert(and_value == false, ""); 452static_assert(or_value == true, ""); 453