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
4 // A conditional-expression is a core constant expression unless it involves one
5 // of the following as a potentially evaluated subexpression [...]:
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];
12 int this1
: this1
; // expected-error {{undeclared}}
13 int this2
: this->this1
; // expected-error {{invalid}}
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'}}
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}}
28 struct NonConstexpr2
{
29 constexpr NonConstexpr2(); // expected-note {{here}}
32 struct NonConstexpr3
{
34 int m
: NonConstexpr2().n
; // expected-error {{constant expression}} expected-note {{undefined constructor 'NonConstexpr2'}}
36 struct NonConstexpr4
{
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}}
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}}
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
55 namespace NonConstExprReturn
{
56 static constexpr const int &id_ref(const int &n
) {
59 struct NonConstExprFunction
{
60 int n
: id_ref(16); // ok
62 constexpr const int *address_of(const int &a
) {
65 constexpr const int *return_param(int n
) {
69 int n
: *return_param(0); // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
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
{
79 constexpr T(const int &r
) :
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}}
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}}
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}}
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}}
111 // DR1458: taking the address of an object of incomplete class type
112 namespace IncompleteClassTypeAddr
{
115 constexpr S
*p
= &s
; // ok
116 static_assert(p
, "");
119 constexpr S (*p2
)[] = &sArr
; // ok
122 constexpr S
*operator&() const { return nullptr; }
124 constexpr S
*q
= &s
; // ok
125 static_assert(!q
, "");
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
{
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'}}
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}}
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
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}}
179 constexpr S s
= { 5 };
180 constexpr const int *p
= &s
.m
+ 1;
181 constexpr const int &f(const int *q
) {
184 constexpr int n
= (f(p
), 0); // ok
186 int n
: f(p
); // expected-error {{not an integral constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
191 struct B
: A
{ int n
; };
193 constexpr B
*p
= a
[0] + 4; // expected-error {{constant expression}} expected-note {{element 4 of array of 3 elements}}
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}}
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 *np1
= (int*)nullptr + 0; // ok
208 constexpr const int *np2
= &(*(int(*)[4])nullptr)[0]; // ok
209 constexpr const int *np3
= &(*(int(*)[4])nullptr)[2]; // expected-error {{constant expression}} expected-note {{cannot perform pointer arithmetic on null pointer}}
212 constexpr int f() const { return 0; }
214 constexpr int k1
= c
.f(); // ok
215 constexpr int k2
= ((C
*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced null pointer}}
216 constexpr int k3
= (&c
)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
218 constexpr int k4
= c2
.f(); // ok!
220 constexpr int diff1
= &a
[2] - &a
[0];
221 constexpr int diff2
= &a
[1][3] - &a
[1][0];
222 constexpr int diff3
= &a
[2][0] - &a
[1][0]; // expected-error {{constant expression}} expected-note {{subtracted pointers are not elements of the same array}}
223 static_assert(&a
[2][0] == &a
[1][3], "");
224 constexpr int diff4
= (&b
+ 1) - &b
;
225 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}}
226 constexpr int diff6
= &a
[1][2].n
- &a
[1][2].n
;
227 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}}
231 // Signed int overflow.
232 constexpr int n1
= 2 * 3 * 3 * 7 * 11 * 31 * 151 * 331; // ok
233 constexpr int n2
= 65536 * 32768; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
234 constexpr int n3
= n1
+ 1; // ok
235 constexpr int n4
= n3
+ 1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
236 constexpr int n5
= -65536 * 32768; // ok
237 constexpr int n6
= 3 * -715827883; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
238 constexpr int n7
= -n3
+ -1; // ok
239 constexpr int n8
= -1 + n7
; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
240 constexpr int n9
= n3
- 0; // ok
241 constexpr int n10
= n3
- -1; // expected-error {{constant expression}} expected-note {{value 2147483648 is outside the range of }}
242 constexpr int n11
= -1 - n3
; // ok
243 constexpr int n12
= -2 - n3
; // expected-error {{constant expression}} expected-note {{value -2147483649 is outside the range of }}
244 constexpr int n13
= n5
+ n5
; // expected-error {{constant expression}} expected-note {{value -4294967296 is outside the range of }}
245 constexpr int n14
= n3
- n5
; // expected-error {{constant expression}} expected-note {{value 4294967295 is outside the range of }}
246 constexpr int n15
= n5
* n5
; // expected-error {{constant expression}} expected-note {{value 4611686018427387904 is outside the range of }}
247 constexpr signed char c1
= 100 * 2; // ok expected-warning{{changes value}}
248 constexpr signed char c2
= '\x64' * '\2'; // also ok expected-warning{{changes value}}
249 constexpr long long ll1
= 0x7fffffffffffffff; // ok
250 constexpr long long ll2
= ll1
+ 1; // expected-error {{constant}} expected-note {{ 9223372036854775808 }}
251 constexpr long long ll3
= -ll1
- 1; // ok
252 constexpr long long ll4
= ll3
- 1; // expected-error {{constant}} expected-note {{ -9223372036854775809 }}
253 constexpr long long ll5
= ll3
* ll3
; // expected-error {{constant}} expected-note {{ 85070591730234615865843651857942052864 }}
256 char melchizedek
[2200000000];
257 typedef decltype(melchizedek
[1] - melchizedek
[0]) ptrdiff_t;
258 constexpr ptrdiff_t d1
= &melchizedek
[0x7fffffff] - &melchizedek
[0]; // ok
259 constexpr ptrdiff_t d2
= &melchizedek
[0x80000000u
] - &melchizedek
[0]; // expected-error {{constant expression}} expected-note {{ 2147483648 }}
260 constexpr ptrdiff_t d3
= &melchizedek
[0] - &melchizedek
[0x80000000u
]; // ok
261 constexpr ptrdiff_t d4
= &melchizedek
[0] - &melchizedek
[0x80000001u
]; // expected-error {{constant expression}} expected-note {{ -2147483649 }}
263 // Unsigned int overflow.
264 static_assert(65536u * 65536u == 0u, ""); // ok
265 static_assert(4294967295u + 1u == 0u, ""); // ok
266 static_assert(0u - 1u == 4294967295u, ""); // ok
267 static_assert(~0u * ~0u == 1u, ""); // ok
269 template<typename T
> constexpr bool isinf(T v
) { return v
&& v
/ 2 == v
; }
271 // Floating-point overflow and NaN.
272 constexpr float f1
= 1e38f
* 3.4028f
; // ok
273 constexpr float f2
= 1e38f
* 3.4029f
; // ok, +inf is in range of representable values
274 constexpr float f3
= 1e38f
/ -.2939f
; // ok
275 constexpr float f4
= 1e38f
/ -.2938f
; // ok, -inf is in range of representable values
276 constexpr float f5
= 2e38f
+ 2e38f
; // ok, +inf is in range of representable values
277 constexpr float f6
= -2e38f
- 2e38f
; // ok, -inf is in range of representable values
278 constexpr float f7
= 0.f
/ 0.f
; // expected-error {{constant expression}} expected-note {{division by zero}}
279 constexpr float f8
= 1.f
/ 0.f
; // expected-error {{constant expression}} expected-note {{division by zero}}
280 constexpr float f9
= 1e308
/ 1e-308; // ok, +inf
281 constexpr float f10
= f2
- f2
; // expected-error {{constant expression}} expected-note {{produces a NaN}}
282 constexpr float f11
= f2
+ f4
; // expected-error {{constant expression}} expected-note {{produces a NaN}}
283 constexpr float f12
= f2
/ f2
; // expected-error {{constant expression}} expected-note {{produces a NaN}}
284 #pragma float_control(push)
285 #pragma float_control(except, on)
286 constexpr float pi
= 3.14f
;
287 constexpr unsigned ubig
= 0xFFFFFFFF;
288 constexpr float ce
= 1.0 / 3.0; // not-expected-error {{constant expression}} not-expected-note {{floating point arithmetic suppressed in strict evaluation modes}}
289 constexpr int ci
= (int) pi
;
290 constexpr float fbig
= (float) ubig
; // not-expected-error {{constant expression}} not-expected-note {{floating point arithmetic suppressed in strict evaluation modes}}
291 constexpr float fabspi
= __builtin_fabs(pi
); // no error expected
292 constexpr float negpi
= -pi
; // expect no error on unary operator
293 #pragma float_control(pop)
294 static_assert(!isinf(f1
), "");
295 static_assert(isinf(f2
), "");
296 static_assert(!isinf(f3
), "");
297 static_assert(isinf(f4
), "");
298 static_assert(isinf(f5
), "");
299 static_assert(isinf(f6
), "");
300 static_assert(isinf(f9
), "");
303 #if __cplusplus >= 201703L
304 namespace CompoundAssignment
{
305 constexpr int rem() { // expected-error {{constexpr function never produces a constant expression}}
306 int x
= ~__INT_MAX__
;
307 return x
%=-1; // cxx20-note {{value 2147483648 is outside the range of representable values of type 'int'}}
313 // - a lambda-expression (5.1.2);
315 int n
: []{ return 1; }(); // cxx11-error {{constant expression}} cxx11-error {{integral constant expression}} cxx11-note {{non-literal type}}
318 // - an lvalue-to-rvalue conversion (4.1) unless it is applied to
319 namespace LValueToRValue
{
320 // - a non-volatile glvalue of integral or enumeration type that refers to a
321 // non-volatile const object with a preceding initialization, initialized
322 // with a constant expression [Note: a string literal (2.14.5 [lex.string])
323 // corresponds to an array of such objects. -end note], or
324 volatile const int vi
= 1; // expected-note 2{{here}}
326 volatile const int &vrci
= ci
;
327 static_assert(vi
, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
328 static_assert(const_cast<int&>(vi
), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
329 static_assert(vrci
, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
331 // - a non-volatile glvalue of literal type that refers to a non-volatile
332 // object defined with constexpr, or that refers to a sub-object of such an
335 constexpr V() : v(1) {}
336 volatile int v
; // expected-note {{not literal because}}
338 constexpr V v
; // expected-error {{non-literal type}}
340 constexpr S(int=0) : i(1), v(const_cast<volatile int&>(vi
)) {}
341 constexpr S(const S
&s
) : i(2), v(const_cast<volatile int&>(vi
)) {}
346 constexpr volatile S vs
; // expected-note {{here}}
347 constexpr const volatile S
&vrs
= s
; // ok
348 static_assert(s
.i
, "");
349 static_assert(s
.v
, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
350 static_assert(const_cast<int&>(s
.v
), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vi'}}
351 static_assert(vs
.i
, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
352 static_assert(const_cast<int&>(vs
.i
), ""); // expected-error {{constant expression}} expected-note {{read of volatile object 'vs'}}
353 static_assert(vrs
.i
, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
355 // - a non-volatile glvalue of literal type that refers to a non-volatile
356 // temporary object whose lifetime has not ended, initialized with a
357 // constant expression;
358 constexpr volatile S
f() { return S(); }
359 static_assert(f().i
, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
360 static_assert(((volatile const S
&&)(S
)0).i
, ""); // expected-error {{constant expression}} expected-note {{read of volatile-qualified type}}
363 // DR1312: The proposed wording for this defect has issues, so we ignore this
364 // bullet and instead prohibit casts from pointers to cv void (see core-20842
367 // - an lvalue-to-rvalue conversion (4.1 [conv.lval]) that is applied to a
368 // glvalue of type cv1 T that refers to an object of type cv2 U, where T and U
369 // are neither the same type nor similar types (4.4 [conv.qual]);
371 // - an lvalue-to-rvalue conversion (4.1) that is applied to a glvalue that
372 // refers to a non-active member of a union or a subobject thereof;
373 namespace LValueToRValueUnion
{
374 // test/SemaCXX/constant-expression-cxx11.cpp contains more thorough testing
376 union U
{ int a
, b
; } constexpr u
= U();
377 static_assert(u
.a
== 0, "");
378 constexpr const int *bp
= &u
.b
;
379 constexpr int b
= *bp
; // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
382 constexpr const int *pua
= &pu
.a
;
383 constexpr const int *pub
= &pu
.b
;
384 constexpr U pu
= { .b
= 1 }; // cxx11-warning {{C++20 extension}}
385 constexpr const int a2
= *pua
; // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
386 constexpr const int b2
= *pub
; // ok
389 // - an id-expression that refers to a variable or data member of reference type
390 // unless the reference has a preceding initialization, initialized with a
391 // constant expression;
392 namespace References
{
394 int &b
= *const_cast<int*>(&a
);
395 int c
= 10; // expected-note 2 {{here}}
397 constexpr int e
= 42;
398 int &f
= const_cast<int&>(e
);
399 extern int &g
; // expected-note {{here}}
400 constexpr int &h(); // expected-note {{here}}
401 int &i
= h(); // expected-note {{here}}
402 constexpr int &j() { return b
; }
408 int C
: c
; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
409 int D
: d
; // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
410 int D2
: &d
- &c
+ 1;
413 int G
: g
; // expected-error {{constant expression}} expected-note {{initializer of 'g' is unknown}}
414 int H
: h(); // expected-error {{constant expression}} expected-note {{undefined function 'h'}}
415 int I
: i
; // expected-error {{constant expression}} expected-note {{initializer of 'i' is not a constant expression}}
421 // - a dynamic_cast (5.2.7);
422 namespace DynamicCast
{
424 constexpr S s
{ 16 };
426 int n
: dynamic_cast<const S
*>(&s
)->n
; // cxx11-warning {{constant expression}} cxx11-note {{dynamic_cast}}
430 // - a reinterpret_cast (5.2.10);
431 namespace ReinterpretCast
{
433 constexpr S s
{ 16 };
435 int n
: reinterpret_cast<const S
*>(&s
)->n
; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
438 int m
: (long)(S
*)6; // expected-warning {{constant expression}} expected-note {{reinterpret_cast}}
442 // - a pseudo-destructor call (5.2.4);
443 namespace PseudoDtor
{
447 int n
: (k
.~I(), 1); // expected-error {{constant expression}} expected-note {{visible outside that expression}}
450 constexpr int f(int a
= 1) { // cxx11-error {{constant expression}} expected-note {{destroying object 'a' whose lifetime has already ended}}
452 a
.~I(), // cxx11-note {{pseudo-destructor}}
455 static_assert(f() == 0, ""); // expected-error {{constant expression}}
457 // This is OK in C++20: the union has no active member after the
458 // pseudo-destructor call, so the union destructor has no effect.
460 constexpr int g(U u
= {1}) { // cxx11-error {{constant expression}}
462 u
.x
.~I(), // cxx11-note 2{{pseudo-destructor}}
465 static_assert(g() == 0, ""); // cxx11-error {{constant expression}} cxx11-note {{in call}}
468 // - increment or decrement operations (5.2.6, 5.3.2);
472 int n
: ++k
; // expected-error {{constant expression}} cxx20-note {{visible outside}}
473 int m
: --k
; // expected-error {{constant expression}} cxx20-note {{visible outside}}
477 // - a typeid expression (5.2.8) whose operand is of a polymorphic class type;
480 virtual ~type_info();
485 struct S
{ virtual void f(); };
487 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}}
490 constexpr const std::type_info
&ti2
= typeid(t
);
493 // - a new-expression (5.3.4);
494 // - a delete-expression (5.3.5);
495 namespace NewDelete
{
496 constexpr int *p
= 0;
498 int n
: *new int(4); // expected-warning {{constant expression}} cxx11-note {{until C++20}} cxx20-note {{was not deallocated}}
499 int m
: (delete p
, 2); // cxx11-warning {{constant expression}} cxx11-note {{until C++20}}
503 // - a relational (5.9) or equality (5.10) operator where the result is
505 namespace UnspecifiedRelations
{
507 constexpr int *p
= &a
, *q
= &b
;
508 // C++11 [expr.rel]p2: If two pointers p and q of the same type point to
509 // different objects that are not members of the same array or to different
510 // functions, or if only one of them is null, the results of p<q, p>q, p<=q,
511 // and p>=q are unspecified.
512 constexpr bool u1
= p
< q
; // expected-error {{constant expression}} expected-note {{comparison between '&a' and '&b' has unspecified value}}
513 constexpr bool u2
= p
> q
; // expected-error {{constant expression}} expected-note {{comparison between '&a' and '&b' has unspecified value}}
514 constexpr bool u3
= p
<= q
; // expected-error {{constant expression}} expected-note {{comparison between '&a' and '&b' has unspecified value}}
515 constexpr bool u4
= p
>= q
; // expected-error {{constant expression}} expected-note {{comparison between '&a' and '&b' has unspecified value}}
516 constexpr bool u5
= p
< (int*)0; // expected-error {{constant expression}} expected-note {{comparison between '&a' and 'nullptr' has unspecified value}}
517 constexpr bool u6
= p
<= (int*)0; // expected-error {{constant expression}} expected-note {{comparison between '&a' and 'nullptr' has unspecified value}}
518 constexpr bool u7
= p
> (int*)0; // expected-error {{constant expression}} expected-note {{comparison between '&a' and 'nullptr' has unspecified value}}
519 constexpr bool u8
= p
>= (int*)0; // expected-error {{constant expression}} expected-note {{comparison between '&a' and 'nullptr' has unspecified value}}
520 constexpr bool u9
= (int*)0 < q
; // expected-error {{constant expression}} expected-note {{comparison between 'nullptr' and '&b' has unspecified value}}
521 constexpr bool u10
= (int*)0 <= q
; // expected-error {{constant expression}} expected-note {{comparison between 'nullptr' and '&b' has unspecified value}}
522 constexpr bool u11
= (int*)0 > q
; // expected-error {{constant expression}} expected-note {{comparison between 'nullptr' and '&b' has unspecified value}}
523 constexpr bool u12
= (int*)0 >= q
; // expected-error {{constant expression}} expected-note {{comparison between 'nullptr' and '&b' has unspecified value}}
526 constexpr void (*pf
)() = &f
, (*pg
)() = &g
;
527 constexpr bool u13
= pf
< pg
; // expected-error {{constant expression}} expected-note {{comparison between '&f' and '&g' has unspecified value}}
528 // expected-warning@-1 {{ordered comparison of function pointers}}
529 constexpr bool u14
= pf
== pg
;
531 // If two pointers point to non-static data members of the same object with
532 // different access control, the result is unspecified.
535 constexpr A() : a(0), b(0) {}
537 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}}
541 static_assert(A().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}}
545 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}}
549 static_assert(B().cmp(), ""); // expected-error {{constant expression}} expected-note {{in call}}
551 // If two pointers point to different base sub-objects of the same object, or
552 // one points to a base subobject and the other points to a member, the result
553 // of the comparison is unspecified. This is not explicitly called out by
554 // [expr.rel]p2, but is covered by 'Other pointer comparisons are
567 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}}
568 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}}
569 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 // [expr.rel]p3: Pointers to void can be compared [...] if both pointers
572 // represent the same address or are both the null pointer [...]; otherwise
573 // the result is unspecified.
574 // Same address restriction removed by CWG2749
575 struct S
{ int a
, b
; } s
;
576 constexpr void *null
= 0;
577 constexpr void *pv
= (void*)&s
.a
;
578 constexpr void *qv
= (void*)&s
.b
;
579 constexpr bool v1
= null
< (int*)0;
580 constexpr bool v2
= null
< pv
; // expected-error {{constant expression}} expected-note {{comparison between 'nullptr' and '&s.a' has unspecified value}}
581 constexpr bool v3
= null
== pv
;
582 constexpr bool v4
= qv
== pv
;
583 constexpr bool v5
= qv
>= pv
;
584 constexpr bool v6
= qv
> null
; // expected-error {{constant expression}} expected-note {{comparison between '&s.b' and 'nullptr' has unspecified value}}
585 constexpr bool v7
= qv
<= (void*)&s
.b
;
586 constexpr bool v8
= qv
> (void*)&s
.a
;
589 // - an assignment or a compound assignment (5.17); or
590 namespace Assignment
{
593 int n
: (k
= 9); // expected-error {{constant expression}} cxx20-note {{visible outside}}
594 int m
: (k
*= 2); // expected-error {{constant expression}} cxx20-note {{visible outside}}
598 constexpr Literal(const char *name
) : name(name
) {}
602 constexpr Expr(Literal l
) : IsLiteral(true), l(l
) {}
610 constexpr MulEq(Expr a
, Expr b
) : LHS(a
), RHS(b
) {}
614 constexpr MulEq
operator*=(Expr a
, Expr b
) { return MulEq(a
, b
); }
617 MulEq c
= a
*= b
; // ok
620 // - a throw-expression (15.1)
623 int n
: (throw "hello", 10); // expected-error {{constant expression}}
628 template<unsigned int v
>
629 class bitWidthHolding
{
632 unsigned int width
= (v
== 0 ? 0 : bitWidthHolding
<(v
>> 1)>::width
+ 1);
635 static const int width
=bitWidthHolding
<255>::width
;
638 struct always_false
{
639 static const bool value
= false;
644 static const bool and_value
= b
&& and_or
<always_false
<b
>::value
>::and_value
;
645 static const bool or_value
= !b
|| and_or
<always_false
<b
>::value
>::or_value
;
648 static const bool and_value
= and_or
<true>::and_value
;
649 static const bool or_value
= and_or
<true>::or_value
;
651 static_assert(and_value
== false, "");
652 static_assert(or_value
== true, "");
654 namespace rdar13090123
{
655 typedef __INTPTR_TYPE__
intptr_t;
657 constexpr intptr_t f(intptr_t x
) {
658 return (((x
) >> 21) * 8);
663 constexpr intptr_t i
= f((intptr_t)&foo
- 10); // expected-error{{constexpr variable 'i' must be initialized by a constant expression}} \
664 // expected-note{{reinterpret_cast}}