p2-0x.cpp revision 08d6e032a2a0a8656d12b3b7b93942987bb12eb7
1// RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 256 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(); 37 int n; 38}; 39struct 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; 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 {{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}} 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 256 calls}} expected-note 9{{in call to 'RecurseForever(}} expected-note {{skipping 246 calls}} 99 } 100 struct AlsoRecurseForever { 101 constexpr AlsoRecurseForever(int n) : 102 n(AlsoRecurseForever(n+1).n) // expected-note {{constexpr evaluation exceeded maximum depth of 256 calls}} expected-note 9{{in call to 'AlsoRecurseForever(}} expected-note {{skipping 246 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)(unsigned)(long long)4.4e9: // ok 122 case (float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}} 123 case (int)((float)1e37 / 1e30): // ok 124 case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}} 125 break; 126 } 127 } 128 129 struct S { 130 int m; 131 }; 132 constexpr S s = { 5 }; // expected-note {{declared here}} 133 constexpr const int *p = &s.m + 1; 134 constexpr const int &f(const int *q) { 135 return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}} 136 } 137 struct T { 138 int n : f(p); // expected-error {{not an integer constant expression}} expected-note {{in call to 'f(&s.m + 1)'}} 139 }; 140} 141 142// - a lambda-expression (5.1.2); 143struct Lambda { 144 // FIXME: clang crashes when trying to parse this! Revisit this check once 145 // lambdas are fully implemented. 146 //int n : []{ return 1; }(); 147}; 148 149// FIXME: 150// - an lvalue-to-rvalue conversion (4.1) unless it is applied to 151// 152// - a non-volatile glvalue of integral or enumeration type that refers to a 153// non-volatile const object with a preceding initialization, initialized with 154// a constant expression [Note: a string literal (2.14.5 [lex.string]) 155// corresponds to an array of such objects. -end note], or 156// 157// - a non-volatile glvalue of literal type that refers to a non-volatile 158// object defined with constexpr, or that refers to a sub-object of such an 159// object, or 160// 161// - a non-volatile glvalue of literal type that refers to a non-volatile 162// temporary object whose lifetime has not ended, initialized with a constant 163// expression; 164 165// FIXME: 166// 167// DR1312: The proposed wording for this defect has issues, so we instead 168// prohibit casts from pointers to cv void (see core-20842 and core-20845). 169// 170// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a 171// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U 172// are neither the same type nor similar types (4.4 [conv.qual]); 173 174// FIXME: 175// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that 176// refers to a non-active member of a union or a subobject thereof; 177 178// FIXME: 179// - an id-expression that refers to a variable or data member of reference type 180// unless the reference has a preceding initialization, initialized with a 181// constant expression; 182namespace References { 183 const int a = 2; 184 int &b = *const_cast<int*>(&a); 185 int c = 10; 186 int &d = c; 187 constexpr int e = 42; 188 int &f = const_cast<int&>(e); 189 extern int &g; 190 constexpr int &h(); // expected-note {{here}} 191 int &i = h(); 192 constexpr int &j() { return b; } 193 int &k = j(); 194 195 struct S { 196 int A : a; 197 int B : b; 198 int C : c; // expected-error {{constant expression}} 199 int D : d; // expected-error {{constant expression}} 200 int D2 : &d - &c + 1; 201 int E : e / 2; 202 int F : f - 11; 203 int G : g; // expected-error {{constant expression}} 204 int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}} 205 int I : i; // expected-error {{constant expression}} 206 int J : j(); 207 int K : k; 208 }; 209} 210 211// - a dynamic_cast (5.2.7); 212namespace DynamicCast { 213 struct S { int n; }; 214 constexpr S s { 16 }; 215 struct T { 216 int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}} 217 }; 218} 219 220// - a reinterpret_cast (5.2.10); 221namespace ReinterpretCast { 222 struct S { int n; }; 223 constexpr S s { 16 }; 224 struct T { 225 int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}} 226 }; 227 struct U { 228 int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}} 229 }; 230} 231 232// - a pseudo-destructor call (5.2.4); 233namespace PseudoDtor { 234 int k; 235 typedef int I; 236 struct T { 237 int n : (k.~I(), 0); // expected-error {{constant expression}} expected-note{{subexpression}} 238 }; 239} 240 241// - increment or decrement operations (5.2.6, 5.3.2); 242namespace IncDec { 243 int k = 2; 244 struct T { 245 int n : ++k; // expected-error {{constant expression}} 246 int m : --k; // expected-error {{constant expression}} 247 }; 248} 249 250// - a typeid expression (5.2.8) whose operand is of a polymorphic class type; 251namespace std { 252 struct type_info { 253 virtual ~type_info(); 254 const char *name; 255 }; 256} 257namespace TypeId { 258 struct S { virtual void f(); }; 259 constexpr S *p = 0; 260 constexpr const std::type_info &ti1 = typeid(*p); // expected-error {{must be initialized by a constant expression}} 261 262 // FIXME: Implement typeid evaluation. 263 struct T {} t; 264 constexpr const std::type_info &ti2 = typeid(t); // unexpected-error {{must be initialized by a constant expression}} 265} 266 267// - a new-expression (5.3.4); 268// - a delete-expression (5.3.5); 269namespace NewDelete { 270 int *p = 0; 271 struct T { 272 int n : *new int(4); // expected-error {{constant expression}} expected-note {{subexpression}} 273 int m : (delete p, 2); // expected-error {{constant expression}} expected-note {{subexpression}} 274 }; 275} 276 277// - a relational (5.9) or equality (5.10) operator where the result is 278// unspecified; 279namespace UnspecifiedRelations { 280 int a, b; 281 constexpr int *p = &a, *q = &b; 282 // C++11 [expr.rel]p2: If two pointers p and q of the same type point to 283 // different objects that are not members of the same array or to different 284 // functions, or if only one of them is null, the results of p<q, p>q, p<=q, 285 // and p>=q are unspecified. 286 constexpr bool u1 = p < q; // expected-error {{constant expression}} 287 constexpr bool u2 = p > q; // expected-error {{constant expression}} 288 constexpr bool u3 = p <= q; // expected-error {{constant expression}} 289 constexpr bool u4 = p >= q; // expected-error {{constant expression}} 290 constexpr bool u5 = p < 0; // expected-error {{constant expression}} 291 constexpr bool u6 = p <= 0; // expected-error {{constant expression}} 292 constexpr bool u7 = p > 0; // expected-error {{constant expression}} 293 constexpr bool u8 = p >= 0; // expected-error {{constant expression}} 294 constexpr bool u9 = 0 < q; // expected-error {{constant expression}} 295 constexpr bool u10 = 0 <= q; // expected-error {{constant expression}} 296 constexpr bool u11 = 0 > q; // expected-error {{constant expression}} 297 constexpr bool u12 = 0 >= q; // expected-error {{constant expression}} 298 void f(), g(); 299 300 constexpr void (*pf)() = &f, (*pg)() = &g; 301 constexpr bool u13 = pf < pg; // expected-error {{constant expression}} 302 constexpr bool u14 = pf == pg; 303 304 // FIXME: 305 // If two pointers point to non-static data members of the same object with 306 // different access control, the result is unspecified. 307 308 // FIXME: 309 // [expr.rel]p3: Pointers to void can be compared [...] if both pointers 310 // represent the same address or are both the null pointer [...]; otherwise 311 // the result is unspecified. 312 313 // FIXME: Implement comparisons of pointers to members. 314 // [expr.eq]p2: If either is a pointer to a virtual member function and 315 // neither is null, the result is unspecified. 316} 317 318// - an assignment or a compound assignment (5.17); or 319namespace Assignment { 320 int k; 321 struct T { 322 int n : (k = 9); // expected-error {{constant expression}} 323 int m : (k *= 2); // expected-error {{constant expression}} 324 }; 325 326 struct Literal { 327 constexpr Literal(const char *name) : name(name) {} 328 const char *name; 329 }; 330 struct Expr { 331 constexpr Expr(Literal l) : IsLiteral(true), l(l) {} 332 bool IsLiteral; 333 union { 334 Literal l; 335 // ... 336 }; 337 }; 338 struct MulEq { 339 constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {} 340 Expr LHS; 341 Expr RHS; 342 }; 343 constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); } 344 Literal a("a"); 345 Literal b("b"); 346 MulEq c = a *= b; // ok 347} 348 349// - a throw-expression (15.1) 350namespace Throw { 351 struct S { 352 int n : (throw "hello", 10); // expected-error {{constant expression}} expected-note {{subexpression}} 353 }; 354} 355 356// PR9999 357template<bool v> 358class bitWidthHolding { 359public: 360 static const 361 unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1); 362}; 363 364static const int width=bitWidthHolding<255>::width; 365 366template<bool b> 367struct always_false { 368 static const bool value = false; 369}; 370 371template<bool b> 372struct and_or { 373 static const bool and_value = b && and_or<always_false<b>::value>::and_value; 374 static const bool or_value = !b || and_or<always_false<b>::value>::or_value; 375}; 376 377static const bool and_value = and_or<true>::and_value; 378static const bool or_value = and_or<true>::or_value; 379