blob: 43d683aeca4f63058c6fa89db97d6adba95cd6ca [file] [log] [blame]
Richard Smith1b470412012-02-01 08:10:20 +00001// RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify -fcxx-exceptions %s -fconstexpr-depth 128 -triple i686-pc-linux-gnu
Richard Smith357362d2011-12-13 06:39:58 +00002
3// A conditional-expression is a core constant expression unless it involves one
4// of the following as a potentially evaluated subexpression [...]:
5
6// - this (5.1.1 [expr.prim.general]) [Note: when evaluating a constant
7// expression, function invocation substitution (7.1.5 [dcl.constexpr])
8// replaces each occurrence of this in a constexpr member function with a
9// pointer to the class object. -end note];
10struct This {
11 int this1 : this1; // expected-error {{undeclared}}
12 int this2 : this->this1; // expected-error {{invalid}}
13 void this3() {
14 int n1[this->this1]; // expected-warning {{variable length array}}
15 int n2[this1]; // expected-warning {{variable length array}}
16 (void)n1, (void)n2;
17 }
18};
19
20// - an invocation of a function other than a constexpr constructor for a
21// literal class or a constexpr function [ Note: Overload resolution (13.3)
22// is applied as usual - end note ];
23struct NonConstexpr1 {
24 static int f() { return 1; } // expected-note {{here}}
25 int n : f(); // expected-error {{constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
26};
27struct NonConstexpr2 {
28 constexpr NonConstexpr2(); // expected-note {{here}}
29 int n;
30};
31struct NonConstexpr3 {
32 NonConstexpr3();
33 int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}}
34};
35struct NonConstexpr4 {
Richard Smithfddd3842011-12-30 21:15:51 +000036 NonConstexpr4(); // expected-note {{declared here}}
Richard Smith357362d2011-12-13 06:39:58 +000037 int n;
38};
39struct NonConstexpr5 {
Richard Smithfddd3842011-12-30 21:15:51 +000040 int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-constexpr constructor 'NonConstexpr4' cannot be used in a constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +000041};
42
43// - an invocation of an undefined constexpr function or an undefined
44// constexpr constructor;
45struct UndefinedConstexpr {
46 constexpr UndefinedConstexpr();
47 static constexpr int undefinedConstexpr1(); // expected-note {{here}}
48 int undefinedConstexpr2 : undefinedConstexpr1(); // expected-error {{constant expression}} expected-note {{undefined function 'undefinedConstexpr1' cannot be used in a constant expression}}
49};
50
51// - an invocation of a constexpr function with arguments that, when substituted
52// by function invocation substitution (7.1.5), do not produce a constant
53// expression;
54namespace NonConstExprReturn {
55 static constexpr const int &id_ref(const int &n) {
56 return n; // expected-note {{reference to temporary cannot be returned from a constexpr function}}
57 }
58 struct NonConstExprFunction {
Richard Smithf6f003a2011-12-16 19:06:07 +000059 int n : id_ref( // expected-error {{constant expression}} expected-note {{in call to 'id_ref(16)'}}
Richard Smith357362d2011-12-13 06:39:58 +000060 16 // expected-note {{temporary created here}}
61 );
62 };
63 constexpr const int *address_of(const int &a) {
64 return &a; // expected-note {{pointer to 'n' cannot be returned from a constexpr function}}
65 }
66 constexpr const int *return_param(int n) { // expected-note {{declared here}}
Richard Smithf6f003a2011-12-16 19:06:07 +000067 return address_of(n); // expected-note {{in call to 'address_of(n)'}}
Richard Smith357362d2011-12-13 06:39:58 +000068 }
69 struct S {
Richard Smithf6f003a2011-12-16 19:06:07 +000070 int n : *return_param(0); // expected-error {{constant expression}} expected-note {{in call to 'return_param(0)'}}
Richard Smith357362d2011-12-13 06:39:58 +000071 };
72}
73
74// - an invocation of a constexpr constructor with arguments that, when
75// substituted by function invocation substitution (7.1.5), do not produce all
76// constant expressions for the constructor calls and full-expressions in the
77// mem-initializers (including conversions);
78namespace NonConstExprCtor {
79 struct T {
80 constexpr T(const int &r) :
Richard Smithd0b4dd62011-12-19 06:19:21 +000081 r(r) { // expected-note 2{{reference to temporary cannot be used to initialize a member in a constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +000082 }
83 const int &r;
84 };
85 constexpr int n = 0;
86 constexpr T t1(n); // ok
Richard Smithd0b4dd62011-12-19 06:19:21 +000087 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)'}}
Richard Smith357362d2011-12-13 06:39:58 +000088
89 struct S {
Richard Smithf6f003a2011-12-16 19:06:07 +000090 int n : T(4).r; // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'T(4)'}}
Richard Smith357362d2011-12-13 06:39:58 +000091 };
92}
93
94// - an invocation of a constexpr function or a constexpr constructor that would
95// exceed the implementation-defined recursion limits (see Annex B);
96namespace RecursionLimits {
97 constexpr int RecurseForever(int n) {
Richard Smith864711ee2011-12-16 21:59:02 +000098 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 Smith357362d2011-12-13 06:39:58 +000099 }
100 struct AlsoRecurseForever {
101 constexpr AlsoRecurseForever(int n) :
Richard Smith864711ee2011-12-16 21:59:02 +0000102 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 Smith357362d2011-12-13 06:39:58 +0000103 {}
104 int n;
105 };
106 struct S {
Richard Smithf6f003a2011-12-16 19:06:07 +0000107 int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}}
108 int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}}
Richard Smith357362d2011-12-13 06:39:58 +0000109 };
110}
111
Richard Smith90cacbb2012-02-08 08:11:33 +0000112// DR1458: taking the address of an object of incomplete class type
113namespace IncompleteClassTypeAddr {
114 struct S; // expected-note {{forward}}
115 extern S s;
116 constexpr S *p = &s; // expected-error {{constant expression}} expected-note {{cannot take address of object of incomplete class type 'IncompleteClassTypeAddr::S' in a constant expression}}
117
118 extern S sArr[];
119 constexpr S (*p2)[] = &sArr; // ok
120
121 struct S {
122 constexpr S *operator&() { return nullptr; }
123 };
124 constexpr S *q = &s;
125 static_assert(!q, "");
126}
127
Richard Smith357362d2011-12-13 06:39:58 +0000128// - an operation that would have undefined behavior [Note: including, for
129// example, signed integer overflow (Clause 5 [expr]), certain pointer
130// arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
131// shift operations (5.8 [expr.shift]) -end note];
132namespace UndefinedBehavior {
133 void f(int n) {
134 switch (n) {
135 case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}}
Richard Smith006bfc92012-01-31 01:47:46 +0000136 case (int)0x80000000u: // ok
137 case (int)10000000000ll: // expected-note {{here}}
Richard Smith911e1422012-01-30 22:27:01 +0000138 case (unsigned int)10000000000ll: // expected-error {{duplicate case value}}
Richard Smith357362d2011-12-13 06:39:58 +0000139 case (int)(unsigned)(long long)4.4e9: // ok
Richard Smithf8379a02012-01-18 23:55:52 +0000140 case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value 1.0E+300 is outside the range of representable values of type 'float'}}
Richard Smith357362d2011-12-13 06:39:58 +0000141 case (int)((float)1e37 / 1e30): // ok
142 case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}}
143 break;
144 }
145 }
146
Richard Smithfe800032012-01-31 04:08:20 +0000147 constexpr int int_min = ~0x7fffffff;
148 constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
149 constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
150 constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
151 constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
Richard Smith000e9aa2012-01-31 23:24:19 +0000152 constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
Richard Smithfe800032012-01-31 04:08:20 +0000153
154 constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
155 constexpr int shl_0 = 0 << 0; // ok
156 constexpr int shl_31 = 0 << 31; // ok
157 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}}
158 constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok
159 constexpr int shl_unsigned_into_sign = 1u << 31; // ok
160 constexpr int shl_unsigned_overflow = 1024u << 31; // ok
161 constexpr int shl_signed_negative = (-3) << 1; // expected-error {{constant expression}} expected-note {{left shift of negative value -3}}
162 constexpr int shl_signed_ok = 1 << 30; // ok
Richard Smithda7c4ba2012-02-08 06:14:53 +0000163 constexpr int shl_signed_into_sign = 1 << 31; // ok (DR1457)
164 constexpr int shl_signed_into_sign_2 = 0x7fffffff << 1; // ok (DR1457)
165 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}}
166 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}}
167 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 Smithfe800032012-01-31 04:08:20 +0000168 constexpr int shl_signed_ok2 = 1024 << 20; // ok
169
170 constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
171 constexpr int shr_0 = 0 >> 0; // ok
172 constexpr int shr_31 = 0 >> 31; // ok
173 constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}}
174
Richard Smith357362d2011-12-13 06:39:58 +0000175 struct S {
176 int m;
177 };
178 constexpr S s = { 5 }; // expected-note {{declared here}}
179 constexpr const int *p = &s.m + 1;
180 constexpr const int &f(const int *q) {
181 return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}}
182 }
Richard Smith02ab9c22012-01-12 06:08:57 +0000183 constexpr int n = (f(p), 0); // expected-error {{constant expression}} expected-note {{in call to 'f(&s.m + 1)'}}
Richard Smith357362d2011-12-13 06:39:58 +0000184 struct T {
Richard Smith2ec40612012-01-15 03:51:30 +0000185 int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
Richard Smith357362d2011-12-13 06:39:58 +0000186 };
Richard Smitha8105bc2012-01-06 16:39:00 +0000187
188 namespace Ptr {
189 struct A {};
190 struct B : A { int n; };
191 B a[3][3];
192 constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
193 B b = {};
194 constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
195 constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
196 constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
197 constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
198
199 constexpr A *na = nullptr;
200 constexpr B *nb = nullptr;
201 constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
202 constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
203 static_assert((A*)nb == 0, "");
204 static_assert((B*)na == 0, "");
205 constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
206 constexpr const int &np = (*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot access array element of null pointer}}
Richard Smith47b34932012-02-01 02:39:43 +0000207
208 struct C {
209 constexpr int f() { return 0; }
210 } constexpr c = C();
211 constexpr int k1 = c.f(); // ok
212 constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{cannot call member function on null pointer}}
213 constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{cannot call member function on pointer past the end of object}}
214 C c2;
215 constexpr int k4 = c2.f(); // ok!
Richard Smith84f6dcf2012-02-02 01:16:57 +0000216
217 constexpr int diff1 = &a[2] - &a[0];
218 constexpr int diff2 = &a[1][3] - &a[1][0];
219 constexpr int diff3 = &a[2][0] - &a[1][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
220 static_assert(&a[2][0] == &a[1][3], "");
221 constexpr int diff4 = (&b + 1) - &b;
222 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}}
223 constexpr int diff6 = &a[1][2].n - &a[1][2].n;
224 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 Smitha8105bc2012-01-06 16:39:00 +0000225 }
Richard Smithc8042322012-02-01 05:53:12 +0000226
227 namespace Overflow {
228 // Signed int overflow.
229 constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok
230 constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
231 constexpr int n3 = n1 + 1; // ok
232 constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
233 constexpr int n5 = -65536 * 32768; // ok
234 constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
235 constexpr int n7 = -n3 + -1; // ok
236 constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
237 constexpr int n9 = n3 - 0; // ok
238 constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
239 constexpr int n11 = -1 - n3; // ok
240 constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
241 constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }}
242 constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }}
243 constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }}
244 constexpr signed char c1 = 100 * 2; // ok
245 constexpr signed char c2 = '\x64' * '\2'; // also ok
246 constexpr long long ll1 = 0x7fffffffffffffff; // ok
247 constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }}
248 constexpr long long ll3 = -ll1 - 1; // ok
249 constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }}
250 constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }}
251
Richard Smith1b470412012-02-01 08:10:20 +0000252 // Yikes.
253 char melchizedek[2200000000];
254 typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t;
255 constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0]; // ok
256 constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }}
257 constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u]; // ok
258 constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // expected-error {{constant expression}} expected-note {{ -2147483649 }}
259
Richard Smithc8042322012-02-01 05:53:12 +0000260 // Unsigned int overflow.
261 static_assert(65536u * 65536u == 0u, ""); // ok
262 static_assert(4294967295u + 1u == 0u, ""); // ok
263 static_assert(0u - 1u == 4294967295u, ""); // ok
264 static_assert(~0u * ~0u == 1u, ""); // ok
265
266 // Floating-point overflow and NaN.
267 constexpr float f1 = 1e38f * 3.4028f; // ok
268 constexpr float f2 = 1e38f * 3.4029f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
269 constexpr float f3 = 1e38f / -.2939f; // ok
270 constexpr float f4 = 1e38f / -.2938f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
271 constexpr float f5 = 2e38f + 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
272 constexpr float f6 = -2e38f - 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
273 constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces a NaN}}
274 }
Richard Smith357362d2011-12-13 06:39:58 +0000275}
276
277// - a lambda-expression (5.1.2);
278struct Lambda {
279 // FIXME: clang crashes when trying to parse this! Revisit this check once
280 // lambdas are fully implemented.
281 //int n : []{ return 1; }();
282};
283
Richard Smith357362d2011-12-13 06:39:58 +0000284// - an lvalue-to-rvalue conversion (4.1) unless it is applied to
Richard Smithf2b681b2011-12-21 05:04:46 +0000285namespace LValueToRValue {
286 // - a non-volatile glvalue of integral or enumeration type that refers to a
287 // non-volatile const object with a preceding initialization, initialized
288 // with a constant expression [Note: a string literal (2.14.5 [lex.string])
289 // corresponds to an array of such objects. -end note], or
Richard Smithc82fae62012-02-05 01:23:16 +0000290 volatile const int vi = 1; // expected-note {{here}}
Richard Smithf2b681b2011-12-21 05:04:46 +0000291 const int ci = 1;
292 volatile const int &vrci = ci;
Richard Smithc82fae62012-02-05 01:23:16 +0000293 static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
Richard Smithf2b681b2011-12-21 05:04:46 +0000294 static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
Richard Smithc82fae62012-02-05 01:23:16 +0000295 static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
Richard Smithf2b681b2011-12-21 05:04:46 +0000296
297 // - a non-volatile glvalue of literal type that refers to a non-volatile
298 // object defined with constexpr, or that refers to a sub-object of such an
299 // object, or
300 struct S {
301 constexpr S(int=0) : i(1), v(1) {}
302 constexpr S(const S &s) : i(2), v(2) {}
303 int i;
Richard Smithf4c51d92012-02-04 09:53:13 +0000304 volatile int v; // expected-note {{here}}
Richard Smithf2b681b2011-12-21 05:04:46 +0000305 };
306 constexpr S s;
Richard Smithc82fae62012-02-05 01:23:16 +0000307 constexpr volatile S vs; // expected-note {{here}}
Richard Smithf2b681b2011-12-21 05:04:46 +0000308 constexpr const volatile S &vrs = s;
309 static_assert(s.i, "");
Richard Smithc82fae62012-02-05 01:23:16 +0000310 static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
311 static_assert(const_cast<int&>(s.v), ""); // expected-error {{constant expression}} expected-note {{read of volatile member 'v'}}
312 static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
Richard Smithf2b681b2011-12-21 05:04:46 +0000313 static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
Richard Smithc82fae62012-02-05 01:23:16 +0000314 static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
Richard Smithf2b681b2011-12-21 05:04:46 +0000315
316 // - a non-volatile glvalue of literal type that refers to a non-volatile
317 // temporary object whose lifetime has not ended, initialized with a
318 // constant expression;
319 constexpr volatile S f() { return S(); }
320 static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
Richard Smithf4c51d92012-02-04 09:53:13 +0000321 static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}}
Richard Smithf2b681b2011-12-21 05:04:46 +0000322}
Richard Smith357362d2011-12-13 06:39:58 +0000323
Richard Smithf2b681b2011-12-21 05:04:46 +0000324// DR1312: The proposed wording for this defect has issues, so we ignore this
325// bullet and instead prohibit casts from pointers to cv void (see core-20842
326// and core-20845).
Richard Smith357362d2011-12-13 06:39:58 +0000327//
328// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
329// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
330// are neither the same type nor similar types (4.4 [conv.qual]);
331
Richard Smith357362d2011-12-13 06:39:58 +0000332// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
333// refers to a non-active member of a union or a subobject thereof;
Richard Smith84f6dcf2012-02-02 01:16:57 +0000334namespace LValueToRValueUnion {
335 // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing
336 // of this.
337 union U { int a, b; } constexpr u = U();
338 static_assert(u.a == 0, "");
339 constexpr const int *bp = &u.b;
340 constexpr int b = *bp; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
341
342 extern const U pu;
343 constexpr const int *pua = &pu.a;
344 constexpr const int *pub = &pu.b;
345 constexpr U pu = { .b = 1 }; // expected-warning {{C99 feature}}
346 constexpr const int a2 = *pua; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
347 constexpr const int b2 = *pub; // ok
348}
Richard Smith357362d2011-12-13 06:39:58 +0000349
Richard Smith357362d2011-12-13 06:39:58 +0000350// - an id-expression that refers to a variable or data member of reference type
351// unless the reference has a preceding initialization, initialized with a
352// constant expression;
353namespace References {
354 const int a = 2;
355 int &b = *const_cast<int*>(&a);
Richard Smithf2b681b2011-12-21 05:04:46 +0000356 int c = 10; // expected-note 2 {{here}}
Richard Smith357362d2011-12-13 06:39:58 +0000357 int &d = c;
358 constexpr int e = 42;
359 int &f = const_cast<int&>(e);
360 extern int &g;
Richard Smithd0b4dd62011-12-19 06:19:21 +0000361 constexpr int &h(); // expected-note 2{{here}}
362 int &i = h(); // expected-note {{here}} expected-note {{undefined function 'h' cannot be used in a constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +0000363 constexpr int &j() { return b; }
364 int &k = j();
365
366 struct S {
367 int A : a;
368 int B : b;
Richard Smithf2b681b2011-12-21 05:04:46 +0000369 int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
370 int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
Richard Smith357362d2011-12-13 06:39:58 +0000371 int D2 : &d - &c + 1;
372 int E : e / 2;
373 int F : f - 11;
374 int G : g; // expected-error {{constant expression}}
375 int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
Richard Smithd0b4dd62011-12-19 06:19:21 +0000376 int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +0000377 int J : j();
378 int K : k;
379 };
380}
381
382// - a dynamic_cast (5.2.7);
383namespace DynamicCast {
384 struct S { int n; };
385 constexpr S s { 16 };
386 struct T {
387 int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
388 };
389}
390
391// - a reinterpret_cast (5.2.10);
392namespace ReinterpretCast {
393 struct S { int n; };
394 constexpr S s { 16 };
395 struct T {
396 int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
397 };
398 struct U {
399 int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
400 };
401}
402
403// - a pseudo-destructor call (5.2.4);
404namespace PseudoDtor {
405 int k;
406 typedef int I;
407 struct T {
Richard Smithf4c51d92012-02-04 09:53:13 +0000408 int n : (k.~I(), 0); // expected-error {{constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +0000409 };
410}
411
412// - increment or decrement operations (5.2.6, 5.3.2);
413namespace IncDec {
414 int k = 2;
415 struct T {
416 int n : ++k; // expected-error {{constant expression}}
417 int m : --k; // expected-error {{constant expression}}
418 };
419}
420
421// - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
422namespace std {
423 struct type_info {
424 virtual ~type_info();
425 const char *name;
426 };
427}
428namespace TypeId {
429 struct S { virtual void f(); };
430 constexpr S *p = 0;
Richard Smith6e525142011-12-27 12:18:28 +0000431 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 Smith357362d2011-12-13 06:39:58 +0000432
Richard Smith357362d2011-12-13 06:39:58 +0000433 struct T {} t;
Richard Smith6e525142011-12-27 12:18:28 +0000434 constexpr const std::type_info &ti2 = typeid(t);
Richard Smith357362d2011-12-13 06:39:58 +0000435}
436
437// - a new-expression (5.3.4);
438// - a delete-expression (5.3.5);
439namespace NewDelete {
440 int *p = 0;
441 struct T {
Richard Smithf4c51d92012-02-04 09:53:13 +0000442 int n : *new int(4); // expected-error {{constant expression}}
443 int m : (delete p, 2); // expected-error {{constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +0000444 };
445}
446
447// - a relational (5.9) or equality (5.10) operator where the result is
448// unspecified;
449namespace UnspecifiedRelations {
450 int a, b;
451 constexpr int *p = &a, *q = &b;
452 // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
453 // different objects that are not members of the same array or to different
454 // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
455 // and p>=q are unspecified.
456 constexpr bool u1 = p < q; // expected-error {{constant expression}}
457 constexpr bool u2 = p > q; // expected-error {{constant expression}}
458 constexpr bool u3 = p <= q; // expected-error {{constant expression}}
459 constexpr bool u4 = p >= q; // expected-error {{constant expression}}
460 constexpr bool u5 = p < 0; // expected-error {{constant expression}}
461 constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
462 constexpr bool u7 = p > 0; // expected-error {{constant expression}}
463 constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
464 constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
465 constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
466 constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
467 constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
468 void f(), g();
469
470 constexpr void (*pf)() = &f, (*pg)() = &g;
471 constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
472 constexpr bool u14 = pf == pg;
473
Richard Smith357362d2011-12-13 06:39:58 +0000474 // If two pointers point to non-static data members of the same object with
475 // different access control, the result is unspecified.
Richard Smith84f6dcf2012-02-02 01:16:57 +0000476 struct A {
477 public:
478 constexpr A() : a(0), b(0) {}
479 int a;
480 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}}
481 private:
482 int b;
483 };
484 class B {
485 public:
486 A a;
487 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}}
488 protected:
489 A b;
490 };
491
492 // If two pointers point to different base sub-objects of the same object, or
493 // one points to a base subobject and the other points to a member, the result
494 // of the comparison is unspecified. This is not explicitly called out by
495 // [expr.rel]p2, but is covered by 'Other pointer comparisons are
496 // unspecified'.
497 struct C {
498 int c[2];
499 };
500 struct D {
501 int d;
502 };
503 struct E : C, D {
504 struct Inner {
505 int f;
506 } e;
507 } e;
508 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}}
509 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}}
510 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 Smith357362d2011-12-13 06:39:58 +0000511
Richard Smith357362d2011-12-13 06:39:58 +0000512 // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
513 // represent the same address or are both the null pointer [...]; otherwise
514 // the result is unspecified.
Richard Smithde21b242012-01-31 06:41:30 +0000515 struct S { int a, b; } s;
516 constexpr void *null = 0;
517 constexpr void *pv = (void*)&s.a;
518 constexpr void *qv = (void*)&s.b;
519 constexpr bool v1 = null < 0;
520 constexpr bool v2 = null < pv; // expected-error {{constant expression}}
521 constexpr bool v3 = null == pv; // ok
522 constexpr bool v4 = qv == pv; // ok
523 constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
524 constexpr bool v6 = qv > null; // expected-error {{constant expression}}
525 constexpr bool v7 = qv <= (void*)&s.b; // ok
526 constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
Richard Smith357362d2011-12-13 06:39:58 +0000527}
528
529// - an assignment or a compound assignment (5.17); or
530namespace Assignment {
531 int k;
532 struct T {
533 int n : (k = 9); // expected-error {{constant expression}}
534 int m : (k *= 2); // expected-error {{constant expression}}
535 };
536
537 struct Literal {
538 constexpr Literal(const char *name) : name(name) {}
539 const char *name;
540 };
541 struct Expr {
542 constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
543 bool IsLiteral;
544 union {
545 Literal l;
546 // ...
547 };
548 };
549 struct MulEq {
550 constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
551 Expr LHS;
552 Expr RHS;
553 };
554 constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
555 Literal a("a");
556 Literal b("b");
557 MulEq c = a *= b; // ok
558}
559
560// - a throw-expression (15.1)
561namespace Throw {
562 struct S {
Richard Smithf4c51d92012-02-04 09:53:13 +0000563 int n : (throw "hello", 10); // expected-error {{constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +0000564 };
565}
Douglas Gregorfcafc6e2011-05-24 16:02:01 +0000566
567// PR9999
Richard Smithf8379a02012-01-18 23:55:52 +0000568template<unsigned int v>
Douglas Gregorfcafc6e2011-05-24 16:02:01 +0000569class bitWidthHolding {
570public:
571 static const
572 unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
573};
574
575static const int width=bitWidthHolding<255>::width;
576
577template<bool b>
578struct always_false {
579 static const bool value = false;
580};
581
582template<bool b>
583struct and_or {
584 static const bool and_value = b && and_or<always_false<b>::value>::and_value;
585 static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
586};
587
588static const bool and_value = and_or<true>::and_value;
589static const bool or_value = and_or<true>::or_value;
Richard Smithf8379a02012-01-18 23:55:52 +0000590
591static_assert(and_value == false, "");
592static_assert(or_value == true, "");