p2-0x.cpp revision 282e7e66748cc6dd14d6f7f2cb52e5373c531e61
115efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith// RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128 -triple i686-pc-linux-gnu 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// - an operation that would have undefined behavior [Note: including, for 113c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// example, signed integer overflow (Clause 5 [expr]), certain pointer 114c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain 115c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// shift operations (5.8 [expr.shift]) -end note]; 116c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace UndefinedBehavior { 117c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith void f(int n) { 118c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith switch (n) { 119c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard 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'}} 120395f1c08ff720be7df6535a86df14b6d36a2d57aRichard Smith case (int)0x80000000u: // ok 121395f1c08ff720be7df6535a86df14b6d36a2d57aRichard Smith case (int)10000000000ll: // expected-note {{here}} 122f72fccf533bca206af8e75d041c29db99e6a7f2cRichard Smith case (unsigned int)10000000000ll: // expected-error {{duplicate case value}} 123c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith case (int)(unsigned)(long long)4.4e9: // ok 1248ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard 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'}} 125c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith case (int)((float)1e37 / 1e30): // ok 126c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}} 127c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith break; 128c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 129c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 130c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 131789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int int_min = ~0x7fffffff; 132789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 133789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}} 134789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}} 135789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 1363df61308ddfbdba0897b762a129b5a38750c87d0Richard Smith constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 137789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 138789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}} 139789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_0 = 0 << 0; // ok 140789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_31 = 0 << 31; // ok 141789f9b6be5df6e5151ac35e68416cdf550db1196Richard 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}} 142789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok 143789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_unsigned_into_sign = 1u << 31; // ok 144789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_unsigned_overflow = 1024u << 31; // ok 145789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_signed_negative = (-3) << 1; // expected-error {{constant expression}} expected-note {{left shift of negative value -3}} 146789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_signed_ok = 1 << 30; // ok 147789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_signed_into_sign = 1 << 31; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}} 148789f9b6be5df6e5151ac35e68416cdf550db1196Richard 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}} 149789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shl_signed_ok2 = 1024 << 20; // ok 150789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 151789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}} 152789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shr_0 = 0 >> 0; // ok 153789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shr_31 = 0 >> 31; // ok 154789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}} 155789f9b6be5df6e5151ac35e68416cdf550db1196Richard Smith 156c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { 157c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int m; 158c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 159c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr S s = { 5 }; // expected-note {{declared here}} 160c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr const int *p = &s.m + 1; 161c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr const int &f(const int *q) { 162c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}} 163c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith } 16461e616206413d1779c7545c7a8ad1ce1129ad9c1Richard Smith constexpr int n = (f(p), 0); // expected-error {{constant expression}} expected-note {{in call to 'f(&s.m + 1)'}} 165c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 166244ee7b89a483fd3764637abdf95de2893b437d0Richard Smith int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}} 167c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 168b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 169b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith namespace Ptr { 170b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith struct A {}; 171b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith struct B : A { int n; }; 172b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith B a[3][3]; 173b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}} 174b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith B b = {}; 175b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}} 176b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}} 177b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}} 178b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}} 179b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith 180b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr A *na = nullptr; 181b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr B *nb = nullptr; 182b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}} 183b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}} 184b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith static_assert((A*)nb == 0, ""); 185b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith static_assert((B*)na == 0, ""); 186b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}} 187b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}} 188b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith 189b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith struct C { 190b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith constexpr int f() { return 0; } 191b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith } constexpr c = C(); 192b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith constexpr int k1 = c.f(); // ok 193b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{cannot call member function on null pointer}} 194b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{cannot call member function on pointer past the end of object}} 195b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith C c2; 196b04035a7b1a3c9b93cea72ae56dd2ea6e787bae9Richard Smith constexpr int k4 = c2.f(); // ok! 197f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 198f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr int diff1 = &a[2] - &a[0]; 199f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr int diff2 = &a[1][3] - &a[1][0]; 200f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr int diff3 = &a[2][0] - &a[1][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}} 201f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith static_assert(&a[2][0] == &a[1][3], ""); 202f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr int diff4 = (&b + 1) - &b; 203f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 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}} 204f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr int diff6 = &a[1][2].n - &a[1][2].n; 205f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 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}} 206b4e85ed51905fc94378d7b4ff62b06e0d08042b7Richard Smith } 2077b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 2087b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith namespace Overflow { 2097b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith // Signed int overflow. 2107b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok 2117b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }} 2127b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n3 = n1 + 1; // ok 2137b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }} 2147b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n5 = -65536 * 32768; // ok 2157b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }} 2167b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n7 = -n3 + -1; // ok 2177b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }} 2187b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n9 = n3 - 0; // ok 2197b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }} 2207b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n11 = -1 - n3; // ok 2217b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }} 2227b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }} 2237b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }} 2247b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }} 2257b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr signed char c1 = 100 * 2; // ok 2267b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr signed char c2 = '\x64' * '\2'; // also ok 2277b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr long long ll1 = 0x7fffffffffffffff; // ok 2287b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }} 2297b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr long long ll3 = -ll1 - 1; // ok 2307b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }} 2317b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }} 2327b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 23315efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith // Yikes. 23415efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith char melchizedek[2200000000]; 23515efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t; 23615efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0]; // ok 23715efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }} 23815efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u]; // ok 23915efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // expected-error {{constant expression}} expected-note {{ -2147483649 }} 24015efc4d597a47e6ba5794d4fd8d561bf6947233cRichard Smith 2417b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith // Unsigned int overflow. 2427b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith static_assert(65536u * 65536u == 0u, ""); // ok 2437b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith static_assert(4294967295u + 1u == 0u, ""); // ok 2447b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith static_assert(0u - 1u == 4294967295u, ""); // ok 2457b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith static_assert(~0u * ~0u == 1u, ""); // ok 2467b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith 2477b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith // Floating-point overflow and NaN. 2487b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f1 = 1e38f * 3.4028f; // ok 2497b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f2 = 1e38f * 3.4029f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}} 2507b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f3 = 1e38f / -.2939f; // ok 2517b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f4 = 1e38f / -.2938f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}} 2527b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f5 = 2e38f + 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}} 2537b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f6 = -2e38f - 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}} 2547b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces a NaN}} 2557b48a2986345480241f3b8209f71bb21b0530b4fRichard Smith } 256c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 257c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 258c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a lambda-expression (5.1.2); 259c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithstruct Lambda { 260c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // FIXME: clang crashes when trying to parse this! Revisit this check once 261c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // lambdas are fully implemented. 262c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith //int n : []{ return 1; }(); 263c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith}; 264c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 265c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1) unless it is applied to 2667098cbd601ad915aed22d4b5850da99359f25bf3Richard Smithnamespace LValueToRValue { 2677098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // - a non-volatile glvalue of integral or enumeration type that refers to a 2687098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // non-volatile const object with a preceding initialization, initialized 2697098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // with a constant expression [Note: a string literal (2.14.5 [lex.string]) 2707098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // corresponds to an array of such objects. -end note], or 271282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith volatile const int vi = 1; // expected-note 2{{here}} 2727098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith const int ci = 1; 2737098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith volatile const int &vrci = ci; 274282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}} 2757098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}} 276282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith static_assert(vrci, ""); // ok, vrci is converted to a prvalue before 277282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith // evaluation and loses its volatility in the 278282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith // conversion. 2797098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 2807098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // - a non-volatile glvalue of literal type that refers to a non-volatile 2817098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // object defined with constexpr, or that refers to a sub-object of such an 2827098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // object, or 2837098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith struct S { 2847098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith constexpr S(int=0) : i(1), v(1) {} 2857098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith constexpr S(const S &s) : i(2), v(2) {} 2867098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith int i; 287282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith volatile int v; // expected-note {{here}} 2887098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith }; 2897098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith constexpr S s; 290282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith constexpr volatile S vs; // expected-note 2{{here}} 2917098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith constexpr const volatile S &vrs = s; 2927098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(s.i, ""); 293282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile member 'v'}} 294282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}} 2957098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}} 296282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith static_assert(vrs.i, ""); // ok 2977098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith 2987098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // - a non-volatile glvalue of literal type that refers to a non-volatile 2997098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // temporary object whose lifetime has not ended, initialized with a 3007098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith // constant expression; 3017098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith constexpr volatile S f() { return S(); } 3027098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here! 303282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} 3047098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith} 305c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 3067098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// DR1312: The proposed wording for this defect has issues, so we ignore this 3077098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// bullet and instead prohibit casts from pointers to cv void (see core-20842 3087098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith// and core-20845). 309c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// 310c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a 311c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U 312c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// are neither the same type nor similar types (4.4 [conv.qual]); 313c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 314c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that 315c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// refers to a non-active member of a union or a subobject thereof; 316f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smithnamespace LValueToRValueUnion { 317f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing 318f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // of this. 319f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith union U { int a, b; } constexpr u = U(); 320f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith static_assert(u.a == 0, ""); 321f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr const int *bp = &u.b; 322f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr int b = *bp; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}} 323f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 324f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith extern const U pu; 325f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr const int *pua = &pu.a; 326f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr const int *pub = &pu.b; 327f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr U pu = { .b = 1 }; // expected-warning {{C99 feature}} 328f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr const int a2 = *pua; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}} 329f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr const int b2 = *pub; // ok 330f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith} 331c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 332c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an id-expression that refers to a variable or data member of reference type 333c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// unless the reference has a preceding initialization, initialized with a 334c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// constant expression; 335c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace References { 336c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const int a = 2; 337c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int &b = *const_cast<int*>(&a); 3387098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith int c = 10; // expected-note 2 {{here}} 339c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int &d = c; 340c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr int e = 42; 341c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int &f = const_cast<int&>(e); 342c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith extern int &g; 343099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith constexpr int &h(); // expected-note 2{{here}} 344099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}} 345c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr int &j() { return b; } 346c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int &k = j(); 347c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 348c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { 349c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int A : a; 350c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int B : b; 3517098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 3527098cbd601ad915aed22d4b5850da99359f25bf3Richard Smith int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}} 353c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int D2 : &d - &c + 1; 354c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int E : e / 2; 355c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int F : f - 11; 356c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int G : g; // expected-error {{constant expression}} 357c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}} 358099e7f647ccda915513f2b2ec53352dc756082d3Richard Smith int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}} 359c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int J : j(); 360c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int K : k; 361c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 362c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 363c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 364c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a dynamic_cast (5.2.7); 365c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace DynamicCast { 366c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { int n; }; 367c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr S s { 16 }; 368c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 369c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}} 370c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 371c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 372c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 373c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a reinterpret_cast (5.2.10); 374c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace ReinterpretCast { 375c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { int n; }; 376c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr S s { 16 }; 377c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 378c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}} 379c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 380c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct U { 381c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}} 382c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 383c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 384c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 385c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a pseudo-destructor call (5.2.4); 386c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace PseudoDtor { 387c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int k; 388c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith typedef int I; 389c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 390282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith int n : (k.~I(), 0); // expected-error {{constant expression}} 391c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 392c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 393c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 394c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - increment or decrement operations (5.2.6, 5.3.2); 395c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace IncDec { 396c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int k = 2; 397c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 398c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : ++k; // expected-error {{constant expression}} 399c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int m : --k; // expected-error {{constant expression}} 400c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 401c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 402c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 403c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a typeid expression (5.2.8) whose operand is of a polymorphic class type; 404c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace std { 405c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct type_info { 406c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith virtual ~type_info(); 407c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const char *name; 408c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 409c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 410c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace TypeId { 411c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { virtual void f(); }; 412c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr S *p = 0; 41347d2145675099893d702be4bc06bd9f26d8ddd13Richard 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'}} 414c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 415c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T {} t; 41647d2145675099893d702be4bc06bd9f26d8ddd13Richard Smith constexpr const std::type_info &ti2 = typeid(t); 417c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 418c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 419c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a new-expression (5.3.4); 420c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a delete-expression (5.3.5); 421c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace NewDelete { 422c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int *p = 0; 423c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 424282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith int n : *new int(4); // expected-error {{constant expression}} 425282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith int m : (delete p, 2); // expected-error {{constant expression}} 426c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 427c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 428c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 429c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a relational (5.9) or equality (5.10) operator where the result is 430c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// unspecified; 431c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace UnspecifiedRelations { 432c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int a, b; 433c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr int *p = &a, *q = &b; 434c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // C++11 [expr.rel]p2: If two pointers p and q of the same type point to 435c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // different objects that are not members of the same array or to different 436c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // functions, or if only one of them is null, the results of p<q, p>q, p<=q, 437c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // and p>=q are unspecified. 438c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u1 = p < q; // expected-error {{constant expression}} 439c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u2 = p > q; // expected-error {{constant expression}} 440c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u3 = p <= q; // expected-error {{constant expression}} 441c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u4 = p >= q; // expected-error {{constant expression}} 442c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u5 = p < 0; // expected-error {{constant expression}} 443c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u6 = p <= 0; // expected-error {{constant expression}} 444c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u7 = p > 0; // expected-error {{constant expression}} 445c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u8 = p >= 0; // expected-error {{constant expression}} 446c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u9 = 0 < q; // expected-error {{constant expression}} 447c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u10 = 0 <= q; // expected-error {{constant expression}} 448c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u11 = 0 > q; // expected-error {{constant expression}} 449c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u12 = 0 >= q; // expected-error {{constant expression}} 450c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith void f(), g(); 451c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 452c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr void (*pf)() = &f, (*pg)() = &g; 453c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u13 = pf < pg; // expected-error {{constant expression}} 454c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr bool u14 = pf == pg; 455c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 456c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // If two pointers point to non-static data members of the same object with 457c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // different access control, the result is unspecified. 458f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith struct A { 459f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith public: 460f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith constexpr A() : a(0), b(0) {} 461f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith int a; 462f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 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}} 463f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith private: 464f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith int b; 465f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith }; 466f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith class B { 467f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith public: 468f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith A a; 469f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 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}} 470f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith protected: 471f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith A b; 472f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith }; 473f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 474f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // If two pointers point to different base sub-objects of the same object, or 475f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // one points to a base subobject and the other points to a member, the result 476f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // of the comparison is unspecified. This is not explicitly called out by 477f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // [expr.rel]p2, but is covered by 'Other pointer comparisons are 478f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith // unspecified'. 479f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith struct C { 480f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith int c[2]; 481f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith }; 482f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith struct D { 483f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith int d; 484f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith }; 485f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith struct E : C, D { 486f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith struct Inner { 487f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith int f; 488f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } e; 489f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith } e; 490f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 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}} 491f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 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}} 492f15fda02e9c8c82b4a716618f4010b9af8bff796Richard Smith 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}} 493c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 494c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // [expr.rel]p3: Pointers to void can be compared [...] if both pointers 495c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // represent the same address or are both the null pointer [...]; otherwise 496c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // the result is unspecified. 49782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith struct S { int a, b; } s; 49882f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr void *null = 0; 49982f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr void *pv = (void*)&s.a; 50082f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr void *qv = (void*)&s.b; 50182f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v1 = null < 0; 50282f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v2 = null < pv; // expected-error {{constant expression}} 50382f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v3 = null == pv; // ok 50482f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v4 = qv == pv; // ok 50582f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}} 50682f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v6 = qv > null; // expected-error {{constant expression}} 50782f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v7 = qv <= (void*)&s.b; // ok 50882f28583b8e81ae9b61635a0652f6a45623df16dRichard Smith constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}} 509c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 510c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 511c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - an assignment or a compound assignment (5.17); or 512c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace Assignment { 513c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int k; 514c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct T { 515c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int n : (k = 9); // expected-error {{constant expression}} 516c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith int m : (k *= 2); // expected-error {{constant expression}} 517c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 518c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 519c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct Literal { 520c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr Literal(const char *name) : name(name) {} 521c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith const char *name; 522c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 523c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct Expr { 524c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr Expr(Literal l) : IsLiteral(true), l(l) {} 525c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith bool IsLiteral; 526c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith union { 527c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Literal l; 528c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith // ... 529c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 530c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 531c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct MulEq { 532c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {} 533c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Expr LHS; 534c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Expr RHS; 535c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 536c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); } 537c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Literal a("a"); 538c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith Literal b("b"); 539c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith MulEq c = a *= b; // ok 540c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 541c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith 542c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith// - a throw-expression (15.1) 543c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smithnamespace Throw { 544c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith struct S { 545282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith int n : (throw "hello", 10); // expected-error {{constant expression}} 546c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith }; 547c1c5f27c64dfc3332d53ad30e44d626e4f9afac3Richard Smith} 54863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 54963fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor// PR9999 5508ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithtemplate<unsigned int v> 55163fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorclass bitWidthHolding { 55263fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorpublic: 55363fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor static const 55463fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1); 55563fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor}; 55663fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 55763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const int width=bitWidthHolding<255>::width; 55863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 55963fe6814f339df30b8463b39995947cbdf920e48Douglas Gregortemplate<bool b> 56063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstruct always_false { 56163fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor static const bool value = false; 56263fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor}; 56363fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 56463fe6814f339df30b8463b39995947cbdf920e48Douglas Gregortemplate<bool b> 56563fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstruct and_or { 56663fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor static const bool and_value = b && and_or<always_false<b>::value>::and_value; 56763fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor static const bool or_value = !b || and_or<always_false<b>::value>::or_value; 56863fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor}; 56963fe6814f339df30b8463b39995947cbdf920e48Douglas Gregor 57063fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const bool and_value = and_or<true>::and_value; 57163fe6814f339df30b8463b39995947cbdf920e48Douglas Gregorstatic const bool or_value = and_or<true>::or_value; 5728ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith 5738ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(and_value == false, ""); 5748ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(or_value == true, ""); 575