xref: /llvm-project/clang/test/CXX/expr/expr.const/p2-0x.cpp (revision fd6baa477fa13a4b893aeeba7fce92eb6a1f4962)
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}}
14   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 {
25   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 {
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   };
62   constexpr const int *address_of(const int &a) {
63     return &a;
64   }
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 {
79     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 {
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 {
100     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 {
122     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 {
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 minus_int_min_from_shift = -(1<<31); // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
150   constexpr int div0 = 3 / 0; // expected-error {{constant expression}} expected-note {{division by zero}}
151   constexpr int mod0 = 3 % 0; // expected-error {{constant expression}} expected-note {{division by zero}}
152   constexpr int int_min_div_minus_1 = int_min / -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
153   constexpr int int_min_mod_minus_1 = int_min % -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range}}
154 
155   constexpr int shl_m1 = 0 << -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}}
156   constexpr int shl_0 = 0 << 0; // ok
157   constexpr int shl_31 = 0 << 31; // ok
158   constexpr int shl_32 = 0 << 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type 'int' (32}}
159   constexpr int shl_unsigned_negative = unsigned(-3) << 1; // ok
160   constexpr int shl_unsigned_into_sign = 1u << 31; // ok
161   constexpr int shl_unsigned_overflow = 1024u << 31; // ok
162   constexpr int shl_signed_negative = (-3) << 1; // cxx11-error {{constant expression}} cxx11-note {{left shift of negative value -3}}
163   constexpr int shl_signed_ok = 1 << 30; // ok
164   constexpr int shl_signed_into_sign = 1 << 31; // ok (DR1457)
165   constexpr int shl_signed_into_sign_2 = 0x7fffffff << 1; // ok (DR1457)
166   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}}
167   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}}
168   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}}
169   constexpr int shl_signed_ok2 = 1024 << 20; // ok
170 
171   constexpr int shr_m1 = 0 >> -1; // expected-error {{constant expression}} expected-note {{negative shift count -1}}
172   constexpr int shr_0 = 0 >> 0; // ok
173   constexpr int shr_31 = 0 >> 31; // ok
174   constexpr int shr_32 = 0 >> 32; // expected-error {{constant expression}} expected-note {{shift count 32 >= width of type}}
175 
176   struct S {
177     int m;
178   };
179   constexpr S s = { 5 };
180   constexpr const int *p = &s.m + 1;
181   constexpr const int &f(const int *q) {
182     return q[0];
183   }
184   constexpr int n = (f(p), 0); // ok
185   struct T {
186     int n : f(p); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
187   };
188 
189   namespace Ptr {
190     struct A {};
191     struct B : A { int n; int m; };
192     B a[3][3];
193     constexpr B *p = a[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
194     B b = {};
195     constexpr A *pa = &b + 1; // expected-error {{constant expression}} expected-note {{base class of pointer past the end}}
196     constexpr B *pb = (B*)((A*)&b + 1); // expected-error {{constant expression}} expected-note {{derived class of pointer past the end}}
197     constexpr const int *pn = &(&b + 1)->n; // expected-error {{constant expression}} expected-note {{field of pointer past the end}}
198     constexpr B *parr = &a[3][0]; // expected-error {{constant expression}} expected-note {{array element of pointer past the end}}
199 
200     constexpr A *na = nullptr;
201     constexpr B *nb = nullptr;
202     constexpr A &ra = *nb; // expected-error {{constant expression}} expected-note {{cannot access base class of null pointer}}
203     constexpr B &rb = (B&)*na; // expected-error {{constant expression}} expected-note {{cannot access derived class of null pointer}}
204     static_assert((A*)nb == 0, "");
205     static_assert((B*)na == 0, "");
206     constexpr const int &nf = nb->n; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
207     constexpr const int &mf = nb->m; // expected-error {{constant expression}} expected-note {{cannot access field of null pointer}}
208     constexpr const int *np1 = (int*)nullptr + 0; // ok
209     constexpr const int *np2 = &(*(int(*)[4])nullptr)[0]; // ok
210     constexpr const int *np3 = &(*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot perform pointer arithmetic on null pointer}}
211 
212     struct C {
213       constexpr int f() const { return 0; }
214     } constexpr c = C();
215     constexpr int k1 = c.f(); // ok
216     constexpr int k2 = ((C*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced null pointer}}
217     constexpr int k3 = (&c)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
218     C c2;
219     constexpr int k4 = c2.f(); // ok!
220 
221     constexpr int diff1 = &a[2] - &a[0];
222     constexpr int diff2 = &a[1][3] - &a[1][0];
223     constexpr int diff3 = &a[2][0] - &a[1][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
224     static_assert(&a[2][0] == &a[1][3], "");
225     constexpr int diff4 = (&b + 1) - &b;
226     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}}
227     constexpr int diff6 = &a[1][2].n - &a[1][2].n;
228     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}}
229   }
230 
231   namespace Overflow {
232     // Signed int overflow.
233     constexpr int n1 = 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok
234     constexpr int n2 = 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
235     constexpr int n3 = n1 + 1; // ok
236     constexpr int n4 = n3 + 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
237     constexpr int n5 = -65536 * 32768; // ok
238     constexpr int n6 = 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
239     constexpr int n7 = -n3 + -1; // ok
240     constexpr int n8 = -1 + n7; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
241     constexpr int n9 = n3 - 0; // ok
242     constexpr int n10 = n3 - -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
243     constexpr int n11 = -1 - n3; // ok
244     constexpr int n12 = -2 - n3; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
245     constexpr int n13 = n5 + n5; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }}
246     constexpr int n14 = n3 - n5; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }}
247     constexpr int n15 = n5 * n5; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }}
248     constexpr signed char c1 = 100 * 2; // ok expected-warning{{changes value}}
249     constexpr signed char c2 = '\x64' * '\2'; // also ok  expected-warning{{changes value}}
250     constexpr long long ll1 = 0x7fffffffffffffff; // ok
251     constexpr long long ll2 = ll1 + 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }}
252     constexpr long long ll3 = -ll1 - 1; // ok
253     constexpr long long ll4 = ll3 - 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }}
254     constexpr long long ll5 = ll3 * ll3; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }}
255 
256     // Yikes.
257     char melchizedek[2200000000];
258     typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t;
259     constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0]; // ok
260     constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }}
261     constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u]; // ok
262     constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // expected-error {{constant expression}} expected-note {{ -2147483649 }}
263 
264     // Unsigned int overflow.
265     static_assert(65536u * 65536u == 0u, ""); // ok
266     static_assert(4294967295u + 1u == 0u, ""); // ok
267     static_assert(0u - 1u == 4294967295u, ""); // ok
268     static_assert(~0u * ~0u == 1u, ""); // ok
269 
270     template<typename T> constexpr bool isinf(T v) { return v && v / 2 == v; }
271 
272     // Floating-point overflow and NaN.
273     constexpr float f1 = 1e38f * 3.4028f; // ok
274     constexpr float f2 = 1e38f * 3.4029f; // ok, +inf is in range of representable values
275     constexpr float f3 = 1e38f / -.2939f; // ok
276     constexpr float f4 = 1e38f / -.2938f; // ok, -inf is in range of representable values
277     constexpr float f5 = 2e38f + 2e38f; // ok, +inf is in range of representable values
278     constexpr float f6 = -2e38f - 2e38f; // ok, -inf is in range of representable values
279     constexpr float f7 = 0.f / 0.f; // expected-error {{constant expression}} expected-note {{division by zero}}
280     constexpr float f8 = 1.f / 0.f; // expected-error {{constant expression}} expected-note {{division by zero}}
281     constexpr float f9 = 1e308 / 1e-308; // ok, +inf
282     constexpr float f10 = f2 - f2; // expected-error {{constant expression}} expected-note {{produces a NaN}}
283     constexpr float f11 = f2 + f4; // expected-error {{constant expression}} expected-note {{produces a NaN}}
284     constexpr float f12 = f2 / f2; // expected-error {{constant expression}} expected-note {{produces a NaN}}
285 #pragma float_control(push)
286 #pragma float_control(except, on)
287 constexpr float pi = 3.14f;
288 constexpr unsigned ubig = 0xFFFFFFFF;
289 constexpr float ce = 1.0 / 3.0; // not-expected-error {{constant expression}} not-expected-note {{floating point arithmetic suppressed in strict evaluation modes}}
290 constexpr int ci = (int) pi;
291 constexpr float fbig = (float) ubig; // not-expected-error {{constant expression}} not-expected-note {{floating point arithmetic suppressed in strict evaluation modes}}
292 constexpr float fabspi = __builtin_fabs(pi); // no error expected
293 constexpr float negpi = -pi; // expect no error on unary operator
294 #pragma float_control(pop)
295     static_assert(!isinf(f1), "");
296     static_assert(isinf(f2), "");
297     static_assert(!isinf(f3), "");
298     static_assert(isinf(f4), "");
299     static_assert(isinf(f5), "");
300     static_assert(isinf(f6), "");
301     static_assert(isinf(f9), "");
302   }
303 
304 #if __cplusplus >= 201703L
305 namespace CompoundAssignment {
306 constexpr int rem() { // expected-error {{constexpr function never produces a constant expression}}
307     int x = ~__INT_MAX__;
308     return x%=-1; // cxx20-note {{value 2147483648 is outside the range of representable values of type 'int'}}
309 }
310 }
311 #endif
312 }
313 
314 // - a lambda-expression (5.1.2);
315 struct Lambda {
316   int n : []{ return 1; }(); // cxx11-error {{constant expression}} cxx11-error {{integral constant expression}} cxx11-note {{non-literal type}}
317 };
318 
319 // - an lvalue-to-rvalue conversion (4.1) unless it is applied to
320 namespace LValueToRValue {
321   // - a non-volatile glvalue of integral or enumeration type that refers to a
322   //   non-volatile const object with a preceding initialization, initialized
323   //   with a constant expression  [Note: a string literal (2.14.5 [lex.string])
324   //   corresponds to an array of such objects. -end note], or
325   volatile const int vi = 1; // expected-note 2{{here}}
326   const int ci = 1;
327   volatile const int &vrci = ci;
328   static_assert(vi, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
329   static_assert(const_cast<int&>(vi), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
330   static_assert(vrci, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
331 
332   // - a non-volatile glvalue of literal type that refers to a non-volatile
333   //   object defined with constexpr, or that refers to a sub-object of such an
334   //   object, or
335   struct V {
336     constexpr V() : v(1) {}
337     volatile int v; // expected-note {{not literal because}}
338   };
339   constexpr V v; // expected-error {{non-literal type}}
340   struct S {
341     constexpr S(int=0) : i(1), v(const_cast<volatile int&>(vi)) {}
342     constexpr S(const S &s) : i(2), v(const_cast<volatile int&>(vi)) {}
343     int i;
344     volatile int &v;
345   };
346   constexpr S s; // ok
347   constexpr volatile S vs; // expected-note {{here}}
348   constexpr const volatile S &vrs = s; // ok
349   static_assert(s.i, "");
350   static_assert(s.v, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
351   static_assert(const_cast<int&>(s.v), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
352   static_assert(vs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
353   static_assert(const_cast<int&>(vs.i), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
354   static_assert(vrs.i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
355 
356   // - a non-volatile glvalue of literal type that refers to a non-volatile
357   //   temporary object whose lifetime has not ended, initialized with a
358   //   constant expression;
359   constexpr volatile S f() { return S(); }
360   static_assert(f().i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
361   static_assert(((volatile const S&&)(S)0).i, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
362 }
363 
364 // DR1312: The proposed wording for this defect has issues, so we ignore this
365 // bullet and instead prohibit casts from pointers to cv void (see core-20842
366 // and core-20845).
367 //
368 // - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
369 // glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
370 // are neither the same type nor similar types (4.4 [conv.qual]);
371 
372 // - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
373 // refers to a non-active member of a union or a subobject thereof;
374 namespace LValueToRValueUnion {
375   // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing
376   // of this.
377   union U { int a, b; } constexpr u = U();
378   static_assert(u.a == 0, "");
379   constexpr const int *bp = &u.b;
380   constexpr int b = *bp; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
381 
382   extern const U pu;
383   constexpr const int *pua = &pu.a;
384   constexpr const int *pub = &pu.b;
385   constexpr U pu = { .b = 1 }; // cxx11-warning {{C++20 extension}}
386   constexpr const int a2 = *pua; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
387   constexpr const int b2 = *pub; // ok
388 }
389 
390 // - an id-expression that refers to a variable or data member of reference type
391 //   unless the reference has a preceding initialization, initialized with a
392 //   constant expression;
393 namespace References {
394   const int a = 2;
395   int &b = *const_cast<int*>(&a);
396   int c = 10; // expected-note 2 {{here}}
397   int &d = c;
398   constexpr int e = 42;
399   int &f = const_cast<int&>(e);
400   extern int &g; // expected-note {{here}}
401   constexpr int &h(); // expected-note {{here}}
402   int &i = h(); // expected-note {{here}}
403   constexpr int &j() { return b; }
404   int &k = j();
405 
406   struct S {
407     int A : a;
408     int B : b;
409     int C : c; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
410     int D : d; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
411     int D2 : &d - &c + 1;
412     int E : e / 2;
413     int F : f - 11;
414     int G : g; // expected-error {{constant expression}} expected-note {{initializer of 'g' is unknown}}
415     int H : h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
416     int I : i; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
417     int J : j();
418     int K : k;
419   };
420 }
421 
422 // - a dynamic_cast (5.2.7);
423 namespace DynamicCast {
424   struct S { int n; };
425   constexpr S s { 16 };
426   struct T {
427     int n : dynamic_cast<const S*>(&s)->n; // cxx11-warning {{constant expression}} cxx11-note {{dynamic_cast}}
428   };
429 }
430 
431 // - a reinterpret_cast (5.2.10);
432 namespace ReinterpretCast {
433   struct S { int n; };
434   constexpr S s { 16 };
435   struct T {
436     int n : reinterpret_cast<const S*>(&s)->n; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
437   };
438   struct U {
439     int m : (long)(S*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
440   };
441 }
442 
443 // - a pseudo-destructor call (5.2.4);
444 namespace PseudoDtor {
445   int k;
446   typedef int I;
447   struct T {
448     int n : (k.~I(), 1); // expected-error {{constant expression}} expected-note {{visible outside that expression}}
449   };
450 
451   constexpr int f(int a = 1) { // cxx11-error {{constant expression}} expected-note {{destroying object 'a' whose lifetime has already ended}}
452     return (
453         a.~I(), // cxx11-note {{pseudo-destructor}}
454         0);
455   }
456   static_assert(f() == 0, ""); // expected-error {{constant expression}}
457 
458   // This is OK in C++20: the union has no active member after the
459   // pseudo-destructor call, so the union destructor has no effect.
460   union U { int x; };
461   constexpr int g(U u = {1}) { // cxx11-error {{constant expression}}
462     return (
463         u.x.~I(), // cxx11-note 2{{pseudo-destructor}}
464         0);
465   }
466   static_assert(g() == 0, ""); // cxx11-error {{constant expression}} cxx11-note {{in call}}
467 }
468 
469 // - increment or decrement operations (5.2.6, 5.3.2);
470 namespace IncDec {
471   int k = 2;
472   struct T {
473     int n : ++k; // expected-error {{constant expression}} cxx20-note {{visible outside}}
474     int m : --k; // expected-error {{constant expression}} cxx20-note {{visible outside}}
475   };
476 }
477 
478 // - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
479 namespace std {
480   struct type_info {
481     virtual ~type_info();
482     const char *name;
483   };
484 }
485 namespace TypeId {
486   struct S { virtual void f(); };
487   constexpr S *p = 0;
488   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 'S'}} cxx20-note {{dereferenced null pointer}}
489 
490   struct T {} t;
491   constexpr const std::type_info &ti2 = typeid(t);
492 }
493 
494 // - a new-expression (5.3.4);
495 // - a delete-expression (5.3.5);
496 namespace NewDelete {
497   constexpr int *p = 0;
498   struct T {
499     int n : *new int(4); // expected-warning {{constant expression}} cxx11-note {{until C++20}} cxx20-note {{was not deallocated}}
500     int m : (delete p, 2); // cxx11-warning {{constant expression}} cxx11-note {{until C++20}}
501   };
502 }
503 
504 // - a relational (5.9) or equality (5.10) operator where the result is
505 //   unspecified;
506 namespace UnspecifiedRelations {
507   int a, b;
508   constexpr int *p = &a, *q = &b;
509   // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
510   // different objects that are not members of the same array or to different
511   // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
512   // and p>=q are unspecified.
513   constexpr bool u1 = p < q; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects '&a' and '&b' has unspecified value}}
514   constexpr bool u2 = p > q; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects '&a' and '&b' has unspecified value}}
515   constexpr bool u3 = p <= q; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects '&a' and '&b' has unspecified value}}
516   constexpr bool u4 = p >= q; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects '&a' and '&b' has unspecified value}}
517   constexpr bool u5 = p < (int*)0; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects '&a' and 'nullptr' has unspecified value}}
518   constexpr bool u6 = p <= (int*)0; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects '&a' and 'nullptr' has unspecified value}}
519   constexpr bool u7 = p > (int*)0; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects '&a' and 'nullptr' has unspecified value}}
520   constexpr bool u8 = p >= (int*)0; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects '&a' and 'nullptr' has unspecified value}}
521   constexpr bool u9 = (int*)0 < q; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects 'nullptr' and '&b' has unspecified value}}
522   constexpr bool u10 = (int*)0 <= q; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects 'nullptr' and '&b' has unspecified value}}
523   constexpr bool u11 = (int*)0 > q; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects 'nullptr' and '&b' has unspecified value}}
524   constexpr bool u12 = (int*)0 >= q; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects 'nullptr' and '&b' has unspecified value}}
525   void f(), g();
526 
527   constexpr void (*pf)() = &f, (*pg)() = &g;
528   constexpr bool u13 = pf < pg; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects '&f' and '&g' has unspecified value}}
529                                 // expected-warning@-1 {{ordered comparison of function pointers}}
530   constexpr bool u14 = pf == pg;
531 
532   // If two pointers point to non-static data members of the same object with
533   // different access control, the result is unspecified.
534   struct A {
535   public:
536     constexpr A() : a(0), b(0) {}
537     int a;
538     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}}
539   private:
540     int b;
541   };
542   static_assert(A().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}}
543   class B {
544   public:
545     A a;
546     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}}
547   protected:
548     A b;
549   };
550   static_assert(B().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}}
551 
552   // If two pointers point to different base sub-objects of the same object, or
553   // one points to a base subobject and the other points to a member, the result
554   // of the comparison is unspecified. This is not explicitly called out by
555   // [expr.rel]p2, but is covered by 'Other pointer comparisons are
556   // unspecified'.
557   struct C {
558     int c[2];
559   };
560   struct D {
561     int d;
562   };
563   struct E : C, D {
564     struct Inner {
565       int f;
566     } e;
567   } e;
568   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}}
569   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}}
570   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}}
571 
572   // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
573   // represent the same address or are both the null pointer [...]; otherwise
574   // the result is unspecified.
575   // Same address restriction removed by CWG2749
576   struct S { int a, b; } s;
577   constexpr void *null = 0;
578   constexpr void *pv = (void*)&s.a;
579   constexpr void *qv = (void*)&s.b;
580   constexpr bool v1 = null < (int*)0;
581   constexpr bool v2 = null < pv; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects 'nullptr' and '&s.a' has unspecified value}}
582   constexpr bool v3 = null == pv;
583   constexpr bool v4 = qv == pv;
584   constexpr bool v5 = qv >= pv;
585   constexpr bool v6 = qv > null; // expected-error {{constant expression}} expected-note {{comparison between pointers to unrelated objects '&s.b' and 'nullptr' has unspecified value}}
586   constexpr bool v7 = qv <= (void*)&s.b;
587   constexpr bool v8 = qv > (void*)&s.a;
588 }
589 
590 // - an assignment or a compound assignment (5.17); or
591 namespace Assignment {
592   int k;
593   struct T {
594     int n : (k = 9); // expected-error {{constant expression}} cxx20-note {{visible outside}}
595     int m : (k *= 2); // expected-error {{constant expression}} cxx20-note {{visible outside}}
596   };
597 
598   struct Literal {
599     constexpr Literal(const char *name) : name(name) {}
600     const char *name;
601   };
602   struct Expr {
603     constexpr Expr(Literal l) : IsLiteral(true), l(l) {}
604     bool IsLiteral;
605     union {
606       Literal l;
607       // ...
608     };
609   };
610   struct MulEq {
611     constexpr MulEq(Expr a, Expr b) : LHS(a), RHS(b) {}
612     Expr LHS;
613     Expr RHS;
614   };
615   constexpr MulEq operator*=(Expr a, Expr b) { return MulEq(a, b); }
616   Literal a("a");
617   Literal b("b");
618   MulEq c = a *= b; // ok
619 }
620 
621 // - a throw-expression (15.1)
622 namespace Throw {
623   struct S {
624     int n : (throw "hello", 10); // expected-error {{constant expression}}
625   };
626 }
627 
628 // PR9999
629 template<unsigned int v>
630 class bitWidthHolding {
631 public:
632   static const
633   unsigned int width = (v == 0 ? 0 : bitWidthHolding<(v >> 1)>::width + 1);
634 };
635 
636 static const int width=bitWidthHolding<255>::width;
637 
638 template<bool b>
639 struct always_false {
640   static const bool value = false;
641 };
642 
643 template<bool b>
644 struct and_or {
645   static const bool and_value = b && and_or<always_false<b>::value>::and_value;
646   static const bool or_value = !b || and_or<always_false<b>::value>::or_value;
647 };
648 
649 static const bool and_value = and_or<true>::and_value;
650 static const bool or_value = and_or<true>::or_value;
651 
652 static_assert(and_value == false, "");
653 static_assert(or_value == true, "");
654 
655 namespace rdar13090123 {
656   typedef __INTPTR_TYPE__ intptr_t;
657 
658   constexpr intptr_t f(intptr_t x) {
659     return (((x) >> 21) * 8);
660   }
661 
662   extern "C" int foo;
663 
664   constexpr intptr_t i = f((intptr_t)&foo - 10); // expected-error{{constexpr variable 'i' must be initialized by a constant expression}} \
665   // expected-note{{reinterpret_cast}}
666 }
667