blob: 8e68b9a0fb6348bf47e2ce7a1da60a210992c138 [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
112// FIXME:
113// - an operation that would have undefined behavior [Note: including, for
114// example, signed integer overflow (Clause 5 [expr]), certain pointer
115// arithmetic (5.7 [expr.add]), division by zero (5.6 [expr.mul]), or certain
116// shift operations (5.8 [expr.shift]) -end note];
117namespace UndefinedBehavior {
118 void f(int n) {
119 switch (n) {
120 case (int)4.4e9: // expected-error {{constant expression}} expected-note {{value 4.4E+9 is outside the range of representable values of type 'int'}}
Richard Smith006bfc92012-01-31 01:47:46 +0000121 case (int)0x80000000u: // ok
122 case (int)10000000000ll: // expected-note {{here}}
Richard Smith911e1422012-01-30 22:27:01 +0000123 case (unsigned int)10000000000ll: // expected-error {{duplicate case value}}
Richard Smith357362d2011-12-13 06:39:58 +0000124 case (int)(unsigned)(long long)4.4e9: // ok
Richard Smithf8379a02012-01-18 23:55:52 +0000125 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 +0000126 case (int)((float)1e37 / 1e30): // ok
127 case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value 65536 is outside the range of representable values of type 'half'}}
128 break;
129 }
130 }
131
Richard Smithfe800032012-01-31 04:08:20 +0000132 constexpr int int_min = ~0x7fffffff;
133 constexpr int minus_int_min = -int_min; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
134 constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
135 constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}} expected-warning {{undefined}}
136 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 +0000137 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 +0000138
139 constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
140 constexpr int shl_0 = 0 << 0; // ok
141 constexpr int shl_31 = 0 << 31; // ok
142 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}}
143 constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok
144 constexpr int shl_unsigned_into_sign = 1u << 31; // ok
145 constexpr int shl_unsigned_overflow = 1024u << 31; // ok
146 constexpr int shl_signed_negative = (-3) << 1; // expected-error {{constant expression}} expected-note {{left shift of negative value -3}}
147 constexpr int shl_signed_ok = 1 << 30; // ok
148 constexpr int shl_signed_into_sign = 1 << 31; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
149 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}}
150 constexpr int shl_signed_ok2 = 1024 << 20; // ok
151
152 constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}} expected-warning {{negative}}
153 constexpr int shr_0 = 0 >> 0; // ok
154 constexpr int shr_31 = 0 >> 31; // ok
155 constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}} expected-warning {{>= width of type}}
156
Richard Smith357362d2011-12-13 06:39:58 +0000157 struct S {
158 int m;
159 };
160 constexpr S s = { 5 }; // expected-note {{declared here}}
161 constexpr const int *p = &s.m + 1;
162 constexpr const int &f(const int *q) {
163 return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}}
164 }
Richard Smith02ab9c22012-01-12 06:08:57 +0000165 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 +0000166 struct T {
Richard Smith2ec40612012-01-15 03:51:30 +0000167 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 +0000168 };
Richard Smitha8105bc2012-01-06 16:39:00 +0000169
170 namespace Ptr {
171 struct A {};
172 struct B : A { int n; };
173 B a[3][3];
174 constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
175 B b = {};
176 constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
177 constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
178 constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
179 constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
180
181 constexpr A *na = nullptr;
182 constexpr B *nb = nullptr;
183 constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
184 constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
185 static_assert((A*)nb == 0, "");
186 static_assert((B*)na == 0, "");
187 constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
188 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 +0000189
190 struct C {
191 constexpr int f() { return 0; }
192 } constexpr c = C();
193 constexpr int k1 = c.f(); // ok
194 constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{cannot call member function on null pointer}}
195 constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{cannot call member function on pointer past the end of object}}
196 C c2;
197 constexpr int k4 = c2.f(); // ok!
Richard Smitha8105bc2012-01-06 16:39:00 +0000198 }
Richard Smithc8042322012-02-01 05:53:12 +0000199
200 namespace Overflow {
201 // Signed int overflow.
202 constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok
203 constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
204 constexpr int n3 = n1 + 1; // ok
205 constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
206 constexpr int n5 = -65536 * 32768; // ok
207 constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
208 constexpr int n7 = -n3 + -1; // ok
209 constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
210 constexpr int n9 = n3 - 0; // ok
211 constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
212 constexpr int n11 = -1 - n3; // ok
213 constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
214 constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }}
215 constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }}
216 constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }}
217 constexpr signed char c1 = 100 * 2; // ok
218 constexpr signed char c2 = '\x64' * '\2'; // also ok
219 constexpr long long ll1 = 0x7fffffffffffffff; // ok
220 constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }}
221 constexpr long long ll3 = -ll1 - 1; // ok
222 constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }}
223 constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }}
224
Richard Smith1b470412012-02-01 08:10:20 +0000225 // Yikes.
226 char melchizedek[2200000000];
227 typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t;
228 constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0]; // ok
229 constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }}
230 constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u]; // ok
231 constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // expected-error {{constant expression}} expected-note {{ -2147483649 }}
232
Richard Smithc8042322012-02-01 05:53:12 +0000233 // Unsigned int overflow.
234 static_assert(65536u * 65536u == 0u, ""); // ok
235 static_assert(4294967295u + 1u == 0u, ""); // ok
236 static_assert(0u - 1u == 4294967295u, ""); // ok
237 static_assert(~0u * ~0u == 1u, ""); // ok
238
239 // Floating-point overflow and NaN.
240 constexpr float f1 = 1e38f * 3.4028f; // ok
241 constexpr float f2 = 1e38f * 3.4029f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
242 constexpr float f3 = 1e38f / -.2939f; // ok
243 constexpr float f4 = 1e38f / -.2938f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
244 constexpr float f5 = 2e38f + 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
245 constexpr float f6 = -2e38f - 2e38f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces an infinity}}
246 constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{floating point arithmetic produces a NaN}}
247 }
Richard Smith357362d2011-12-13 06:39:58 +0000248}
249
250// - a lambda-expression (5.1.2);
251struct Lambda {
252 // FIXME: clang crashes when trying to parse this! Revisit this check once
253 // lambdas are fully implemented.
254 //int n : []{ return 1; }();
255};
256
Richard Smith357362d2011-12-13 06:39:58 +0000257// - an lvalue-to-rvalue conversion (4.1) unless it is applied to
Richard Smithf2b681b2011-12-21 05:04:46 +0000258namespace LValueToRValue {
259 // - a non-volatile glvalue of integral or enumeration type that refers to a
260 // non-volatile const object with a preceding initialization, initialized
261 // with a constant expression [Note: a string literal (2.14.5 [lex.string])
262 // corresponds to an array of such objects. -end note], or
263 volatile const int vi = 1; // expected-note {{here}}
264 const int ci = 1;
265 volatile const int &vrci = ci;
266 static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
267 static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
268 static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
269
270 // - a non-volatile glvalue of literal type that refers to a non-volatile
271 // object defined with constexpr, or that refers to a sub-object of such an
272 // object, or
273 struct S {
274 constexpr S(int=0) : i(1), v(1) {}
275 constexpr S(const S &s) : i(2), v(2) {}
276 int i;
277 volatile int v;
278 };
279 constexpr S s;
280 constexpr volatile S vs; // expected-note {{here}}
281 constexpr const volatile S &vrs = s;
282 static_assert(s.i, "");
283 static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
284 static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
285 static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
286 static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
287
288 // - a non-volatile glvalue of literal type that refers to a non-volatile
289 // temporary object whose lifetime has not ended, initialized with a
290 // constant expression;
291 constexpr volatile S f() { return S(); }
292 static_assert(f().i, ""); // ok! there's no lvalue-to-rvalue conversion here!
293 static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{subexpression}}
294}
Richard Smith357362d2011-12-13 06:39:58 +0000295
296// FIXME:
297//
Richard Smithf2b681b2011-12-21 05:04:46 +0000298// DR1312: The proposed wording for this defect has issues, so we ignore this
299// bullet and instead prohibit casts from pointers to cv void (see core-20842
300// and core-20845).
Richard Smith357362d2011-12-13 06:39:58 +0000301//
302// - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
303// glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
304// are neither the same type nor similar types (4.4 [conv.qual]);
305
306// FIXME:
307// - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
308// refers to a non-active member of a union or a subobject thereof;
309
Richard Smith357362d2011-12-13 06:39:58 +0000310// - an id-expression that refers to a variable or data member of reference type
311// unless the reference has a preceding initialization, initialized with a
312// constant expression;
313namespace References {
314 const int a = 2;
315 int &b = *const_cast<int*>(&a);
Richard Smithf2b681b2011-12-21 05:04:46 +0000316 int c = 10; // expected-note 2 {{here}}
Richard Smith357362d2011-12-13 06:39:58 +0000317 int &d = c;
318 constexpr int e = 42;
319 int &f = const_cast<int&>(e);
320 extern int &g;
Richard Smithd0b4dd62011-12-19 06:19:21 +0000321 constexpr int &h(); // expected-note 2{{here}}
322 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 +0000323 constexpr int &j() { return b; }
324 int &k = j();
325
326 struct S {
327 int A : a;
328 int B : b;
Richard Smithf2b681b2011-12-21 05:04:46 +0000329 int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
330 int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
Richard Smith357362d2011-12-13 06:39:58 +0000331 int D2 : &d - &c + 1;
332 int E : e / 2;
333 int F : f - 11;
334 int G : g; // expected-error {{constant expression}}
335 int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
Richard Smithd0b4dd62011-12-19 06:19:21 +0000336 int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
Richard Smith357362d2011-12-13 06:39:58 +0000337 int J : j();
338 int K : k;
339 };
340}
341
342// - a dynamic_cast (5.2.7);
343namespace DynamicCast {
344 struct S { int n; };
345 constexpr S s { 16 };
346 struct T {
347 int n : dynamic_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{dynamic_cast}}
348 };
349}
350
351// - a reinterpret_cast (5.2.10);
352namespace ReinterpretCast {
353 struct S { int n; };
354 constexpr S s { 16 };
355 struct T {
356 int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
357 };
358 struct U {
359 int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
360 };
361}
362
363// - a pseudo-destructor call (5.2.4);
364namespace PseudoDtor {
365 int k;
366 typedef int I;
367 struct T {
368 int n : (k.~I(), 0); // expected-error {{constant expression}} expected-note{{subexpression}}
369 };
370}
371
372// - increment or decrement operations (5.2.6, 5.3.2);
373namespace IncDec {
374 int k = 2;
375 struct T {
376 int n : ++k; // expected-error {{constant expression}}
377 int m : --k; // expected-error {{constant expression}}
378 };
379}
380
381// - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
382namespace std {
383 struct type_info {
384 virtual ~type_info();
385 const char *name;
386 };
387}
388namespace TypeId {
389 struct S { virtual void f(); };
390 constexpr S *p = 0;
Richard Smith6e525142011-12-27 12:18:28 +0000391 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 +0000392
Richard Smith357362d2011-12-13 06:39:58 +0000393 struct T {} t;
Richard Smith6e525142011-12-27 12:18:28 +0000394 constexpr const std::type_info &ti2 = typeid(t);
Richard Smith357362d2011-12-13 06:39:58 +0000395}
396
397// - a new-expression (5.3.4);
398// - a delete-expression (5.3.5);
399namespace NewDelete {
400 int *p = 0;
401 struct T {
402 int n : *new int(4); // expected-error {{constant expression}} expected-note {{subexpression}}
403 int m : (delete p, 2); // expected-error {{constant expression}} expected-note {{subexpression}}
404 };
405}
406
407// - a relational (5.9) or equality (5.10) operator where the result is
408// unspecified;
409namespace UnspecifiedRelations {
410 int a, b;
411 constexpr int *p = &a, *q = &b;
412 // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
413 // different objects that are not members of the same array or to different
414 // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
415 // and p>=q are unspecified.
416 constexpr bool u1 = p < q; // expected-error {{constant expression}}
417 constexpr bool u2 = p > q; // expected-error {{constant expression}}
418 constexpr bool u3 = p <= q; // expected-error {{constant expression}}
419 constexpr bool u4 = p >= q; // expected-error {{constant expression}}
420 constexpr bool u5 = p < 0; // expected-error {{constant expression}}
421 constexpr bool u6 = p <= 0; // expected-error {{constant expression}}
422 constexpr bool u7 = p > 0; // expected-error {{constant expression}}
423 constexpr bool u8 = p >= 0; // expected-error {{constant expression}}
424 constexpr bool u9 = 0 < q; // expected-error {{constant expression}}
425 constexpr bool u10 = 0 <= q; // expected-error {{constant expression}}
426 constexpr bool u11 = 0 > q; // expected-error {{constant expression}}
427 constexpr bool u12 = 0 >= q; // expected-error {{constant expression}}
428 void f(), g();
429
430 constexpr void (*pf)() = &f, (*pg)() = &g;
431 constexpr bool u13 = pf < pg; // expected-error {{constant expression}}
432 constexpr bool u14 = pf == pg;
433
434 // FIXME:
435 // If two pointers point to non-static data members of the same object with
436 // different access control, the result is unspecified.
437
Richard Smith357362d2011-12-13 06:39:58 +0000438 // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
439 // represent the same address or are both the null pointer [...]; otherwise
440 // the result is unspecified.
Richard Smithde21b242012-01-31 06:41:30 +0000441 struct S { int a, b; } s;
442 constexpr void *null = 0;
443 constexpr void *pv = (void*)&s.a;
444 constexpr void *qv = (void*)&s.b;
445 constexpr bool v1 = null < 0;
446 constexpr bool v2 = null < pv; // expected-error {{constant expression}}
447 constexpr bool v3 = null == pv; // ok
448 constexpr bool v4 = qv == pv; // ok
449 constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
450 constexpr bool v6 = qv > null; // expected-error {{constant expression}}
451 constexpr bool v7 = qv <= (void*)&s.b; // ok
452 constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
Richard Smith357362d2011-12-13 06:39:58 +0000453
454 // FIXME: Implement comparisons of pointers to members.
455 // [expr.eq]p2: If either is a pointer to a virtual member function and
456 // neither is null, the result is unspecified.
457}
458
459// - an assignment or a compound assignment (5.17); or
460namespace Assignment {
461 int k;
462 struct T {
463 int n : (k = 9); // expected-error {{constant expression}}
464 int m : (k *= 2); // expected-error {{constant expression}}
465 };
466
467 struct Literal {
468 constexpr Literal(const char *name) : name(name) {}
469 const char *name;
470 };
471 struct Expr {
472 constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
473 bool IsLiteral;
474 union {
475 Literal l;
476 // ...
477 };
478 };
479 struct MulEq {
480 constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
481 Expr LHS;
482 Expr RHS;
483 };
484 constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
485 Literal a("a");
486 Literal b("b");
487 MulEq c = a *= b; // ok
488}
489
490// - a throw-expression (15.1)
491namespace Throw {
492 struct S {
493 int n : (throw "hello", 10); // expected-error {{constant expression}} expected-note {{subexpression}}
494 };
495}
Douglas Gregorfcafc6e2011-05-24 16:02:01 +0000496
497// PR9999
Richard Smithf8379a02012-01-18 23:55:52 +0000498template<unsigned int v>
Douglas Gregorfcafc6e2011-05-24 16:02:01 +0000499class bitWidthHolding {
500public:
501 static const
502 unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
503};
504
505static const int width=bitWidthHolding<255>::width;
506
507template<bool b>
508struct always_false {
509 static const bool value = false;
510};
511
512template<bool b>
513struct and_or {
514 static const bool and_value = b && and_or<always_false<b>::value>::and_value;
515 static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
516};
517
518static const bool and_value = and_or<true>::and_value;
519static const bool or_value = and_or<true>::or_value;
Richard Smithf8379a02012-01-18 23:55:52 +0000520
521static_assert(and_value == false, "");
522static_assert(or_value == true, "");