1 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -pedantic -verify=expected,cxx11 -fcxx-exceptions %s -fconstexpr-depth 128 -triple i686-pc-linux-gnu
2 // RUN: %clang_cc1 -fsyntax-only -std=c++2a -pedantic -verify=expected,cxx20 -fcxx-exceptions %s -fconstexpr-depth 128 -triple i686-pc-linux-gnu
3 
4 // A conditional-expression is a core constant expression unless it involves one
5 // of the following as a potentially evaluated subexpression [...]:
6 
7 // - this (5.1.1 [expr.prim.general]) [Note: when evaluating a constant
8 //   expression, function invocation substitution (7.1.5 [dcl.constexpr])
9 //   replaces each occurrence of this in a constexpr member function with a
10 //   pointer to the class object. -end note];
11 struct This {
12   int this1 : this1; // expected-error {{undeclared}}
13   int this2 : this->this1; // expected-error {{invalid}}
this3This14   void this3() {
15     int n1[this->this1]; // expected-warning {{variable length array}} expected-note {{'this'}}
16     int n2[this1]; // expected-warning {{variable length array}} expected-note {{'this'}}
17     (void)n1, (void)n2;
18   }
19 };
20 
21 // - an invocation of a function other than a constexpr constructor for a
22 //   literal class or a constexpr function [ Note: Overload resolution (13.3)
23 //   is applied as usual - end note ];
24 struct NonConstexpr1 {
fNonConstexpr125   static int f() { return 1; } // expected-note {{here}}
26   int n : f(); // expected-error {{constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
27 };
28 struct NonConstexpr2 {
29   constexpr NonConstexpr2(); // expected-note {{here}}
30   int n;
31 };
32 struct NonConstexpr3 {
33   NonConstexpr3();
34   int m : NonConstexpr2().n; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}}
35 };
36 struct NonConstexpr4 {
37   NonConstexpr4();
38   int n;
39 };
40 struct NonConstexpr5 {
41   int n : NonConstexpr4().n; // expected-error {{constant expression}} expected-note {{non-literal type 'NonConstexpr4' cannot be used in a constant expression}}
42 };
43 
44 // - an invocation of an undefined constexpr function or an undefined
45 //   constexpr constructor;
46 struct UndefinedConstexpr {
47   constexpr UndefinedConstexpr();
48   static constexpr int undefinedConstexpr1(); // expected-note {{here}}
49   int undefinedConstexpr2 : undefinedConstexpr1(); // expected-error {{constant expression}} expected-note {{undefined function 'undefinedConstexpr1' cannot be used in a constant expression}}
50 };
51 
52 // - an invocation of a constexpr function with arguments that, when substituted
53 //   by function invocation substitution (7.1.5), do not produce a core constant
54 //   expression;
55 namespace NonConstExprReturn {
id_ref(const int & n)56   static constexpr const int &id_ref(const int &n) {
57     return n;
58   }
59   struct NonConstExprFunction {
60     int n : id_ref(16); // ok
61   };
address_of(const int & a)62   constexpr const int *address_of(const int &a) {
63     return &a;
64   }
return_param(int n)65   constexpr const int *return_param(int n) {
66     return address_of(n);
67   }
68   struct S {
69     int n : *return_param(0); // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
70   };
71 }
72 
73 // - an invocation of a constexpr constructor with arguments that, when
74 //   substituted by function invocation substitution (7.1.5), do not produce all
75 //   constant expressions for the constructor calls and full-expressions in the
76 //   mem-initializers (including conversions);
77 namespace NonConstExprCtor {
78   struct T {
TNonConstExprCtor::T79     constexpr T(const int &r) :
80       r(r) {
81     }
82     const int &r;
83   };
84   constexpr int n = 0;
85   constexpr T t1(n); // ok
86   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}}
87 
88   struct S {
89     int n : T(4).r; // ok
90   };
91 }
92 
93 // - an invocation of a constexpr function or a constexpr constructor that would
94 //   exceed the implementation-defined recursion limits (see Annex B);
95 namespace RecursionLimits {
RecurseForever(int n)96   constexpr int RecurseForever(int n) {
97     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}}
98   }
99   struct AlsoRecurseForever {
AlsoRecurseForeverRecursionLimits::AlsoRecurseForever100     constexpr AlsoRecurseForever(int n) :
101       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}}
102     {}
103     int n;
104   };
105   struct S {
106     int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}}
107     int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}}
108   };
109 }
110 
111 // DR1458: taking the address of an object of incomplete class type
112 namespace IncompleteClassTypeAddr {
113   struct S;
114   extern S s;
115   constexpr S *p = &s; // ok
116   static_assert(p, "");
117 
118   extern S sArr[];
119   constexpr S (*p2)[] = &sArr; // ok
120 
121   struct S {
operator &IncompleteClassTypeAddr::S122     constexpr S *operator&() const { return nullptr; }
123   };
124   constexpr S *q = &s; // ok
125   static_assert(!q, "");
126 }
127 
128 // - 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];
132 namespace UndefinedBehavior {
f(int n)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'}}
136     case (int)0x80000000u: // ok
137     case (int)10000000000ll: // expected-note {{here}}
138     case (unsigned int)10000000000ll: // expected-error {{duplicate case value}}
139     case (int)(unsigned)(long long)4.4e9: // ok
140     case (int)(float)1e300: // expected-error {{constant expression}} expected-note {{value +Inf is outside the range of representable values of type 'int'}}
141     case (int)((float)1e37 / 1e30): // ok
142     case (int)(__fp16)65536: // expected-error {{constant expression}} expected-note {{value +Inf is outside the range of representable values of type 'int'}}
143       break;
144     }
145   }
146 
147   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}}
150   constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}}
151   constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
152   constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
153 
154   constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}}
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}}
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; // cxx11-error {{constant expression}} cxx11-note {{left shift of negative value -3}}
162   constexpr int shl_signed_ok = 1 << 30; // ok
163   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; // cxx11-error {{constant expression}} cxx11-note {{signed left shift discards bits}} cxx11-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; // cxx11-error {{constant expression}} cxx11-note {{signed left shift discards bits}} cxx11-warning {{signed shift result (0x1FFFFFFFC) requires 34 bits to represent, but 'int' only has 32 bits}}
167   constexpr int shl_signed_overflow = 1024 << 31; // cxx11-error {{constant expression}} cxx11-note {{signed left shift discards bits}} cxx11-warning {{requires 43 bits to represent}}
168   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}}
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}}
174 
175   struct S {
176     int m;
177   };
178   constexpr S s = { 5 };
179   constexpr const int *p = &s.m + 1;
f(const int * q)180   constexpr const int &f(const int *q) {
181     return q[0];
182   }
183   constexpr int n = (f(p), 0); // ok
184   struct T {
185     int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
186   };
187 
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 *np1 = (int*)nullptr + 0; // ok
207     constexpr const int *np2 = &(*(int(*)[4])nullptr)[0]; // ok
208     constexpr const int *np3 = &(*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot perform pointer arithmetic on null pointer}}
209 
210     struct C {
fUndefinedBehavior::Ptr::C211       constexpr int f() const { return 0; }
212     } constexpr c = C();
213     constexpr int k1 = c.f(); // ok
214     constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced null pointer}}
215     constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
216     C c2;
217     constexpr int k4 = c2.f(); // ok!
218 
219     constexpr int diff1 = &a[2] - &a[0];
220     constexpr int diff2 = &a[1][3] - &a[1][0];
221     constexpr int diff3 = &a[2][0] - &a[1][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
222     static_assert(&a[2][0] == &a[1][3], "");
223     constexpr int diff4 = (&b + 1) - &b;
224     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}}
225     constexpr int diff6 = &a[1][2].n - &a[1][2].n;
226     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}}
227   }
228 
229   namespace Overflow {
230     // Signed int overflow.
231     constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok
232     constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
233     constexpr int n3 = n1 + 1; // ok
234     constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
235     constexpr int n5 = -65536 * 32768; // ok
236     constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
237     constexpr int n7 = -n3 + -1; // ok
238     constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
239     constexpr int n9 = n3 - 0; // ok
240     constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
241     constexpr int n11 = -1 - n3; // ok
242     constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
243     constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }}
244     constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }}
245     constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }}
246     constexpr signed char c1 = 100 * 2; // ok expected-warning{{changes value}}
247     constexpr signed char c2 = '\x64' * '\2'; // also ok  expected-warning{{changes value}}
248     constexpr long long ll1 = 0x7fffffffffffffff; // ok
249     constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }}
250     constexpr long long ll3 = -ll1 - 1; // ok
251     constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }}
252     constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }}
253 
254     // Yikes.
255     char melchizedek[2200000000];
256     typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t;
257     constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0]; // ok
258     constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }}
259     constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u]; // ok
260     constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // expected-error {{constant expression}} expected-note {{ -2147483649 }}
261 
262     // Unsigned int overflow.
263     static_assert(65536u * 65536u == 0u, ""); // ok
264     static_assert(4294967295u + 1u == 0u, ""); // ok
265     static_assert(0u - 1u == 4294967295u, ""); // ok
266     static_assert(~0u * ~0u == 1u, ""); // ok
267 
isinf(T v)268     template<typename T> constexpr bool isinf(T v) { return v && v / 2 == v; }
269 
270     // Floating-point overflow and NaN.
271     constexpr float f1 = 1e38f * 3.4028f; // ok
272     constexpr float f2 = 1e38f * 3.4029f; // ok, +inf is in range of representable values
273     constexpr float f3 = 1e38f / -.2939f; // ok
274     constexpr float f4 = 1e38f / -.2938f; // ok, -inf is in range of representable values
275     constexpr float f5 = 2e38f + 2e38f; // ok, +inf is in range of representable values
276     constexpr float f6 = -2e38f - 2e38f; // ok, -inf is in range of representable values
277     constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{division by zero}}
278     constexpr float f8 = 1.f / 0.f; // expected-error {{constant expression}} expected-note {{division by zero}}
279     constexpr float f9 = 1e308 / 1e-308; // ok, +inf
280     constexpr float f10 = f2 - f2; // expected-error {{constant expression}} expected-note {{produces a NaN}}
281     constexpr float f11 = f2 + f4; // expected-error {{constant expression}} expected-note {{produces a NaN}}
282     constexpr float f12 = f2 / f2; // expected-error {{constant expression}} expected-note {{produces a NaN}}
283 #pragma float_control(push)
284 #pragma float_control(except, on)
285 constexpr float pi = 3.14f;
286 constexpr unsigned ubig = 0xFFFFFFFF;
287 constexpr float ce = 1.0 / 3.0; // not-expected-error {{constant expression}} not-expected-note {{floating point arithmetic suppressed in strict evaluation modes}}
288 constexpr int ci = (int) pi;
289 constexpr float fbig = (float) ubig; // not-expected-error {{constant expression}} not-expected-note {{floating point arithmetic suppressed in strict evaluation modes}}
290 constexpr float fabspi = __builtin_fabs(pi); // no error expected
291 constexpr float negpi = -pi; // expect no error on unary operator
292 #pragma float_control(pop)
293     static_assert(!isinf(f1), "");
294     static_assert(isinf(f2), "");
295     static_assert(!isinf(f3), "");
296     static_assert(isinf(f4), "");
297     static_assert(isinf(f5), "");
298     static_assert(isinf(f6), "");
299     static_assert(isinf(f9), "");
300   }
301 }
302 
303 // - a lambda-expression (5.1.2);
304 struct Lambda {
__anon2196d83d0102Lambda305   int n : []{ return 1; }(); // cxx11-error {{constant expression}} cxx11-error {{integral constant expression}} cxx11-note {{non-literal type}}
306 };
307 
308 // - an lvalue-to-rvalue conversion (4.1) unless it is applied to
309 namespace LValueToRValue {
310   // - a non-volatile glvalue of integral or enumeration type that refers to a
311   //   non-volatile const object with a preceding initialization, initialized
312   //   with a constant expression  [Note: a string literal (2.14.5 [lex.string])
313   //   corresponds to an array of such objects. -end note], or
314   volatile const int vi = 1; // expected-note 2{{here}}
315   const int ci = 1;
316   volatile const int &vrci = ci;
317   static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
318   static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
319   static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
320 
321   // - a non-volatile glvalue of literal type that refers to a non-volatile
322   //   object defined with constexpr, or that refers to a sub-object of such an
323   //   object, or
324   struct V {
VLValueToRValue::V325     constexpr V() : v(1) {}
326     volatile int v; // expected-note {{not literal because}}
327   };
328   constexpr V v; // expected-error {{non-literal type}}
329   struct S {
SLValueToRValue::S330     constexpr S(int=0) : i(1), v(const_cast<volatile int&>(vi)) {}
SLValueToRValue::S331     constexpr S(const S &s) : i(2), v(const_cast<volatile int&>(vi)) {}
332     int i;
333     volatile int &v;
334   };
335   constexpr S s; // ok
336   constexpr volatile S vs; // expected-note {{here}}
337   constexpr const volatile S &vrs = s; // ok
338   static_assert(s.i, "");
339   static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
340   static_assert(const_cast<int&>(s.v), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
341   static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
342   static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
343   static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
344 
345   // - a non-volatile glvalue of literal type that refers to a non-volatile
346   //   temporary object whose lifetime has not ended, initialized with a
347   //   constant expression;
f()348   constexpr volatile S f() { return S(); }
349   static_assert(f().i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
350   static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
351 }
352 
353 // DR1312: The proposed wording for this defect has issues, so we ignore this
354 // bullet and instead prohibit casts from pointers to cv void (see core-20842
355 // and core-20845).
356 //
357 // - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
358 // glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
359 // are neither the same type nor similar types (4.4 [conv.qual]);
360 
361 // - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
362 // refers to a non-active member of a union or a subobject thereof;
363 namespace LValueToRValueUnion {
364   // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing
365   // of this.
366   union U { int a, b; } constexpr u = U();
367   static_assert(u.a == 0, "");
368   constexpr const int *bp = &u.b;
369   constexpr int b = *bp; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
370 
371   extern const U pu;
372   constexpr const int *pua = &pu.a;
373   constexpr const int *pub = &pu.b;
374   constexpr U pu = { .b = 1 }; // cxx11-warning {{C++20 extension}}
375   constexpr const int a2 = *pua; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
376   constexpr const int b2 = *pub; // ok
377 }
378 
379 // - an id-expression that refers to a variable or data member of reference type
380 //   unless the reference has a preceding initialization, initialized with a
381 //   constant expression;
382 namespace References {
383   const int a = 2;
384   int &b = *const_cast<int*>(&a);
385   int c = 10; // expected-note 2 {{here}}
386   int &d = c;
387   constexpr int e = 42;
388   int &f = const_cast<int&>(e);
389   extern int &g; // expected-note {{here}}
390   constexpr int &h(); // expected-note {{here}}
391   int &i = h(); // expected-note {{here}}
j()392   constexpr int &j() { return b; }
393   int &k = j();
394 
395   struct S {
396     int A : a;
397     int B : b;
398     int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
399     int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
400     int D2 : &d - &c + 1;
401     int E : e / 2;
402     int F : f - 11;
403     int G : g; // expected-error {{constant expression}} expected-note {{initializer of 'g' is unknown}}
404     int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
405     int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
406     int J : j();
407     int K : k;
408   };
409 }
410 
411 // - a dynamic_cast (5.2.7);
412 namespace DynamicCast {
413   struct S { int n; };
414   constexpr S s { 16 };
415   struct T {
416     int n : dynamic_cast<const S*>(&s)->n; // cxx11-warning {{constant expression}} cxx11-note {{dynamic_cast}}
417   };
418 }
419 
420 // - a reinterpret_cast (5.2.10);
421 namespace ReinterpretCast {
422   struct S { int n; };
423   constexpr S s { 16 };
424   struct T {
425     int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
426   };
427   struct U {
428     int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
429   };
430 }
431 
432 // - a pseudo-destructor call (5.2.4);
433 namespace PseudoDtor {
434   int k;
435   typedef int I;
436   struct T {
437     int n : (k.~I(), 1); // expected-error {{constant expression}} expected-note {{visible outside that expression}}
438   };
439 
f(int a=1)440   constexpr int f(int a = 1) { // cxx11-error {{constant expression}} expected-note {{destroying object 'a' whose lifetime has already ended}}
441     return (
442         a.~I(), // cxx11-note {{pseudo-destructor}}
443         0);
444   }
445   static_assert(f() == 0, ""); // expected-error {{constant expression}}
446 
447   // This is OK in C++20: the union has no active member after the
448   // pseudo-destructor call, so the union destructor has no effect.
449   union U { int x; };
g(U u={1})450   constexpr int g(U u = {1}) { // cxx11-error {{constant expression}}
451     return (
452         u.x.~I(), // cxx11-note 2{{pseudo-destructor}}
453         0);
454   }
455   static_assert(g() == 0, ""); // cxx11-error {{constant expression}} cxx11-note {{in call}}
456 }
457 
458 // - increment or decrement operations (5.2.6, 5.3.2);
459 namespace IncDec {
460   int k = 2;
461   struct T {
462     int n : ++k; // expected-error {{constant expression}} cxx20-note {{visible outside}}
463     int m : --k; // expected-error {{constant expression}} cxx20-note {{visible outside}}
464   };
465 }
466 
467 // - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
468 namespace std {
469   struct type_info {
470     virtual ~type_info();
471     const char *name;
472   };
473 }
474 namespace TypeId {
475   struct S { virtual void f(); };
476   constexpr S *p = 0;
477   constexpr const std::type_info &ti1 = typeid(*p); // expected-error {{must be initialized by a constant expression}} cxx11-note {{typeid applied to expression of polymorphic type 'TypeId::S'}} cxx20-note {{dereferenced null pointer}}
478 
479   struct T {} t;
480   constexpr const std::type_info &ti2 = typeid(t);
481 }
482 
483 // - a new-expression (5.3.4);
484 // - a delete-expression (5.3.5);
485 namespace NewDelete {
486   constexpr int *p = 0;
487   struct T {
488     int n : *new int(4); // expected-warning {{constant expression}} cxx11-note {{until C++20}} cxx20-note {{was not deallocated}}
489     int m : (delete p, 2); // cxx11-warning {{constant expression}} cxx11-note {{until C++20}}
490   };
491 }
492 
493 // - a relational (5.9) or equality (5.10) operator where the result is
494 //   unspecified;
495 namespace UnspecifiedRelations {
496   int a, b;
497   constexpr int *p = &a, *q = &b;
498   // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
499   // different objects that are not members of the same array or to different
500   // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
501   // and p>=q are unspecified.
502   constexpr bool u1 = p < q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
503   constexpr bool u2 = p > q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
504   constexpr bool u3 = p <= q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
505   constexpr bool u4 = p >= q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
506   constexpr bool u5 = p < (int*)0; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
507   constexpr bool u6 = p <= (int*)0; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
508   constexpr bool u7 = p > (int*)0; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
509   constexpr bool u8 = p >= (int*)0; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
510   constexpr bool u9 = (int*)0 < q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
511   constexpr bool u10 = (int*)0 <= q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
512   constexpr bool u11 = (int*)0 > q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
513   constexpr bool u12 = (int*)0 >= q; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
514   void f(), g();
515 
516   constexpr void (*pf)() = &f, (*pg)() = &g;
517   constexpr bool u13 = pf < pg; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
518                                 // expected-warning@-1 {{ordered comparison of function pointers}}
519   constexpr bool u14 = pf == pg;
520 
521   // If two pointers point to non-static data members of the same object with
522   // different access control, the result is unspecified.
523   struct A {
524   public:
AUnspecifiedRelations::A525     constexpr A() : a(0), b(0) {}
526     int a;
cmpUnspecifiedRelations::A527     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}}
528   private:
529     int b;
530   };
531   static_assert(A().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}}
532   class B {
533   public:
534     A a;
cmp() const535     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}}
536   protected:
537     A b;
538   };
539   static_assert(B().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}}
540 
541   // If two pointers point to different base sub-objects of the same object, or
542   // one points to a base subobject and the other points to a member, the result
543   // of the comparison is unspecified. This is not explicitly called out by
544   // [expr.rel]p2, but is covered by 'Other pointer comparisons are
545   // unspecified'.
546   struct C {
547     int c[2];
548   };
549   struct D {
550     int d;
551   };
552   struct E : C, D {
553     struct Inner {
554       int f;
555     } e;
556   } e;
557   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}}
558   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}}
559   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}}
560 
561   // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
562   // represent the same address or are both the null pointer [...]; otherwise
563   // the result is unspecified.
564   struct S { int a, b; } s;
565   constexpr void *null = 0;
566   constexpr void *pv = (void*)&s.a;
567   constexpr void *qv = (void*)&s.b;
568   constexpr bool v1 = null < (int*)0;
569   constexpr bool v2 = null < pv; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
570   constexpr bool v3 = null == pv; // ok
571   constexpr bool v4 = qv == pv; // ok
572   constexpr bool v5 = qv >= pv; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
573   constexpr bool v6 = qv > null; // expected-error {{constant expression}} expected-note {{comparison has unspecified value}}
574   constexpr bool v7 = qv <= (void*)&s.b; // ok
575   constexpr bool v8 = qv > (void*)&s.a; // expected-error {{constant expression}} expected-note {{unequal pointers to void}}
576 }
577 
578 // - an assignment or a compound assignment (5.17); or
579 namespace Assignment {
580   int k;
581   struct T {
582     int n : (k = 9); // expected-error {{constant expression}} cxx20-note {{visible outside}}
583     int m : (k *= 2); // expected-error {{constant expression}} cxx20-note {{visible outside}}
584   };
585 
586   struct Literal {
LiteralAssignment::Literal587     constexpr Literal(const char *name) : name(name) {}
588     const char *name;
589   };
590   struct Expr {
ExprAssignment::Expr591     constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
592     bool IsLiteral;
593     union {
594       Literal l;
595       // ...
596     };
597   };
598   struct MulEq {
MulEqAssignment::MulEq599     constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
600     Expr LHS;
601     Expr RHS;
602   };
operator *=(Expr a,Expr b)603   constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
604   Literal a("a");
605   Literal b("b");
606   MulEq c = a *= b; // ok
607 }
608 
609 // - a throw-expression (15.1)
610 namespace Throw {
611   struct S {
612     int n : (throw "hello", 10); // expected-error {{constant expression}}
613   };
614 }
615 
616 // PR9999
617 template<unsigned int v>
618 class bitWidthHolding {
619 public:
620   static const
621   unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
622 };
623 
624 static const int width=bitWidthHolding<255>::width;
625 
626 template<bool b>
627 struct always_false {
628   static const bool value = false;
629 };
630 
631 template<bool b>
632 struct and_or {
633   static const bool and_value = b && and_or<always_false<b>::value>::and_value;
634   static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
635 };
636 
637 static const bool and_value = and_or<true>::and_value;
638 static const bool or_value = and_or<true>::or_value;
639 
640 static_assert(and_value == false, "");
641 static_assert(or_value == true, "");
642 
643 namespace rdar13090123 {
644   typedef __INTPTR_TYPE__ intptr_t;
645 
f(intptr_t x)646   constexpr intptr_t f(intptr_t x) {
647     return (((x) >> 21) * 8);
648   }
649 
650   extern "C" int foo;
651 
652   constexpr intptr_t i = f((intptr_t)&foo - 10); // expected-error{{constexpr variable 'i' must be initialized by a constant expression}} \
653   // expected-note{{reinterpret_cast}}
654 }
655