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