p2-0x.cpp revision 7b48a2986345480241f3b8209f71bb21b0530b4f
11e7fc3d31e17fbe314f86de96aac6e9a2f297167Richard Smith// RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128 2c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 3c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// A conditional-expression is a core constant expression unless it involves one 4c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// of the following as a potentially evaluated subexpression [...]: 5c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 6c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - this (5.1.1 [expr.prim.general]) [Note: when evaluating a constant 7c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// expression, function invocation substitution (7.1.5 [dcl.constexpr]) 8c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// replaces each occurrence of this in a constexpr member function with a 9c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// pointer to the class object. -end note]; 10c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct This { 11c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int this1 : this1; // expected-error {{undeclared}} 12c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int this2 : this->this1; // expected-error {{invalid}} 13c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith void this3() { 14c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n1[this->this1]; // expected-warning {{variable length array}} 15c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n2[this1]; // expected-warning {{variable length array}} 16c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith (void)n1, (void)n2; 17c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 18c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}; 19c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 20c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of a function other than a constexpr constructor for a 21c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// literal class or a constexpr function [ Note: Overload resolution (13.3) 22c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// is applied as usual - end note ]; 23c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct NonConstexpr1 { 24c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith static int f() { return 1; } // expected-note {{here}} 25c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : f(); // expected-error {{constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}} 26c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}; 27c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct NonConstexpr2 { 28c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr NonConstexpr2(); // expected-note {{here}} 29c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n; 30c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}; 31c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct NonConstexpr3 { 32c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith NonConstexpr3(); 33c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}} 34c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}; 35c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct NonConstexpr4 { 3651201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith NonConstexpr4(); // expected-note {{declared here}} 37c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n; 38c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}; 39c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct NonConstexpr5 { 4051201882382fb40c9456a06c7f93d6ddd4a57712Richard Smith int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-constexpr constructor 'NonConstexpr4' cannot be used in a constant expression}} 41c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}; 42c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 43c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of an undefined constexpr function or an undefined 44c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// constexpr constructor; 45c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct UndefinedConstexpr { 46c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr UndefinedConstexpr(); 47c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith static constexpr int undefinedConstexpr1(); // expected-note {{here}} 48c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int undefinedConstexpr2 : undefinedConstexpr1(); // expected-error {{constant expression}} expected-note {{undefined function 'undefinedConstexpr1' cannot be used in a constant expression}} 49c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}; 50c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 51c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of a constexpr function with arguments that, when substituted 52c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// by function invocation substitution (7.1.5), do not produce a constant 53c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// expression; 54c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace NonConstExprReturn { 55c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith static constexpr const int &id_ref(const int &n) { 56c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return n; // expected-note {{reference to temporary cannot be returned from a constexpr function}} 57c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 58c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct NonConstExprFunction { 5908d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith int n : id_ref( // expected-error {{constant expression}} expected-note {{in call to 'id_ref(16)'}} 60c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 16 // expected-note {{temporary created here}} 61c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith ); 62c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 63c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr const int *address_of(const int &a) { 64c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return &a; // expected-note {{pointer to 'n' cannot be returned from a constexpr function}} 65c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 66c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr const int *return_param(int n) { // expected-note {{declared here}} 6708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith return address_of(n); // expected-note {{in call to 'address_of(n)'}} 68c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 69c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { 7008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith int n : *return_param(0); // expected-error {{constant expression}} expected-note {{in call to 'return_param(0)'}} 71c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 72c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 73c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 74c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of a constexpr constructor with arguments that, when 75c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// substituted by function invocation substitution (7.1.5), do not produce all 76c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// constant expressions for the constructor calls and full-expressions in the 77c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// mem-initializers (including conversions); 78c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace NonConstExprCtor { 79c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 80c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr T(const int &r) : 81099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith r(r) { // expected-note 2{{reference to temporary cannot be used to initialize a member in a constant expression}} 82c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 83c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const int &r; 84c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 85c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr int n = 0; 86c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr T t1(n); // ok 87099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith 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)'}} 88c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 89c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { 9008d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith int n : T(4).r; // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'T(4)'}} 91c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 92c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 93c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 94c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an invocation of a constexpr function or a constexpr constructor that would 95c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// exceed the implementation-defined recursion limits (see Annex B); 96c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace RecursionLimits { 97c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr int RecurseForever(int n) { 981e7fc3d31e17fbe314f86de96aac6e9a2f297167Richard Smith 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}} 99c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 100c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct AlsoRecurseForever { 101c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr AlsoRecurseForever(int n) : 1021e7fc3d31e17fbe314f86de96aac6e9a2f297167Richard Smith 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}} 103c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith {} 104c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n; 105c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 106c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { 10708d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}} 10808d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}} 109c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 110c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 111c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 112c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// FIXME: 113c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an operation that would have undefined behavior [Note: including, for 114c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// example, signed integer overflow (Clause 5 [expr]), certain pointer 115c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain 116c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// shift operations (5.8 [expr.shift]) -end note]; 117c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace UndefinedBehavior { 118c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith void f(int n) { 119c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith switch (n) { 120c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}} 121395f1c08ff720be7df6535a86df14b6d36a2d57aRichard Smith case (int)0x80000000u: // ok 122395f1c08ff720be7df6535a86df14b6d36a2d57aRichard Smith case (int)10000000000ll: // expected-note {{here}} 123f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith case (unsigned int)10000000000ll: // expected-error {{duplicate case value}} 124c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith case (int)(unsigned)(long long)4.4e9: // ok 1258ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}} 126c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith case (int)((float)1e37 / 1e30): // ok 127c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}} 128c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith break; 129c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 130c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 131c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 132789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int int_min = ~0x7fffffff; 133789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 134789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}} 135789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}} 136789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 1373df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 138789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 139789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}} 140789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_0 = 0 << 0; // ok 141789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_31 = 0 << 31; // ok 142789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 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}} 143789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok 144789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_unsigned_into_sign = 1u << 31; // ok 145789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_unsigned_overflow = 1024u << 31; // ok 146789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_signed_negative = (-3) << 1; // expected-error {{constant expression}} expected-note {{left shift of negative value -3}} 147789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_signed_ok = 1 << 30; // ok 148789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_signed_into_sign = 1 << 31; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 149789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 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}} 150789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_signed_ok2 = 1024 << 20; // ok 151789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 152789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}} 153789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shr_0 = 0 >> 0; // ok 154789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shr_31 = 0 >> 31; // ok 155789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}} 156789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 157c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { 158c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int m; 159c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 160c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr S s = { 5 }; // expected-note {{declared here}} 161c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr const int *p = &s.m + 1; 162c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr const int &f(const int *q) { 163c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}} 164c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 16561e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith constexpr int n = (f(p), 0); // expected-error {{constant expression}} expected-note {{in call to 'f(&s.m + 1)'}} 166c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 167244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} 168c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 169b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 170b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith namespace Ptr { 171b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith struct A {}; 172b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith struct B : A { int n; }; 173b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith B a[3][3]; 174b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}} 175b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith B b = {}; 176b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}} 177b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}} 178b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}} 179b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}} 180b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 181b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr A *na = nullptr; 182b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr B *nb = nullptr; 183b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}} 184b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}} 185b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith static_assert((A*)nb == 0, ""); 186b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith static_assert((B*)na == 0, ""); 187b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}} 188b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}} 189b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith 190b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith struct C { 191b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith constexpr int f() { return 0; } 192b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith } constexpr c = C(); 193b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith constexpr int k1 = c.f(); // ok 194b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{cannot call member function on null pointer}} 195b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{cannot call member function on pointer past the end of object}} 196b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith C c2; 197b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith constexpr int k4 = c2.f(); // ok! 198b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 1997b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 2007b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith namespace Overflow { 2017b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith // Signed int overflow. 2027b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok 2037b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }} 2047b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n3 = n1 + 1; // ok 2057b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }} 2067b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n5 = -65536 * 32768; // ok 2077b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }} 2087b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n7 = -n3 + -1; // ok 2097b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }} 2107b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n9 = n3 - 0; // ok 2117b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }} 2127b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n11 = -1 - n3; // ok 2137b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }} 2147b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }} 2157b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }} 2167b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }} 2177b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr signed char c1 = 100 * 2; // ok 2187b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr signed char c2 = '\x64' * '\2'; // also ok 2197b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr long long ll1 = 0x7fffffffffffffff; // ok 2207b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }} 2217b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr long long ll3 = -ll1 - 1; // ok 2227b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }} 2237b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }} 2247b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 2257b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith // Unsigned int overflow. 2267b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith static_assert(65536u * 65536u == 0u, ""); // ok 2277b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith static_assert(4294967295u + 1u == 0u, ""); // ok 2287b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith static_assert(0u - 1u == 4294967295u, ""); // ok 2297b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith static_assert(~0u * ~0u == 1u, ""); // ok 2307b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 2317b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith // Floating-point overflow and NaN. 2327b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f1 = 1e38f * 3.4028f; // ok 2337b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f2 = 1e38f * 3.4029f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}} 2347b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f3 = 1e38f / -.2939f; // ok 2357b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f4 = 1e38f / -.2938f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}} 2367b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f5 = 2e38f + 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}} 2377b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f6 = -2e38f - 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}} 2387b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces a NaN}} 2397b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith } 240c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 241c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 242c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a lambda-expression (5.1.2); 243c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct Lambda { 244c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // FIXME: clang crashes when trying to parse this! Revisit this check once 245c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // lambdas are fully implemented. 246c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith //int n : []{ return 1; }(); 247c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}; 248c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 249c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1) unless it is applied to 2507098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithnamespace LValueToRValue { 2517098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // - a non-volatile glvalue of integral or enumeration type that refers to a 2527098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // non-volatile const object with a preceding initialization, initialized 2537098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // with a constant expression [Note: a string literal (2.14.5 [lex.string]) 2547098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // corresponds to an array of such objects. -end note], or 2557098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith volatile const int vi = 1; // expected-note {{here}} 2567098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith const int ci = 1; 2577098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith volatile const int &vrci = ci; 2587098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}} 2597098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}} 2607098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 2617098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 2627098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // - a non-volatile glvalue of literal type that refers to a non-volatile 2637098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // object defined with constexpr, or that refers to a sub-object of such an 2647098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // object, or 2657098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith struct S { 2667098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith constexpr S(int=0) : i(1), v(1) {} 2677098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith constexpr S(const S &s) : i(2), v(2) {} 2687098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith int i; 2697098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith volatile int v; 2707098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith }; 2717098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith constexpr S s; 2727098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith constexpr volatile S vs; // expected-note {{here}} 2737098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith constexpr const volatile S &vrs = s; 2747098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(s.i, ""); 2757098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 2767098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 2777098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}} 2787098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}} 2797098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 2807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // - a non-volatile glvalue of literal type that refers to a non-volatile 2817098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // temporary object whose lifetime has not ended, initialized with a 2827098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // constant expression; 2837098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith constexpr volatile S f() { return S(); } 2847098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here! 2857098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{subexpression}} 2867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith} 287c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 288c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// FIXME: 289c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// 2907098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// DR1312: The proposed wording for this defect has issues, so we ignore this 2917098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// bullet and instead prohibit casts from pointers to cv void (see core-20842 2927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// and core-20845). 293c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// 294c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a 295c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U 296c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// are neither the same type nor similar types (4.4 [conv.qual]); 297c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 298c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// FIXME: 299c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that 300c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// refers to a non-active member of a union or a subobject thereof; 301c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 302c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an id-expression that refers to a variable or data member of reference type 303c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// unless the reference has a preceding initialization, initialized with a 304c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// constant expression; 305c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace References { 306c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const int a = 2; 307c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int &b = *const_cast<int*>(&a); 3087098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith int c = 10; // expected-note 2 {{here}} 309c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int &d = c; 310c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr int e = 42; 311c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int &f = const_cast<int&>(e); 312c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith extern int &g; 313099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith constexpr int &h(); // expected-note 2{{here}} 314099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}} 315c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr int &j() { return b; } 316c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int &k = j(); 317c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 318c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { 319c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int A : a; 320c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int B : b; 3217098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 3227098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 323c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int D2 : &d - &c + 1; 324c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int E : e / 2; 325c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int F : f - 11; 326c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int G : g; // expected-error {{constant expression}} 327c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}} 328099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}} 329c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int J : j(); 330c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int K : k; 331c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 332c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 333c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 334c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a dynamic_cast (5.2.7); 335c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace DynamicCast { 336c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { int n; }; 337c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr S s { 16 }; 338c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 339c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}} 340c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 341c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 342c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 343c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a reinterpret_cast (5.2.10); 344c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace ReinterpretCast { 345c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { int n; }; 346c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr S s { 16 }; 347c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 348c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}} 349c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 350c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct U { 351c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}} 352c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 353c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 354c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 355c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a pseudo-destructor call (5.2.4); 356c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace PseudoDtor { 357c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int k; 358c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith typedef int I; 359c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 360c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : (k.~I(), 0); // expected-error {{constant expression}} expected-note{{subexpression}} 361c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 362c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 363c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 364c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - increment or decrement operations (5.2.6, 5.3.2); 365c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace IncDec { 366c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int k = 2; 367c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 368c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : ++k; // expected-error {{constant expression}} 369c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int m : --k; // expected-error {{constant expression}} 370c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 371c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 372c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 373c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a typeid expression (5.2.8) whose operand is of a polymorphic class type; 374c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace std { 375c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct type_info { 376c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith virtual ~type_info(); 377c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const char *name; 378c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 379c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 380c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace TypeId { 381c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { virtual void f(); }; 382c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr S *p = 0; 38347d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith 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'}} 384c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 385c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T {} t; 38647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith constexpr const std::type_info &ti2 = typeid(t); 387c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 388c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 389c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a new-expression (5.3.4); 390c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a delete-expression (5.3.5); 391c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace NewDelete { 392c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int *p = 0; 393c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 394c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : *new int(4); // expected-error {{constant expression}} expected-note {{subexpression}} 395c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int m : (delete p, 2); // expected-error {{constant expression}} expected-note {{subexpression}} 396c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 397c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 398c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 399c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a relational (5.9) or equality (5.10) operator where the result is 400c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// unspecified; 401c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace UnspecifiedRelations { 402c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int a, b; 403c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr int *p = &a, *q = &b; 404c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // C++11 [expr.rel]p2: If two pointers p and q of the same type point to 405c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // different objects that are not members of the same array or to different 406c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // functions, or if only one of them is null, the results of p<q, p>q, p<=q, 407c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // and p>=q are unspecified. 408c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u1 = p < q; // expected-error {{constant expression}} 409c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u2 = p > q; // expected-error {{constant expression}} 410c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u3 = p <= q; // expected-error {{constant expression}} 411c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u4 = p >= q; // expected-error {{constant expression}} 412c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u5 = p < 0; // expected-error {{constant expression}} 413c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u6 = p <= 0; // expected-error {{constant expression}} 414c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u7 = p > 0; // expected-error {{constant expression}} 415c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u8 = p >= 0; // expected-error {{constant expression}} 416c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u9 = 0 < q; // expected-error {{constant expression}} 417c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u10 = 0 <= q; // expected-error {{constant expression}} 418c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u11 = 0 > q; // expected-error {{constant expression}} 419c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u12 = 0 >= q; // expected-error {{constant expression}} 420c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith void f(), g(); 421c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 422c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr void (*pf)() = &f, (*pg)() = &g; 423c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u13 = pf < pg; // expected-error {{constant expression}} 424c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u14 = pf == pg; 425c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 426c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // FIXME: 427c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // If two pointers point to non-static data members of the same object with 428c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // different access control, the result is unspecified. 429c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 430c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // [expr.rel]p3: Pointers to void can be compared [...] if both pointers 431c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // represent the same address or are both the null pointer [...]; otherwise 432c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // the result is unspecified. 43382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith struct S { int a, b; } s; 43482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr void *null = 0; 43582f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr void *pv = (void*)&s.a; 43682f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr void *qv = (void*)&s.b; 43782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v1 = null < 0; 43882f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v2 = null < pv; // expected-error {{constant expression}} 43982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v3 = null == pv; // ok 44082f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v4 = qv == pv; // ok 44182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}} 44282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v6 = qv > null; // expected-error {{constant expression}} 44382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v7 = qv <= (void*)&s.b; // ok 44482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}} 445c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 446c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // FIXME: Implement comparisons of pointers to members. 447c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // [expr.eq]p2: If either is a pointer to a virtual member function and 448c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // neither is null, the result is unspecified. 449c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 450c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 451c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an assignment or a compound assignment (5.17); or 452c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace Assignment { 453c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int k; 454c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 455c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : (k = 9); // expected-error {{constant expression}} 456c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int m : (k *= 2); // expected-error {{constant expression}} 457c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 458c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 459c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct Literal { 460c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr Literal(const char *name) : name(name) {} 461c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const char *name; 462c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 463c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct Expr { 464c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr Expr(Literal l) : IsLiteral(true), l(l) {} 465c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith bool IsLiteral; 466c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith union { 467c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Literal l; 468c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // ... 469c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 470c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 471c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct MulEq { 472c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {} 473c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Expr LHS; 474c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Expr RHS; 475c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 476c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); } 477c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Literal a("a"); 478c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Literal b("b"); 479c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith MulEq c = a *= b; // ok 480c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 481c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 482c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a throw-expression (15.1) 483c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace Throw { 484c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { 485c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : (throw "hello", 10); // expected-error {{constant expression}} expected-note {{subexpression}} 486c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 487c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 48863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 48963fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor// PR9999 4908ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithtemplate<unsigned int v> 49163fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorclass bitWidthHolding { 49263fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorpublic: 49363fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor static const 49463fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1); 49563fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor}; 49663fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 49763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const int width=bitWidthHolding<255>::width; 49863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 49963fe6814f339df30b8463b39995947cbdf920e48Douglas Gregortemplate<bool b> 50063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstruct always_false { 50163fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor static const bool value = false; 50263fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor}; 50363fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 50463fe6814f339df30b8463b39995947cbdf920e48Douglas Gregortemplate<bool b> 50563fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstruct and_or { 50663fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor static const bool and_value = b && and_or<always_false<b>::value>::and_value; 50763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor static const bool or_value = !b || and_or<always_false<b>::value>::or_value; 50863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor}; 50963fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 51063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const bool and_value = and_or<true>::and_value; 51163fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const bool or_value = and_or<true>::or_value; 5128ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith 5138ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(and_value == false, ""); 5148ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(or_value == true, ""); 515