1 // RUN: %clang_cc1 -std=c++23 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx20_23,cxx23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
2 // RUN: %clang_cc1 -std=c++20 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx11_20,cxx20_23,pre-cxx23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
3 // RUN: %clang_cc1 -std=c++11 -isystem %S/Inputs -fsyntax-only -verify=expected,cxx11_20,cxx11,pre-cxx23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic %s -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
5 // This macro forces its argument to be constant-folded, even if it's not
6 // otherwise a constant expression.
7 #define fold(x) (__builtin_constant_p(x) ? (x) : (x))
9 namespace StaticAssertFoldTest
{
12 static_assert(++x
, "test"); // expected-error {{not an integral constant expression}}
13 // cxx20_23-note@-1 {{cannot modify an object that is visible outside that expression}}
14 static_assert(false, "test"); // expected-error {{test}}
18 int array
[(long)(char *)0]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
19 // expected-warning {{variable length array folded to constant array as an extension}} \
20 // expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
22 typedef decltype(sizeof(char)) size_t;
24 template<typename T
> constexpr T
id(const T
&t
) { return t
; }
25 template<typename T
> constexpr T
min(const T
&a
, const T
&b
) {
28 template<typename T
> constexpr T
max(const T
&a
, const T
&b
) {
31 template<typename T
, size_t N
> constexpr T
*begin(T (&xs
)[N
]) { return xs
; }
32 template<typename T
, size_t N
> constexpr T
*end(T (&xs
)[N
]) { return xs
+ N
; }
35 constexpr int zero() const { return 0; }
38 constexpr int arr
[]; // expected-error {{constexpr variable 'arr' must be initialized by a constant expression}}
39 constexpr int arr2
[2]; // expected-error {{constexpr variable 'arr2' must be initialized by a constant expression}}
40 constexpr int arr3
[2] = {};
42 namespace DerivedToVBaseCast
{
45 struct V
: U
{ int n
; };
46 struct A
: virtual V
{ int n
; };
48 struct B
: virtual A
, Aa
{};
49 struct C
: virtual A
, Aa
{};
56 static_assert((void*)p
!= (void*)q
, "");
57 static_assert((A
*)p
== (A
*)q
, "");
58 static_assert((Aa
*)p
!= (Aa
*)q
, "");
62 static_assert((void*)&pp
!= (void*)&qq
, "");
63 static_assert(&(A
&)pp
== &(A
&)qq
, "");
64 static_assert(&(Aa
&)pp
!= &(Aa
&)qq
, "");
68 constexpr V
*x
= (A
*)p
;
69 static_assert(v
== w
, "");
70 static_assert(v
== x
, "");
72 static_assert((U
*)&d
== p
, "");
73 static_assert((U
*)&d
== q
, "");
74 static_assert((U
*)&d
== v
, "");
75 static_assert((U
*)&d
== w
, "");
76 static_assert((U
*)&d
== x
, "");
79 struct Y1
: virtual X
{};
83 static_assert((X
*)(Y1
*)&z
!= (X
*)(Y2
*)&z
, "");
89 constexpr int n2
= const_cast<int&>(n1
);
90 constexpr int *n3
= const_cast<int*>(&n1
);
91 constexpr int n4
= *const_cast<int*>(&n1
);
92 constexpr const int * const *n5
= const_cast<const int* const*>(&n3
);
93 constexpr int **n6
= const_cast<int**>(&n3
);
94 constexpr int n7
= **n5
;
95 constexpr int n8
= **n6
;
97 // const_cast from prvalue to xvalue.
99 constexpr int n9
= (const_cast<A
&&>(A
{123})).n
;
100 static_assert(n9
== 123, "");
104 namespace TemplateArgumentConversion
{
105 template<int n
> struct IntParam
{};
107 using IntParam0
= IntParam
<0>;
108 using IntParam0
= IntParam
<id(0)>;
109 using IntParam0
= IntParam
<MemberZero().zero
>; // expected-error {{did you mean to call it with no arguments?}}
112 namespace CaseStatements
{
116 case MemberZero().zero
: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}}
117 case id(0): // expected-error {{duplicate case value '0'}}
119 case __builtin_constant_p(true) ? (__SIZE_TYPE__
)&x
: 0:; // expected-error {{constant}}
124 extern int &Recurse1
;
125 int &Recurse2
= Recurse1
; // expected-note {{declared here}}
126 int &Recurse1
= Recurse2
;
127 constexpr int &Recurse3
= Recurse2
; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
129 extern const int RecurseA
;
130 const int RecurseB
= RecurseA
; // expected-note {{declared here}}
131 const int RecurseA
= 10;
132 constexpr int RecurseC
= RecurseB
; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
134 namespace MemberEnum
{
135 struct WithMemberEnum
{
139 static_assert(wme
.A
== 42, "");
142 namespace DefaultArguments
{
145 constexpr int Sum(int a
= 0, const int &b
= 0, const int *c
= &z
, char d
= 0) {
146 return a
+ b
+ *c
+ d
;
149 constexpr int eight
= 8;
150 constexpr const int twentyseven
= 27;
151 static_assert(Sum() == 0, "");
152 static_assert(Sum(1) == 1, "");
153 static_assert(Sum(1, four
) == 5, "");
154 static_assert(Sum(1, eight
, &twentyseven
) == 36, "");
155 static_assert(Sum(1, 2, &four
, eight
) == 15, "");
161 // Note, values passed through an ellipsis can't actually be used.
162 constexpr int F(int a
, ...) { return a
; }
163 static_assert(F(0) == 0, "");
164 static_assert(F(1, 0) == 1, "");
165 static_assert(F(2, "test") == 2, "");
166 static_assert(F(3, &F
) == 3, "");
167 int k
= 0; // expected-note {{here}}
168 static_assert(F(4, k
) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
172 namespace Recursion
{
173 constexpr int fib(int n
) { return n
> 1 ? fib(n
-1) + fib(n
-2) : n
; }
174 static_assert(fib(11) == 89, "");
176 constexpr int gcd_inner(int a
, int b
) {
177 return b
== 0 ? a
: gcd_inner(b
, a
% b
);
179 constexpr int gcd(int a
, int b
) {
180 return gcd_inner(max(a
, b
), min(a
, b
));
183 static_assert(gcd(1749237, 5628959) == 7, "");
186 namespace FunctionCast
{
187 // When folding, we allow functions to be cast to different types. Such
188 // cast functions cannot be called, even if they're constexpr.
189 constexpr int f() { return 1; }
190 typedef double (*DoubleFn
)();
191 typedef int (*IntFn
)();
192 int a
[(int)DoubleFn(f
)()]; // expected-error {{variable length array}} expected-warning{{Clang extension}}
193 int b
[(int)IntFn(f
)()]; // ok
196 namespace StaticMemberFunction
{
198 static constexpr int k
= 42;
199 static constexpr int f(int n
) { return n
* k
+ 2; }
202 constexpr int n
= s
.f(19);
203 static_assert(S::f(19) == 800, "");
204 static_assert(s
.f(19) == 800, "");
205 static_assert(n
== 800, "");
207 constexpr int (*sf1
)(int) = &S::f
;
208 constexpr int (*sf2
)(int) = &s
.f
;
209 constexpr const int *sk
= &s
.k
;
211 // Note, out_of_lifetime returns an invalid pointer value, but we don't do
212 // anything with it (other than copy it around), so there's no UB there.
213 constexpr S
*out_of_lifetime(S s
) { return &s
; } // expected-warning {{address of stack}}
214 static_assert(out_of_lifetime({})->k
== 42, "");
215 static_assert(out_of_lifetime({})->f(3) == 128, "");
217 // Similarly, using an inactive union member breaks no rules.
223 static_assert(u
.s
.k
== 42, "");
224 static_assert(u
.s
.f(1) == 44, "");
226 // And likewise for a past-the-end pointer.
227 static_assert((&s
)[1].k
== 42, "");
228 static_assert((&s
)[1].f(1) == 44, "");
231 namespace ParameterScopes
{
234 constexpr const int &ObscureTheTruth(const int &a
) { return a
; }
235 constexpr const int &MaybeReturnJunk(bool b
, const int a
) {
236 return ObscureTheTruth(b
? a
: k
);
238 static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
239 constexpr int a
= MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
241 constexpr const int MaybeReturnNonstaticRef(bool b
, const int a
) {
242 return ObscureTheTruth(b
? a
: k
);
244 static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
245 constexpr int b
= MaybeReturnNonstaticRef(true, 0); // ok
247 constexpr int InternalReturnJunk(int n
) {
248 return MaybeReturnJunk(true, n
); // expected-note {{read of object outside its lifetime}}
250 constexpr int n3
= InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
252 constexpr int LToR(int &n
) { return n
; }
253 constexpr int GrabCallersArgument(bool which
, int a
, int b
) {
254 return LToR(which
? b
: a
);
256 static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
257 static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
263 constexpr int f(int n
, const int *a
, const int *b
, const int *c
) {
264 return n
== 0 ? 0 : *a
+ f(n
-1, b
, c
, a
);
267 const int x
= 1, y
= 10, z
= 100;
268 static_assert(f(23, &x
, &y
, &z
) == 788, "");
270 constexpr int g(int n
, int a
, int b
, int c
) {
271 return f(n
, &a
, &b
, &c
);
273 static_assert(g(23, x
, y
, z
) == 788, "");
277 namespace FunctionPointers
{
279 constexpr int Double(int n
) { return 2 * n
; }
280 constexpr int Triple(int n
) { return 3 * n
; }
281 constexpr int Twice(int (*F
)(int), int n
) { return F(F(n
)); }
282 constexpr int Quadruple(int n
) { return Twice(Double
, n
); }
283 constexpr auto Select(int n
) -> int (*)(int) {
284 return n
== 2 ? &Double
: n
== 3 ? &Triple
: n
== 4 ? &Quadruple
: 0;
286 constexpr int Apply(int (*F
)(int), int n
) { return F(n
); } // expected-note {{'F' evaluates to a null function pointer}}
288 static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
290 constexpr int Invalid
= Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(nullptr, 0)'}}
294 namespace PointerComparison
{
297 static_assert(&x
== &y
, "false"); // expected-error {{false}}
298 static_assert(&x
!= &y
, "");
299 constexpr bool g1
= &x
== &y
;
300 constexpr bool g2
= &x
!= &y
;
301 constexpr bool g3
= &x
<= &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
302 constexpr bool g4
= &x
>= &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
303 constexpr bool g5
= &x
< &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
304 constexpr bool g6
= &x
> &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
306 struct S
{ int x
, y
; } s
;
307 static_assert(&s
.x
== &s
.y
, "false"); // expected-error {{false}}
308 static_assert(&s
.x
!= &s
.y
, "");
309 static_assert(&s
.x
<= &s
.y
, "");
310 static_assert(&s
.x
>= &s
.y
, "false"); // expected-error {{false}}
311 static_assert(&s
.x
< &s
.y
, "");
312 static_assert(&s
.x
> &s
.y
, "false"); // expected-error {{false}}
314 static_assert(0 == &y
, "false"); // expected-error {{false}}
315 static_assert(0 != &y
, "");
316 constexpr bool n3
= (int*)0 <= &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
317 constexpr bool n4
= (int*)0 >= &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
318 constexpr bool n5
= (int*)0 < &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
319 constexpr bool n6
= (int*)0 > &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
321 static_assert(&x
== 0, "false"); // expected-error {{false}}
322 static_assert(&x
!= 0, "");
323 constexpr bool n9
= &x
<= (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
324 constexpr bool n10
= &x
>= (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
325 constexpr bool n11
= &x
< (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
326 constexpr bool n12
= &x
> (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
328 static_assert(&x
== &x
, "");
329 static_assert(&x
!= &x
, "false"); // expected-error {{false}}
330 static_assert(&x
<= &x
, "");
331 static_assert(&x
>= &x
, "");
332 static_assert(&x
< &x
, "false"); // expected-error {{false}}
333 static_assert(&x
> &x
, "false"); // expected-error {{false}}
335 constexpr S
* sptr
= &s
;
336 constexpr bool dyncast
= sptr
== dynamic_cast<S
*>(sptr
); // cxx11-error {{constant expression}} cxx11-note {{dynamic_cast}}
340 int a
: dynamic_cast<S
*>(sptr
) == dynamic_cast<S
*>(sptr
); // \
341 cxx11
-warning
{{not an integral constant expression
}} \
342 cxx11
-note
{{dynamic_cast is
not allowed in a constant expression
}}
343 int b
: reinterpret_cast<S
*>(sptr
) == reinterpret_cast<S
*>(sptr
); // \
344 expected
-warning
{{not an integral constant expression
}} \
345 expected
-note
{{reinterpret_cast is
not allowed in a constant expression
}}
346 int c
: (S
*)(long)(sptr
) == (S
*)(long)(sptr
); // \
347 expected
-warning
{{not an integral constant expression
}} \
348 expected
-note
{{cast that performs the conversions of a
reinterpret_cast is
not allowed in a constant expression
}}
349 int d
: (S
*)(42) == (S
*)(42); // \
350 expected
-warning
{{not an integral constant expression
}} \
351 expected
-note
{{cast that performs the conversions of a
reinterpret_cast is
not allowed in a constant expression
}}
352 int e
: (Str
*)(sptr
) == (Str
*)(sptr
); // \
353 expected
-warning
{{not an integral constant expression
}} \
354 expected
-note
{{cast that performs the conversions of a
reinterpret_cast is
not allowed in a constant expression
}}
355 int f
: &(U
&)(*sptr
) == &(U
&)(*sptr
); // \
356 expected
-warning
{{not an integral constant expression
}} \
357 expected
-note
{{cast that performs the conversions of a
reinterpret_cast is
not allowed in a constant expression
}}
358 int g
: (S
*)(void*)(sptr
) == sptr
; // \
359 expected
-warning
{{not an integral constant expression
}} \
360 expected
-note
{{cast from
'void *' is
not allowed in a constant expression
}}
363 extern char externalvar
[];
364 constexpr bool constaddress
= (void *)externalvar
== (void *)0x4000UL
; // expected-error {{must be initialized by a constant expression}} expected-note {{reinterpret_cast}}
365 static_assert(0 != "foo", "");
367 // OK: These string literals cannot possibly overlap.
368 static_assert(+"foo" != +"bar", "");
369 static_assert("xfoo" + 1 != "yfoo" + 1, "");
370 static_assert(+"foot" != +"foo", "");
371 static_assert(+"foo\0bar" != +"foo\0baz", "");
373 // These can't overlap because the null terminator for UTF-16 is two bytes wide.
374 static_assert(fold((const char*)u
"A" != (const char*)"\0A\0x"), "");
375 static_assert(fold((const char*)u
"A" != (const char*)"A\0\0x"), "");
377 constexpr const char *string
= "hello";
378 constexpr const char *also_string
= string
;
379 static_assert(string
== string
, "");
380 static_assert(string
== also_string
, "");
382 // These strings may overlap, and so the result of the comparison is unknown.
383 constexpr bool may_overlap_1
= +"foo" == +"foo"; // expected-error {{}} expected-note {{addresses of literals}}
384 constexpr bool may_overlap_2
= +"foo" == +"foo\0bar"; // expected-error {{}} expected-note {{addresses of literals}}
385 constexpr bool may_overlap_3
= +"foo" == "bar\0foo" + 4; // expected-error {{}} expected-note {{addresses of literals}}
386 constexpr bool may_overlap_4
= "xfoo" + 1 == "xfoo" + 1; // expected-error {{}} expected-note {{addresses of literals}}
388 // These may overlap even though they have different encodings.
389 // One of these two comparisons is non-constant, but due to endianness we don't
391 constexpr bool may_overlap_different_encoding
[] =
392 {fold((const char*)u
"A" != (const char*)"xA\0\0\0x" + 1), fold((const char*)u
"A" != (const char*)"x\0A\0\0x" + 1)};
393 // expected-error@-2 {{}} expected-note@-1 {{addresses of literals}}
397 namespace MaterializeTemporary
{
399 constexpr int f(const int &r
) { return r
; }
400 constexpr int n
= f(1);
402 constexpr bool same(const int &a
, const int &b
) { return &a
== &b
; }
403 constexpr bool sameTemporary(const int &n
) { return same(n
, n
); }
405 static_assert(n
, "");
406 static_assert(!same(4, 4), "");
407 static_assert(same(n
, n
), "");
408 static_assert(sameTemporary(9), "");
410 struct A
{ int &&r
; };
411 struct B
{ A
&&a1
; A
&&a2
; };
413 constexpr B b1
{ { 1 }, { 2 } }; // expected-note {{temporary created here}}
414 static_assert(&b1
.a1
!= &b1
.a2
, "");
415 static_assert(&b1
.a1
.r
!= &b1
.a2
.r
, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
417 constexpr B
&&b2
{ { 3 }, { 4 } }; // expected-note {{temporary created here}}
418 static_assert(&b1
!= &b2
, "");
419 static_assert(&b1
.a1
!= &b2
.a1
, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
421 constexpr thread_local B b3
{ { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
423 constexpr static B b1
{ { 1 }, { 2 } }; // ok
424 constexpr thread_local B b2
{ { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
425 constexpr B b3
{ { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
428 constexpr B
&&b4
= ((1, 2), 3, 4, B
{ {10}, {{20}} });
429 static_assert(&b4
!= &b2
, "");
431 // Proposed DR: copy-elision doesn't trigger lifetime extension.
432 // cxx11-warning@+1 2{{temporary whose address is used as value of local variable 'b5' will be destroyed at the end of the full-expression}}
433 constexpr B b5
= B
{ {0}, {0} }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
435 namespace NestedNonStatic
{
436 // Proposed DR: for a reference constant expression to refer to a static
437 // storage duration temporary, that temporary must itself be initialized
438 // by a constant expression (a core constant expression is not enough).
439 struct A
{ int &&r
; };
441 constexpr B a
= { A
{0} }; // ok
442 // cxx11-warning@+1 {{temporary bound to reference member of local variable 'b' will be destroyed at the end of the full-expression}}
443 constexpr B b
= { A(A
{0}) }; // cxx11-error {{constant expression}} cxx11-note {{reference to temporary}} cxx11-note {{here}}
446 namespace FakeInitList
{
447 struct init_list_3_ints
{ const int (&x
)[3]; };
448 struct init_list_2_init_list_3_ints
{ const init_list_3_ints (&x
)[2]; };
449 constexpr init_list_2_init_list_3_ints ils
= { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } };
452 namespace ConstAddedByReference
{
456 int &&r2
= 0; // expected-note {{created here}}
457 constexpr int n2
= r2
; // expected-error {{constant}} expected-note {{read of temporary}}
459 struct A
{ constexpr operator int() const { return 0; }};
460 struct B
{ constexpr operator const int() const { return 0; }};
463 constexpr int na
= ra
;
464 constexpr int nb
= rb
;
466 struct C
{ int &&r
; };
467 constexpr C c1
= {1};
468 constexpr int &c1r
= c1
.r
;
469 constexpr const C
&c2
= {2};
470 constexpr int &c2r
= c2
.r
;
471 constexpr C
&&c3
= {3}; // expected-note {{created here}}
472 constexpr int &c3r
= c3
.r
; // expected-error {{constant}} expected-note {{read of temporary}}
477 constexpr int strcmp_ce(const char *p
, const char *q
) {
478 return (!*p
|| *p
!= *q
) ? *p
- *q
: strcmp_ce(p
+1, q
+1);
481 namespace StringLiteral
{
483 template<typename Char
>
484 constexpr int MangleChars(const Char
*p
) {
485 return *p
+ 3 * (*p
? MangleChars(p
+1) : 0);
488 static_assert(MangleChars("constexpr!") == 1768383, "");
489 static_assert(MangleChars(u8
"constexpr!") == 1768383, "");
490 static_assert(MangleChars(L
"constexpr!") == 1768383, "");
491 static_assert(MangleChars(u
"constexpr!") == 1768383, "");
492 static_assert(MangleChars(U
"constexpr!") == 1768383, "");
494 constexpr char c0
= "nought index"[0];
495 constexpr char c1
= "nice index"[10];
496 constexpr char c2
= "nasty index"[12]; // expected-error {{must be initialized by a constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
497 constexpr char c3
= "negative index"[-1]; // expected-error {{must be initialized by a constant expression}} expected-note {{cannot refer to element -1 of array of 15 elements}}
498 constexpr char c4
= ((char*)(int*)"no reinterpret_casts allowed")[14]; // expected-error {{must be initialized by a constant expression}} expected-note {{cast that performs the conversions of a reinterpret_cast}}
500 constexpr const char *p
= "test" + 2;
501 static_assert(*p
== 's', "");
503 constexpr const char *max_iter(const char *a
, const char *b
) {
504 return *a
< *b
? b
: a
;
506 constexpr const char *max_element(const char *a
, const char *b
) {
507 return (a
+1 >= b
) ? a
: max_iter(a
, max_element(a
+1, b
));
510 constexpr char str
[] = "the quick brown fox jumped over the lazy dog";
511 constexpr const char *max
= max_element(begin(str
), end(str
));
512 static_assert(*max
== 'z', "");
513 static_assert(max
== str
+ 38, "");
515 static_assert(strcmp_ce("hello world", "hello world") == 0, "");
516 static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
517 static_assert(strcmp_ce("constexpr", "test") < 0, "");
518 static_assert(strcmp_ce("", " ") < 0, "");
521 int n
: "foo"[4]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
526 constexpr T() : c
{"foo"} {}
530 static_assert(t
.c
[0] == 'f', "");
531 static_assert(t
.c
[1] == 'o', "");
532 static_assert(t
.c
[2] == 'o', "");
533 static_assert(t
.c
[3] == 0, "");
534 static_assert(t
.c
[4] == 0, "");
535 static_assert(t
.c
[5] == 0, "");
536 static_assert(t
.c
[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
541 } constexpr u
= { { L
"test" }, 0 };
542 static_assert(u
.chars
[2] == L
's', "");
546 constexpr V() : c("hi!") {}
548 static_assert(V().c
[1] == "i"[0], "");
551 constexpr unsigned char a
[] = ("foo"), b
[] = {"foo"}, c
[] = {("foo")},
552 d
[4] = ("foo"), e
[5] = {"foo"}, f
[6] = {("foo")};
553 static_assert(a
[0] == 'f', "");
554 static_assert(b
[1] == 'o', "");
555 static_assert(c
[2] == 'o', "");
556 static_assert(d
[0] == 'f', "");
557 static_assert(e
[1] == 'o', "");
558 static_assert(f
[2] == 'o', "");
559 static_assert(f
[5] == 0, "");
560 static_assert(f
[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
567 template<typename Iter
>
568 constexpr auto Sum(Iter begin
, Iter end
) -> decltype(+*begin
) {
569 return begin
== end
? 0 : *begin
+ Sum(begin
+1, end
);
572 constexpr int xs
[] = { 1, 2, 3, 4, 5 };
573 constexpr int ys
[] = { 5, 4, 3, 2, 1 };
574 constexpr int sum_xs
= Sum(begin(xs
), end(xs
));
575 static_assert(sum_xs
== 15, "");
577 constexpr int ZipFoldR(int (*F
)(int x
, int y
, int c
), int n
,
578 const int *xs
, const int *ys
, int c
) {
580 *xs
, // expected-note {{read of dereferenced one-past-the-end pointer}}
582 ZipFoldR(F
, n
-1, xs
+1, ys
+1, c
)) // \
583 expected
-note
{{in call to
'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
584 expected
-note
{{in call to
'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
587 constexpr int MulAdd(int x
, int y
, int c
) { return x
* y
+ c
; }
588 constexpr int InnerProduct
= ZipFoldR(MulAdd
, 5, xs
, ys
, 0);
589 static_assert(InnerProduct
== 35, "");
591 constexpr int SubMul(int x
, int y
, int c
) { return (x
- y
) * c
; }
592 constexpr int DiffProd
= ZipFoldR(SubMul
, 2, xs
+3, ys
+3, 1);
593 static_assert(DiffProd
== 8, "");
594 static_assert(ZipFoldR(SubMul
, 3, xs
+3, ys
+3, 1), ""); // \
595 expected
-error
{{constant expression
}} \
596 expected
-note
{{in call to
'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
598 constexpr const int *p
= xs
+ 3;
599 constexpr int xs4
= p
[1]; // ok
600 constexpr int xs5
= p
[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
601 constexpr int xs6
= p
[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}}
602 constexpr int xs0
= p
[-3]; // ok
603 constexpr int xs_1
= p
[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
605 constexpr int zs
[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
606 static_assert(zs
[0][0][0][0] == 1, "");
607 static_assert(zs
[1][1][1][1] == 16, "");
608 static_assert(zs
[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
609 static_assert((&zs
[0][0][0][2])[-1] == 2, "");
610 static_assert(**(**(zs
+ 1) + 1) == 11, "");
611 static_assert(*(&(&(*(*&(&zs
[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of array of 2 elements in a constant expression}}
612 static_assert(*(&(&(*(*&(&zs
[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
613 constexpr int err_zs_1_2_0_0
= zs
[1][2][0][0]; // \
614 expected
-error
{{constant expression
}} \
615 expected
-note
{{cannot access array element of pointer past the end
}}
617 constexpr int fail(const int &p
) {
618 return (&p
)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
620 static_assert(fail(*(&(&(*(*&(&zs
[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \
621 expected
-error
{{static assertion expression is
not an integral constant expression
}} \
622 expected
-note
{{in call to
'fail(zs[1][0][1][0])'}}
624 constexpr int arr
[40] = { 1, 2, 3, [8] = 4 };
625 constexpr int SumNonzero(const int *p
) {
626 return *p
+ (*p
? SumNonzero(p
+1) : 0);
628 constexpr int CountZero(const int *p
, const int *q
) {
629 return p
== q
? 0 : (*p
== 0) + CountZero(p
+1, q
);
631 static_assert(SumNonzero(arr
) == 6, "");
632 static_assert(CountZero(arr
, arr
+ 40) == 36, "");
635 constexpr ArrayElem() : n(0) {}
637 constexpr int f() const { return n
; }
640 constexpr ArrayRVal() {}
643 static_assert(ArrayRVal().elems
[3].f() == 0, "");
648 constexpr A(const A
&) {}
654 constexpr B b
{{}, {1, 2, 3, 4, 5}};
656 static_assert(c
.arr
[2] == 3, "");
657 static_assert(c
.arr
[7] == 0, "");
659 // OK: the copy ctor for X doesn't read any members.
660 struct X
{ struct Y
{} y
; } x1
;
664 constexpr int selfref
[2][2][2] = {
665 1, selfref
[0][0][0] + 1,
666 1, selfref
[0][1][0] + 1,
667 1, selfref
[0][1][1] + 1 };
668 static_assert(selfref
[0][0][0] == 1, "");
669 static_assert(selfref
[0][0][1] == 2, "");
670 static_assert(selfref
[0][1][0] == 1, "");
671 static_assert(selfref
[0][1][1] == 2, "");
672 static_assert(selfref
[1][0][0] == 1, "");
673 static_assert(selfref
[1][0][1] == 3, "");
674 static_assert(selfref
[1][1][0] == 0, "");
675 static_assert(selfref
[1][1][1] == 0, "");
677 constexpr int badselfref
[2][2][2] = { // expected-error {{constant expression}}
678 badselfref
[1][0][0] // expected-note {{outside its lifetime}}
681 struct TrivialDefCtor
{ int n
; };
682 typedef TrivialDefCtor TDCArray
[2][2];
683 static_assert(TDCArray
{}[1][1].n
== 0, "");
685 struct NonAggregateTDC
: TrivialDefCtor
{};
686 typedef NonAggregateTDC NATDCArray
[2][2];
687 static_assert(NATDCArray
{}[1][1].n
== 0, "");
691 // Per current CWG direction, we reject any cases where pointer arithmetic is
692 // not statically known to be valid.
693 namespace ArrayOfUnknownBound
{
695 constexpr int *a
= arr
;
696 constexpr int *b
= &arr
[0];
697 static_assert(a
== b
, "");
698 constexpr int *c
= &arr
[1]; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
699 constexpr int *d
= &a
[1]; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
700 constexpr int *e
= a
+ 1; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
704 int b
[]; // expected-warning {{C99}}
707 constexpr int *xb
= x
.b
; // expected-error {{constant}} expected-note {{not supported}}
711 constexpr Y
*p
= yarr
;
712 constexpr int *q
= &p
->a
;
714 extern const int carr
[]; // expected-note {{here}}
715 constexpr int n
= carr
[0]; // expected-error {{constant}} expected-note {{non-constexpr variable}}
717 constexpr int local_extern
[] = {1, 2, 3};
718 void f() { extern const int local_extern
[]; }
719 static_assert(local_extern
[1] == 2, "");
722 namespace DependentValues
{
724 struct I
{ int n
; typedef I V
[10]; };
726 int g(); // expected-note {{declared here}}
727 template<bool B
, typename T
> struct S
: T
{
730 I::V
&cells
= B
? x
: y
;
734 constexpr int n
= g(); // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr function 'g'}}
736 constexpr int m
= this->g(); // ok, could be constexpr
740 extern const int n
; // expected-note {{declared here}}
741 template<typename T
> void f() {
742 // This is ill-formed, because a hypothetical instantiation at the point of
743 // template definition would be ill-formed due to a construct that does not
744 // depend on a template parameter.
745 constexpr int k
= n
; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'n' is unknown}}
747 // It doesn't matter that the instantiation could later become valid:
749 template void f
<int>();
755 struct A
{ constexpr A(int a
, int b
) : k(a
+ b
) {} int k
; };
756 constexpr int fn(const A
&a
) { return a
.k
; }
757 static_assert(fn(A(4,5)) == 9, "");
759 struct B
{ int n
; int m
; } constexpr b
= { 0, b
.n
};
761 constexpr C(C
*this_
) : m(42), n(this_
->m
) {} // ok
766 constexpr D() : c(&c
) {}
768 static_assert(D().c
.n
== 42, "");
771 constexpr E() : p(&p
) {}
774 constexpr const E
&e1
= E();
775 // This is a constant expression if we elide the copy constructor call, and
776 // is not a constant expression if we don't! But we do, so it is.
777 constexpr E e2
= E();
778 static_assert(e2
.p
== &e2
.p
, "");
780 static_assert(e3
.p
== &e3
.p
, "");
782 extern const class F f
;
784 constexpr F() : p(&f
.p
) {}
791 constexpr T(T
*p
) : u1(), u2(p
) {}
797 constexpr U2(T
*p
) : c(p
->u1
.b
) {}
801 constexpr G() : t(&t
) {}
804 static_assert(g
.t
.u1
.a
== 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
805 static_assert(g
.t
.u1
.b
== 42, "");
806 static_assert(g
.t
.u2
.c
== 42, "");
807 static_assert(g
.t
.u2
.d
== 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
815 constexpr S(int n
, const S
*p
) : a(5), b(n
), p(p
), d(n
), q("hello") {}
818 S
global(43, &global
);
820 static_assert(S(15, &global
).b
== 15, "");
822 constexpr bool CheckS(const S
&s
) {
823 return s
.a
== 5 && s
.b
== 27 && s
.p
== &global
&& s
.d
== 27. && s
.q
[3] == 'l';
825 static_assert(CheckS(S(27, &global
)), "");
829 constexpr Arr() : arr
{'x', 'y', 'z'} {}
831 constexpr int hash(Arr
&&a
) {
832 return a
.arr
[0] + a
.arr
[1] * 0x100 + a
.arr
[2] * 0x10000;
834 constexpr int k
= hash(Arr());
835 static_assert(k
== 0x007a7978, "");
838 struct AggregateInit
{
846 constexpr AggregateInit agg1
= { "hello"[0] };
848 static_assert(strcmp_ce(&agg1
.c
, "hello") == 0, "");
849 static_assert(agg1
.n
== 0, "");
850 static_assert(agg1
.d
== 0.0, "");
851 static_assert(agg1
.arr
[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
852 static_assert(agg1
.arr
[0] == 0, "");
853 static_assert(agg1
.arr
[4] == 0, "");
854 static_assert(agg1
.arr
[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
855 static_assert(agg1
.p
== nullptr, "");
857 static constexpr const unsigned char uc
[] = { "foo" };
858 static_assert(uc
[0] == 'f', "");
859 static_assert(uc
[3] == 0, "");
861 namespace SimpleDerivedClass
{
864 constexpr B(int n
) : a(n
) {}
868 constexpr D(int n
) : B(n
) {}
871 static_assert(d
.a
== 3, "");
875 struct Bottom
{ constexpr Bottom() {} };
876 struct Base
: Bottom
{
877 constexpr Base(int a
= 42, const char *b
= "test") : a(a
), b(b
) {}
881 struct Base2
: Bottom
{
882 constexpr Base2(const int &r
) : r(r
) {}
886 struct Derived
: Base
, Base2
{
887 constexpr Derived() : Base(76), Base2(a
) {}
891 constexpr bool operator==(const Base
&a
, const Base
&b
) {
892 return a
.a
== b
.a
&& strcmp_ce(a
.b
, b
.b
) == 0;
896 constexpr Base
base2(76);
897 constexpr Derived derived
;
898 static_assert(derived
.a
== 76, "");
899 static_assert(derived
.b
[2] == 's', "");
900 static_assert(derived
.c
== 76 + 'e', "");
901 static_assert(derived
.q
== 123, "");
902 static_assert(derived
.r
== 76, "");
903 static_assert(&derived
.r
== &derived
.a
, "");
905 static_assert(!(derived
== base
), "");
906 static_assert(derived
== base2
, "");
908 constexpr Bottom
&bot1
= (Base
&)derived
;
909 constexpr Bottom
&bot2
= (Base2
&)derived
;
910 static_assert(&bot1
!= &bot2
, "");
912 constexpr Bottom
*pb1
= (Base
*)&derived
;
913 constexpr Bottom
*pb2
= (Base2
*)&derived
;
914 static_assert(&pb1
!= &pb2
, "");
915 static_assert(pb1
== &bot1
, "");
916 static_assert(pb2
== &bot2
, "");
918 constexpr Base2
&fail
= (Base2
&)bot1
; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base2'}}
919 constexpr Base
&fail2
= (Base
&)*pb2
; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base'}}
920 constexpr Base2
&ok2
= (Base2
&)bot2
;
921 static_assert(&ok2
== &derived
, "");
923 constexpr Base2
*pfail
= (Base2
*)pb1
; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base2'}}
924 constexpr Base
*pfail2
= (Base
*)&bot2
; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base'}}
925 constexpr Base2
*pok2
= (Base2
*)pb2
;
926 static_assert(pok2
== &derived
, "");
927 static_assert(&ok2
== pok2
, "");
928 static_assert((Base2
*)(Derived
*)(Base
*)pb1
== pok2
, "");
929 static_assert((Derived
*)(Base
*)pb1
== (Derived
*)pok2
, "");
931 // Core issue 903: we do not perform constant evaluation when checking for a
932 // null pointer in C++11. Just check for an integer literal with value 0.
933 constexpr Base
*nullB
= 42 - 6 * 7; // expected-error {{cannot initialize a variable of type 'Base *const' with an rvalue of type 'int'}}
934 constexpr Base
*nullB1
= 0;
935 static_assert((Bottom
*)nullB
== 0, "");
936 static_assert((Derived
*)nullB1
== 0, "");
937 static_assert((void*)(Bottom
*)nullB1
== (void*)(Derived
*)nullB1
, "");
938 Base
*nullB2
= '\0'; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'char'}}
940 Base
*nullB4
= false; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'bool'}}
941 Base
*nullB5
= ((0ULL));
942 Base
*nullB6
= 0.; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'double'}}
944 Base
*nullB7
= kNull
; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'Class::Null'}}
945 static_assert(nullB1
== (1 - 1), ""); // expected-error {{comparison between pointer and integer}}
949 namespace ConversionOperators
{
952 constexpr T(int n
) : k(5*n
- 3) {}
953 constexpr operator int() const { return k
; }
958 constexpr S(int n
) : k(2*n
+ 1) {}
959 constexpr operator int() const { return k
; }
960 constexpr operator T() const { return T(k
); }
964 constexpr bool check(T a
, T b
) { return a
== b
.k
; }
966 static_assert(S(5) == 11, "");
967 static_assert(check(S(5), 11), "");
972 constexpr (operator int)() const { return 0; }
974 static_assert(X() == 0, "");
981 constexpr int f() const { return 0; }
982 static constexpr int g() { return 0; }
984 constexpr int x
= f(); // expected-error {{must be initialized by a constant}}
985 // expected-note@-1 {{implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
986 constexpr int y
= this->f(); // expected-error {{must be initialized by a constant}}
987 // expected-note-re@-1 {{{{^}}use of 'this' pointer}}
988 constexpr int z
= g();
989 static_assert(z
== 0, "");
995 namespace Temporaries
{
999 constexpr int f() const;
1000 constexpr int g() const;
1003 constexpr T(int n
) : S(), n(n
) {}
1006 constexpr int S::f() const {
1007 return static_cast<const T
*>(this)->n
; // expected-note 5{{cannot cast}}
1009 constexpr int S::g() const {
1010 // FIXME: Better diagnostic for this.
1011 return this->*(int(S::*))&T::n
; // expected-note {{subexpression}}
1013 // The T temporary is implicitly cast to an S subobject, but we can recover the
1014 // T full-object via a base-to-derived cast, or a derived-to-base-casted member
1016 static_assert(S().f(), ""); // expected-error {{constant expression}} expected-note {{in call to 'S().f()'}}
1017 static_assert(S().g(), ""); // expected-error {{constant expression}} expected-note {{in call to 'S().g()'}}
1019 constexpr const S
& slref
= sobj
;
1020 constexpr const S
&& srref
= S();
1021 constexpr const S
*sptr
= &sobj
;
1022 static_assert(sobj
.f(), ""); // expected-error {{constant expression}} \
1023 expected
-note
{{in call to
'sobj.f()'}}
1024 static_assert(sptr
->f(), ""); // expected-error {{constant expression}} \
1025 expected
-note
{{in call to
'sptr->f()'}}
1026 static_assert(slref
.f(), ""); // expected-error {{constant expression}} \
1027 expected
-note
{{in call to
'slref.f()'}}
1028 static_assert(srref
.f(), ""); // expected-error {{constant expression}} \
1029 expected
-note
{{in call to
'srref.f()'}}
1030 static_assert(T(3).f() == 3, "");
1031 static_assert(T(4).g() == 4, "");
1033 constexpr int f(const S
&s
) {
1034 return static_cast<const T
&>(s
).n
;
1036 constexpr int n
= f(T(5));
1037 static_assert(f(T(5)) == 5, "");
1039 constexpr bool b(int n
) { return &n
; }
1040 static_assert(b(0), "");
1043 NonLiteral(); // cxx23-note {{declared here}}
1046 constexpr int k
= NonLiteral().f(); // expected-error {{constant expression}} \
1047 // pre-cxx23-note {{non-literal type 'NonLiteral'}} \
1048 // cxx23-note {{non-constexpr constructor 'NonLiteral' cannot be used in a constant expression}}
1059 constexpr U u
[4] = { { .a
= 0 }, { .b
= 1 }, { .a
= 2 }, { .b
= 3 } };
1060 static_assert(u
[0].a
== 0, "");
1061 static_assert(u
[0].b
, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
1062 static_assert(u
[1].b
== 1, "");
1063 static_assert((&u
[1].b
)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
1064 static_assert(*(&(u
[1].b
) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}}
1065 static_assert((&(u
[1]) + 1 + 1)->b
== 3, "");
1068 static_assert(v
.a
== 0, "");
1071 constexpr Empty e
= {};
1073 // Make sure we handle trivial copy constructors for unions.
1074 constexpr U x
= {42};
1076 static_assert(y
.a
== 42, "");
1077 static_assert(y
.b
== 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}}
1081 namespace MemberPointer
{
1083 constexpr A(int n
) : n(n
) {}
1085 constexpr int f() const { return n
+ 3; }
1088 static_assert(A(5).*&A::n
== 5, "");
1089 static_assert((&a
)->*&A::n
== 7, "");
1090 static_assert((A(8).*&A::f
)() == 11, "");
1091 static_assert(((&a
)->*&A::f
)() == 10, "");
1094 constexpr B(int n
, int m
) : A(n
), m(m
) {}
1096 constexpr int g() const { return n
+ m
+ 1; }
1098 constexpr B
b(9, 13);
1099 static_assert(B(4, 11).*&A::n
== 4, "");
1100 static_assert(B(4, 11).*&B::m
== 11, "");
1101 static_assert(B(4, 11).*(int(A::*))&B::m
== 11, "");
1102 static_assert((&b
)->*&A::n
== 9, "");
1103 static_assert((&b
)->*&B::m
== 13, "");
1104 static_assert((&b
)->*(int(A::*))&B::m
== 13, "");
1105 static_assert((B(4, 11).*&A::f
)() == 7, "");
1106 static_assert((B(4, 11).*&B::g
)() == 16, "");
1107 static_assert((B(4, 11).*(int(A::*)()const)&B::g
)() == 16, "");
1108 static_assert(((&b
)->*&A::f
)() == 12, "");
1109 static_assert(((&b
)->*&B::g
)() == 23, "");
1110 static_assert(((&b
)->*(int(A::*)()const)&B::g
)() == 23, "");
1113 constexpr S(int m
, int n
, int (S::*pf
)() const, int S::*pn
) :
1114 m(m
), n(n
), pf(pf
), pn(pn
) {}
1115 constexpr S() : m(), n(), pf(&S::f
), pn(&S::n
) {}
1117 constexpr int f() const { return this->*pn
; }
1118 virtual int g() const;
1121 int (S::*pf
)() const;
1125 constexpr int S::*pm
= &S::m
;
1126 constexpr int S::*pn
= &S::n
;
1127 constexpr int (S::*pf
)() const = &S::f
;
1128 constexpr int (S::*pg
)() const = &S::g
;
1130 constexpr S
s(2, 5, &S::f
, &S::m
);
1132 static_assert((s
.*&S::f
)() == 2, "");
1133 static_assert((s
.*s
.pf
)() == 2, "");
1135 static_assert(pf
== &S::f
, "");
1136 static_assert(pf
== s
.*&S::pf
, "");
1137 static_assert(pm
== &S::m
, "");
1138 static_assert(pm
!= pn
, "");
1139 static_assert(s
.pn
!= pn
, "");
1140 static_assert(s
.pn
== pm
, "");
1141 static_assert(pg
!= nullptr, "");
1142 static_assert(pf
!= nullptr, "");
1143 static_assert((int S::*)nullptr == nullptr, "");
1144 static_assert(pg
== pg
, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
1145 static_assert(pf
!= pg
, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
1147 template<int n
> struct T
: T
<n
-1> {};
1148 template<> struct T
<0> { int n
; };
1149 template<> struct T
<30> : T
<29> { int m
; };
1154 constexpr int (T
<10>::*deepn
) = &T
<0>::n
;
1155 static_assert(&(t17
.*deepn
) == &t17
.n
, "");
1156 static_assert(deepn
== &T
<2>::n
, "");
1158 constexpr int (T
<15>::*deepm
) = (int(T
<10>::*))&T
<30>::m
;
1159 constexpr int *pbad
= &(t17
.*deepm
); // expected-error {{constant expression}}
1160 static_assert(&(t30
.*deepm
) == &t30
.m
, "");
1161 static_assert(deepm
== &T
<50>::m
, "");
1162 static_assert(deepm
!= deepn
, "");
1164 constexpr T
<5> *p17_5
= &t17
;
1165 constexpr T
<13> *p17_13
= (T
<13>*)p17_5
;
1166 constexpr T
<23> *p17_23
= (T
<23>*)p17_13
; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'T<17>' to type 'T<23>'}}
1167 static_assert(&(p17_5
->*(int(T
<3>::*))deepn
) == &t17
.n
, "");
1168 static_assert(&(p17_13
->*deepn
) == &t17
.n
, "");
1169 constexpr int *pbad2
= &(p17_13
->*(int(T
<9>::*))deepm
); // expected-error {{constant expression}}
1171 constexpr T
<5> *p30_5
= &t30
;
1172 constexpr T
<23> *p30_23
= (T
<23>*)p30_5
;
1173 constexpr T
<13> *p30_13
= p30_23
;
1174 static_assert(&(p30_5
->*(int(T
<3>::*))deepn
) == &t30
.n
, "");
1175 static_assert(&(p30_13
->*deepn
) == &t30
.n
, "");
1176 static_assert(&(p30_23
->*deepn
) == &t30
.n
, "");
1177 static_assert(&(p30_5
->*(int(T
<2>::*))deepm
) == &t30
.m
, "");
1178 static_assert(&(((T
<17>*)p30_13
)->*deepm
) == &t30
.m
, "");
1179 static_assert(&(p30_23
->*deepm
) == &t30
.m
, "");
1181 struct Base
{ int n
; };
1182 template<int N
> struct Mid
: Base
{};
1183 struct Derived
: Mid
<0>, Mid
<1> {};
1184 static_assert(&Mid
<0>::n
== &Mid
<1>::n
, "");
1185 static_assert((int Derived::*)(int Mid
<0>::*)&Mid
<0>::n
!=
1186 (int Derived::*)(int Mid
<1>::*)&Mid
<1>::n
, "");
1187 static_assert(&Mid
<0>::n
== (int Mid
<0>::*)&Base::n
, "");
1189 constexpr int apply(const A
&a
, int (A::*f
)() const) {
1192 static_assert(apply(A(2), &A::f
) == 5, "");
1195 namespace ArrayBaseDerived
{
1201 struct Derived
: Base
{
1202 constexpr Derived() {}
1203 constexpr const int *f() const { return &n
; }
1206 constexpr Derived a
[10];
1207 constexpr Derived
*pd3
= const_cast<Derived
*>(&a
[3]);
1208 constexpr Base
*pb3
= const_cast<Derived
*>(&a
[3]);
1209 static_assert(pb3
== pd3
, "");
1211 // pb3 does not point to an array element.
1212 constexpr Base
*pb4
= pb3
+ 1; // ok, one-past-the-end pointer.
1213 constexpr int pb4n
= pb4
->n
; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}}
1214 constexpr Base
*err_pb5
= pb3
+ 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}}
1215 constexpr int err_pb5n
= err_pb5
->n
; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}}
1216 constexpr Base
*err_pb2
= pb3
- 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}}
1217 constexpr int err_pb2n
= err_pb2
->n
; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}}
1218 constexpr Base
*pb3a
= pb4
- 1;
1220 // pb4 does not point to a Derived.
1221 constexpr Derived
*err_pd4
= (Derived
*)pb4
; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}}
1222 constexpr Derived
*pd3a
= (Derived
*)pb3a
;
1223 constexpr int pd3n
= pd3a
->n
;
1225 // pd3a still points to the Derived array.
1226 constexpr Derived
*pd6
= pd3a
+ 3;
1227 static_assert(pd6
== &a
[6], "");
1228 constexpr Derived
*pd9
= pd6
+ 3;
1229 constexpr Derived
*pd10
= pd6
+ 4;
1230 constexpr int pd9n
= pd9
->n
; // ok
1231 constexpr int err_pd10n
= pd10
->n
; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}}
1232 constexpr int pd0n
= pd10
[-10].n
;
1233 constexpr int err_pdminus1n
= pd10
[-11].n
; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}}
1235 constexpr Base
*pb9
= pd9
;
1236 constexpr const int *(Base::*pfb
)() const =
1237 static_cast<const int *(Base::*)() const>(&Derived::f
);
1238 static_assert((pb9
->*pfb
)() == &a
[9].n
, "");
1246 constexpr complex(int re
= 0, int im
= 0) : re(re
), im(im
) {}
1247 constexpr complex(const complex &o
) : re(o
.re
), im(o
.im
) {}
1248 constexpr complex operator-() const { return complex(-re
, -im
); }
1249 friend constexpr complex operator+(const complex &l
, const complex &r
) {
1250 return complex(l
.re
+ r
.re
, l
.im
+ r
.im
);
1252 friend constexpr complex operator-(const complex &l
, const complex &r
) {
1255 friend constexpr complex operator*(const complex &l
, const complex &r
) {
1256 return complex(l
.re
* r
.re
- l
.im
* r
.im
, l
.re
* r
.im
+ l
.im
* r
.re
);
1258 friend constexpr bool operator==(const complex &l
, const complex &r
) {
1259 return l
.re
== r
.re
&& l
.im
== r
.im
;
1261 constexpr bool operator!=(const complex &r
) const {
1262 return re
!= r
.re
|| im
!= r
.im
;
1264 constexpr int real() const { return re
; }
1265 constexpr int imag() const { return im
; }
1268 constexpr complex i
= complex(0, 1);
1269 constexpr complex k
= (3 + 4*i
) * (6 - 4*i
);
1270 static_assert(complex(1,0).real() == 1, "");
1271 static_assert(complex(1,0).imag() == 0, "");
1272 static_assert(((complex)1).imag() == 0, "");
1273 static_assert(k
.real() == 34, "");
1274 static_assert(k
.imag() == 12, "");
1275 static_assert(k
- 34 == 12*i
, "");
1276 static_assert((complex)1 == complex(1), "");
1277 static_assert((complex)1 != complex(0, 1), "");
1278 static_assert(complex(1) == complex(1), "");
1279 static_assert(complex(1) != complex(0, 1), "");
1280 constexpr complex makeComplex(int re
, int im
) { return complex(re
, im
); }
1281 static_assert(makeComplex(1,0) == complex(1), "");
1282 static_assert(makeComplex(1,0) != complex(0, 1), "");
1284 class complex_wrap
: public complex {
1286 constexpr complex_wrap(int re
, int im
= 0) : complex(re
, im
) {}
1287 constexpr complex_wrap(const complex_wrap
&o
) : complex(o
) {}
1290 static_assert((complex_wrap
)1 == complex(1), "");
1291 static_assert((complex)1 != complex_wrap(0, 1), "");
1292 static_assert(complex(1) == complex_wrap(1), "");
1293 static_assert(complex_wrap(1) != complex(0, 1), "");
1294 constexpr complex_wrap
makeComplexWrap(int re
, int im
) {
1295 return complex_wrap(re
, im
);
1297 static_assert(makeComplexWrap(1,0) == complex(1), "");
1298 static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
1300 constexpr auto GH55390
= 1 / 65536j
; // expected-note {{division by zero}} \
1301 // expected-error {{constexpr variable 'GH55390' must be initialized by a constant expression}} \
1302 // expected-warning {{imaginary constants are a GNU extension}}
1306 struct A
{ constexpr bool operator==(int x
) const { return true; } };
1307 struct B
{ B(); A
& x
; }; // cxx23-note {{declared here}}
1308 static_assert(B().x
== 3, ""); // expected-error {{constant expression}} \
1309 // pre-cxx23-note {{non-literal type 'B' cannot be used in a constant expression}} \
1310 // cxx23-note {{non-constexpr constructor 'B' cannot be used in a constant expression}}
1312 constexpr bool f(int k
) { // cxx11_20-error {{constexpr function never produces a constant expression}}
1313 return B().x
== k
; // cxx11_20-note {{non-literal type 'B' cannot be used in a constant expression}}
1317 namespace ExprWithCleanups
{
1318 struct A
{ A(); ~A(); int get(); };
1319 constexpr int get(bool FromA
) { return FromA
? A().get() : 1; }
1320 constexpr int n
= get(false);
1323 namespace Volatile
{
1325 volatile constexpr int n1
= 0; // expected-note {{here}}
1326 volatile const int n2
= 0; // expected-note {{here}}
1327 int n3
= 37; // expected-note {{declared here}}
1329 constexpr int m1
= n1
; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1330 constexpr int m2
= n2
; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1331 constexpr int m1b
= const_cast<const int&>(n1
); // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
1332 constexpr int m2b
= const_cast<const int&>(n2
); // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
1334 struct T
{ int n
; };
1335 const T t
= { 42 }; // expected-note {{declared here}}
1337 constexpr int f(volatile int &&r
) {
1338 return r
; // expected-note {{read of volatile-qualified type 'volatile int'}}
1340 constexpr int g(volatile int &&r
) {
1341 return const_cast<int&>(r
); // expected-note {{read of volatile temporary is not allowed in a constant expression}}
1344 int j
: f(0); // expected-error {{constant expression}} expected-note {{in call to 'f(0)'}}
1345 int k
: g(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'g(0)'}}
1346 int l
: n3
; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
1347 int m
: t
.n
; // expected-warning{{width of bit-field 'm' (42 bits)}} expected-warning{{expression is not an integral constant expression}} expected-note{{read of non-constexpr variable 't' is not allowed}}
1352 namespace ExternConstexpr
{
1353 extern constexpr int n
= 0;
1354 extern constexpr int m
; // expected-error {{constexpr variable declaration must be a definition}}
1356 extern constexpr int i
; // expected-error {{constexpr variable declaration must be a definition}}
1357 constexpr int j
= 0;
1358 constexpr int k
; // expected-error {{constexpr variable 'k' must be initialized by a constant expression}}
1362 constexpr int g() { return q
; } // expected-note {{outside its lifetime}}
1363 constexpr int q
= g(); // expected-error {{constant expression}} expected-note {{in call}}
1365 extern int r
; // cxx11_20-note {{here}}
1366 constexpr int h() { return r
; } // cxx11_20-error {{never produces a constant}} cxx11_20-note {{read of non-const}}
1368 struct S
{ int n
; };
1370 constexpr int x() { return s
.n
; } // expected-note {{outside its lifetime}}
1371 constexpr S s
= {x()}; // expected-error {{constant expression}} expected-note {{in call}}
1374 namespace ComplexConstexpr
{
1375 constexpr _Complex
float test1
= {}; // expected-warning {{'_Complex' is a C99 extension}}
1376 constexpr _Complex
float test2
= {1}; // expected-warning {{'_Complex' is a C99 extension}}
1377 constexpr _Complex
double test3
= {1,2}; // expected-warning {{'_Complex' is a C99 extension}}
1378 constexpr _Complex
int test4
= {4}; // expected-warning {{'_Complex' is a C99 extension}}
1379 constexpr _Complex
int test5
= 4; // expected-warning {{'_Complex' is a C99 extension}}
1380 constexpr _Complex
int test6
= {5,6}; // expected-warning {{'_Complex' is a C99 extension}}
1381 typedef _Complex
float fcomplex
; // expected-warning {{'_Complex' is a C99 extension}}
1382 constexpr fcomplex test7
= fcomplex();
1384 constexpr const double &t2r
= __real test3
;
1385 constexpr const double &t2i
= __imag test3
;
1386 static_assert(&t2r
+ 1 == &t2i
, "");
1387 static_assert(t2r
== 1.0, "");
1388 static_assert(t2i
== 2.0, "");
1389 constexpr const double *t2p
= &t2r
;
1390 static_assert(t2p
[-1] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element -1 of array of 2 elements}}
1391 static_assert(t2p
[0] == 1.0, "");
1392 static_assert(t2p
[1] == 2.0, "");
1393 static_assert(t2p
[2] == 0.0, ""); // expected-error {{constant expr}} expected-note {{one-past-the-end pointer}}
1394 static_assert(t2p
[3] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element 3 of array of 2 elements}}
1395 constexpr _Complex
float *p
= 0; // expected-warning {{'_Complex' is a C99 extension}}
1396 constexpr float pr
= __real
*p
; // expected-error {{constant expr}} expected-note {{cannot access real component of null}}
1397 constexpr float pi
= __imag
*p
; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of null}}
1398 constexpr const _Complex
double *q
= &test3
+ 1; // expected-warning {{'_Complex' is a C99 extension}}
1399 constexpr double qr
= __real
*q
; // expected-error {{constant expr}} expected-note {{cannot access real component of pointer past the end}}
1400 constexpr double qi
= __imag
*q
; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of pointer past the end}}
1402 static_assert(__real test6
== 5, "");
1403 static_assert(__imag test6
== 6, "");
1404 static_assert(&__imag test6
== &__real test6
+ 1, "");
1407 // _Atomic(T) is exactly like T for the purposes of constant expression
1410 constexpr _Atomic
int n
= 3; // expected-warning {{'_Atomic' is a C11 extension}}
1412 struct S
{ _Atomic(double) d
; }; // expected-warning {{'_Atomic' is a C11 extension}}
1413 constexpr S s
= { 0.5 };
1414 constexpr double d1
= s
.d
;
1415 constexpr double d2
= n
;
1416 constexpr _Atomic
double d3
= n
; // expected-warning {{'_Atomic' is a C11 extension}}
1418 constexpr _Atomic(int) n2
= d3
; // expected-warning {{'_Atomic' is a C11 extension}}
1419 static_assert(d1
== 0.5, "");
1420 static_assert(d3
== 3.0, "");
1424 _Atomic(int) value
; // expected-warning {{'_Atomic' is a C11 extension}}
1425 constexpr TestVar(int value
) : value(value
) {}
1427 constexpr TestVar testVar
{-1};
1428 static_assert(testVar
.value
== -1, "");
1433 struct B
{ _Atomic(A
) a
; }; // expected-warning {{'_Atomic' is a C11 extension}}
1434 constexpr int n
= (B(), B(), 0);
1436 struct C
{ constexpr C() {} void *self
= this; };
1437 constexpr _Atomic(C
) c
= C(); // expected-warning {{'_Atomic' is a C11 extension}}
1441 namespace InstantiateCaseStmt
{
1442 template<int x
> constexpr int f() { return x
; }
1443 template<int x
> int g(int c
) { switch(c
) { case f
<x
>(): return 1; } return 0; }
1444 int gg(int c
) { return g
<4>(c
); }
1447 namespace ConvertedConstantExpr
{
1449 extern int &n
; // expected-note 2{{declared here}}
1451 constexpr int k
= 4;
1452 int &m
= const_cast<int&>(k
);
1454 // If we have nothing more interesting to say, ensure we don't produce a
1455 // useless note and instead just point to the non-constant subexpression.
1458 en
= n
, // expected-error {{not a constant expression}} expected-note {{initializer of 'n' is unknown}}
1459 eo
= (m
+ // expected-error {{not a constant expression}}
1460 n
// expected-note {{initializer of 'n' is unknown}}
1462 eq
= reinterpret_cast<long>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}}
1466 namespace IndirectField
{
1468 struct { // expected-warning {{GNU extension}}
1469 union { // expected-warning {{declared in an anonymous struct}}
1470 struct { // expected-warning {{GNU extension}} expected-warning {{declared in an anonymous union}}
1482 constexpr S(int a
, int b
, int d
, int e
) : a(a
), b(b
), d(d
), e(e
) {}
1483 constexpr S(int c
, int d
, int f
) : c(c
), d(d
), f(f
) {}
1486 constexpr S
s1(1, 2, 3, 4);
1487 constexpr S
s2(5, 6, 7);
1489 // FIXME: The diagnostics here do a very poor job of explaining which unnamed
1490 // member is active and which is requested.
1491 static_assert(s1
.a
== 1, "");
1492 static_assert(s1
.b
== 2, "");
1493 static_assert(s1
.c
== 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1494 static_assert(s1
.d
== 3, "");
1495 static_assert(s1
.e
== 4, "");
1496 static_assert(s1
.f
== 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1498 static_assert(s2
.a
== 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1499 static_assert(s2
.b
== 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1500 static_assert(s2
.c
== 5, "");
1501 static_assert(s2
.d
== 6, "");
1502 static_assert(s2
.e
== 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1503 static_assert(s2
.f
== 7, "");
1506 // DR1405: don't allow reading mutable members in constant expressions.
1507 namespace MutableMembers
{
1509 mutable int n
; // expected-note 3{{declared here}}
1510 } constexpr mm
= { 4 };
1511 constexpr int mmn
= mm
.n
; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1512 int x
= (mm
.n
= 1, 3);
1513 constexpr int mmn2
= mm
.n
; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1515 // Here's one reason why allowing this would be a disaster...
1516 template<int n
> struct Id
{ int k
= n
; };
1518 constexpr MM m
= { 0 };
1520 return Id
<m
.n
>().k
; // expected-error {{not a constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1523 struct A
{ int n
; };
1524 struct B
{ mutable A a
; }; // expected-note {{here}}
1526 constexpr C c
[3] = {};
1527 constexpr int k
= c
[1].b
.a
.n
; // expected-error {{constant expression}} expected-note {{mutable}}
1529 struct D
{ int x
; mutable int y
; }; // expected-note {{here}}
1530 constexpr D d1
= { 1, 2 };
1532 constexpr D d2
= d1
; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1537 mutable int b
; // expected-note {{here}}
1540 constexpr E e1
= {{1}};
1541 constexpr E e2
= e1
; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1548 struct Y
: X
{ X x
; U u
; };
1552 // This is OK; we don't actually read any mutable state here.
1553 constexpr F f1
= {};
1554 constexpr F f2
= f1
;
1559 mutable U u
; // expected-note {{here}}
1561 constexpr G g1
= {};
1562 constexpr G g2
= g1
; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1563 constexpr G::U gu1
= {};
1564 constexpr G::U gu2
= gu1
;
1567 mutable G::X gx
; // expected-note {{here}}
1569 constexpr H h1
= {};
1570 constexpr H h2
= h1
; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1575 constexpr int n
= (long)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}}
1576 constexpr int m
= fold((long)(char*)123); // ok
1577 static_assert(m
== 123, "");
1583 constexpr const int &f(const int &n
) { return n
; }
1584 constexpr int k1
= f(0); // ok
1589 constexpr const Wrap
&g(const Wrap
&w
) { return w
; }
1590 constexpr int k2
= g({0}).value
; // ok
1592 // The temporary here has static storage duration, so we can bind a constexpr
1594 constexpr const int &i
= 1;
1595 constexpr const int j
= i
;
1596 static_assert(j
== 1, "");
1598 // The temporary here is not const, so it can't be read outside the expression
1599 // in which it was created (per the C++14 rules, which we use to avoid a C++11
1601 constexpr int &&k
= 1; // expected-note {{temporary created here}}
1602 constexpr const int l
= k
; // expected-error {{constant expression}} expected-note {{read of temporary}}
1605 // The temporary here has automatic storage duration, so we can't bind a
1606 // constexpr reference to it.
1607 constexpr const int &i
= 1; // expected-error {{constant expression}} expected-note 2{{temporary}}
1612 namespace RecursiveOpaqueExpr
{
1613 template<typename Iter
>
1614 constexpr auto LastNonzero(Iter p
, Iter q
) -> decltype(+*p
) {
1615 return p
!= q
? (LastNonzero(p
+1, q
) ?: *p
) : 0; // expected-warning {{GNU}}
1618 constexpr int arr1
[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 0 };
1619 static_assert(LastNonzero(begin(arr1
), end(arr1
)) == 4, "");
1621 constexpr int arr2
[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 5 };
1622 static_assert(LastNonzero(begin(arr2
), end(arr2
)) == 5, "");
1624 constexpr int arr3
[] = {
1625 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1626 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1627 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1628 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1629 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1630 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1631 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
1632 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1633 static_assert(LastNonzero(begin(arr3
), end(arr3
)) == 2, "");
1636 namespace VLASizeof
{
1638 void f(int k
) { // expected-note {{here}}
1639 int arr
[k
]; // expected-warning {{Clang extension}} expected-note {{function parameter 'k'}}
1640 constexpr int n
= 1 +
1641 sizeof(arr
) // expected-error {{constant expression}}
1646 namespace CompoundLiteral
{
1647 // Matching GCC, file-scope array compound literals initialized by constants
1648 // are lifetime-extended.
1649 constexpr int *p
= (int*)(int[1]){3}; // expected-warning {{C99}}
1650 static_assert(*p
== 3, ""); // expected-error {{static assertion expression is not an integral constant expression}}
1651 // expected-note@-1 {{subexpression not valid}}
1652 // expected-note@-3 {{declared here}}
1653 static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}}
1654 // expected-error@-1 {{static assertion expression is not an integral constant expression}}
1655 // expected-note@-2 {{subexpression not valid}}
1656 // expected-note@-3 {{declared here}}
1658 // Other kinds are not.
1659 struct X
{ int a
[2]; };
1660 constexpr int *n
= (X
){1, 2}.a
; // expected-warning {{C99}} expected-warning {{temporary}}
1661 // expected-error@-1 {{constant expression}}
1662 // expected-note@-2 {{pointer to subobject of temporary}}
1663 // expected-note@-3 {{temporary created here}}
1666 static constexpr int *p
= (int*)(int[1]){3}; // expected-warning {{C99}} expected-warning {{temporary}}
1667 // expected-error@-1 {{constant expression}}
1668 // expected-note@-2 {{pointer to subobject of temporary}}
1669 // expected-note@-3 {{temporary created here}}
1670 static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}}
1675 typedef int __attribute__((vector_size(16))) VI4
;
1676 constexpr VI4
f(int n
) {
1677 return VI4
{ n
* 3, n
+ 4, n
- 5, n
/ 6 };
1679 constexpr auto v1
= f(10);
1681 typedef double __attribute__((vector_size(32))) VD4
;
1682 constexpr VD4
g(int n
) {
1683 return (VD4
) { n
/ 2.0, n
+ 1.5, n
- 5.4, n
* 0.9 }; // expected-warning {{C99}}
1685 constexpr auto v2
= g(4);
1689 namespace InvalidClasses
{
1691 struct X
; // expected-note {{forward declaration}}
1692 struct Y
{ bool b
; X x
; }; // expected-error {{field has incomplete type}}
1698 namespace NamespaceAlias
{
1700 namespace NS
= NamespaceAlias
; // cxx11-warning {{use of this statement in a constexpr function is a C++14 extension}}
1701 return &NS::f
!= nullptr;
1705 // Constructors can be implicitly constexpr, even for a non-literal type.
1706 namespace ImplicitConstexpr
{
1707 struct Q
{ Q() = default; Q(const Q
&) = default; Q(Q
&&) = default; ~Q(); }; // expected-note 3{{here}}
1708 struct R
{ constexpr R() noexcept
; constexpr R(const R
&) noexcept
; constexpr R(R
&&) noexcept
; ~R() noexcept
; };
1709 struct S
{ R r
; }; // expected-note 3{{here}}
1710 struct T
{ T(const T
&) noexcept
; T(T
&&) noexcept
; ~T() noexcept
; };
1711 struct U
{ T t
; }; // cxx11_20-note 3{{here}}
1712 static_assert(!__is_literal_type(Q
), "");
1713 static_assert(!__is_literal_type(R
), "");
1714 static_assert(!__is_literal_type(S
), "");
1715 static_assert(!__is_literal_type(T
), "");
1716 static_assert(!__is_literal_type(U
), "");
1718 friend Q::Q() noexcept
; // expected-error {{follows constexpr}}
1719 friend Q::Q(Q
&&) noexcept
; // expected-error {{follows constexpr}}
1720 friend Q::Q(const Q
&) noexcept
; // expected-error {{follows constexpr}}
1721 friend S::S() noexcept
; // expected-error {{follows constexpr}}
1722 friend S::S(S
&&) noexcept
; // expected-error {{follows constexpr}}
1723 friend S::S(const S
&) noexcept
; // expected-error {{follows constexpr}}
1724 friend constexpr U::U() noexcept
; // cxx11_20-error {{follows non-constexpr}}
1725 friend constexpr U::U(U
&&) noexcept
; // cxx11_20-error {{follows non-constexpr}}
1726 friend constexpr U::U(const U
&) noexcept
; // cxx11_20-error {{follows non-constexpr}}
1730 // Indirectly test that an implicit lvalue to xvalue conversion performed for
1731 // an NRVO move operation isn't implemented as CK_LValueToRValue.
1734 constexpr Foo
id(Foo x
) { return x
; }
1735 constexpr Foo
res(id(Foo()));
1748 // S's default constructor isn't constexpr, because U's default constructor
1749 // doesn't initialize 't', but it's trivial, so value-initialization doesn't
1750 // actually call it.
1751 static_assert(S
{}.t
== 0, "");
1756 constexpr S(int a0
) : m(a0
) {}
1757 constexpr S() : m(6) {}
1760 constexpr S x
[3] = { {4}, 5 };
1761 static_assert(x
[0].m
== 4, "");
1762 static_assert(x
[1].m
== 5, "");
1763 static_assert(x
[2].m
== 6, "");
1766 // Indirectly test that an implicit lvalue-to-rvalue conversion is performed
1767 // when a conditional operator has one argument of type void and where the other
1768 // is a glvalue of class type.
1769 namespace ConditionalLValToRVal
{
1771 constexpr A(int a
) : v(a
) {}
1775 constexpr A
f(const A
&a
) {
1776 return a
.v
== 0 ? throw a
: a
;
1780 static_assert(f(a
).v
== 4, "");
1787 constexpr bool b
= &n
== &n
;
1789 constexpr int *p
= &n
; // expected-error{{constexpr variable 'p' must be initialized by a constant expression}}
1791 constexpr int *f() { return &n
; }
1792 constexpr int *q
= f(); // expected-error{{constexpr variable 'q' must be initialized by a constant expression}}
1793 constexpr bool c
= f() == f();
1795 constexpr int *g() { return &m
; }
1796 constexpr int *r
= g();
1800 constexpr void f() { return; } // cxx11-error{{constexpr function's return type 'void' is not a literal type}}
1802 void assert_failed(const char *msg
, const char *file
, int line
); // expected-note {{declared here}}
1803 #define ASSERT(expr) ((expr) ? static_cast<void>(0) : assert_failed(#expr, __FILE__, __LINE__))
1804 template<typename T
, size_t S
>
1805 constexpr T
get(T (&a
)[S
], size_t k
) {
1806 return ASSERT(k
> 0 && k
< S
), a
[k
]; // expected-note{{non-constexpr function 'assert_failed'}}
1809 template int get(int (&a
)[4], size_t);
1810 constexpr int arr
[] = { 4, 1, 2, 3, 4 };
1811 static_assert(get(arr
, 1) == 1, "");
1812 static_assert(get(arr
, 4) == 4, "");
1813 static_assert(get(arr
, 0) == 4, ""); // expected-error{{not an integral constant expression}} \
1814 // expected-note{{in call to 'get<const int, 5UL>(arr, 0)'}}
1817 namespace std
{ struct type_info
; }
1820 struct A
{ virtual ~A(); };
1822 A
&g(); // cxx20_23-note {{declared here}}
1823 constexpr auto &x
= typeid(f());
1824 constexpr auto &y
= typeid(g()); // expected-error{{constant expression}}
1825 // cxx11-note@-1 {{typeid applied to expression of polymorphic type 'A' is not allowed in a constant expression}}
1826 // expected-warning@-2 {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}
1827 // cxx20_23-note@-3 {{non-constexpr function 'g' cannot be used in a constant expression}}
1832 constexpr duration() {}
1833 constexpr operator int() const { return 0; }
1835 // These are valid per P0859R0 (moved as DR).
1836 template<typename T
> void f() {
1837 constexpr duration d
= duration();
1839 int n
= sizeof(short{duration(duration())});
1842 namespace ArrayEltInit
{
1844 constexpr A() : p(&p
) {}
1848 static_assert(a
[0].p
== &a
[0].p
, "");
1849 static_assert(a
[9].p
== &a
[9].p
, "");
1850 static_assert(a
[0].p
!= &a
[9].p
, "");
1851 static_assert(a
[9].p
!= &a
[0].p
, "");
1853 constexpr A b
[10] = {};
1854 static_assert(b
[0].p
== &b
[0].p
, "");
1855 static_assert(b
[9].p
== &b
[9].p
, "");
1856 static_assert(b
[0].p
!= &b
[9].p
, "");
1857 static_assert(b
[9].p
!= &b
[0].p
, "");
1862 constexpr S
f() { return {}; }
1863 constexpr S
*p
= &f();
1864 // expected-error@-1 {{taking the address of a temporary}}
1865 // expected-error@-2 {{constexpr variable 'p' must be initialized by a constant expression}}
1866 // expected-note@-3 {{pointer to temporary is not a constant expression}}
1867 // expected-note@-4 {{temporary created here}}
1870 namespace AfterError
{
1871 constexpr int error() {
1872 return foobar
; // expected-error {{undeclared identifier}}
1874 constexpr int k
= error(); // expected-error {{constexpr variable 'k' must be initialized by a constant expression}}
1878 typedef decltype(sizeof(int)) size_t;
1881 class initializer_list
1886 constexpr initializer_list(const _E
* __b
, size_t __s
)
1892 typedef _E value_type
;
1893 typedef const _E
& reference
;
1894 typedef const _E
& const_reference
;
1895 typedef size_t size_type
;
1897 typedef const _E
* iterator
;
1898 typedef const _E
* const_iterator
;
1900 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
1902 constexpr size_t size() const {return __size_
;}
1903 constexpr const _E
* begin() const {return __begin_
;}
1904 constexpr const _E
* end() const {return __begin_
+ __size_
;}
1908 namespace InitializerList
{
1909 constexpr int sum(const int *b
, const int *e
) {
1910 return b
!= e
? *b
+ sum(b
+1, e
) : 0;
1912 constexpr int sum(std::initializer_list
<int> ints
) {
1913 return sum(ints
.begin(), ints
.end());
1915 static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
1917 static_assert(*std::initializer_list
<int>{1, 2, 3}.begin() == 1, "");
1918 static_assert(std::initializer_list
<int>{1, 2, 3}.begin()[2] == 3, "");
1921 constexpr std::initializer_list
<float> il
= {1.0, 2.0, 3.0};
1922 static_assert(il
.begin()[1] == 2.0, "");
1926 namespace StmtExpr
{
1927 struct A
{ int k
; };
1929 static_assert(({ const int x
= 5; x
* 3; }) == 15, ""); // expected-warning {{extension}}
1930 constexpr auto a
= ({ A(); }); // expected-warning {{extension}}
1932 constexpr int g(int k
) {
1933 return ({ // expected-warning {{extension}}
1938 static_assert(g(123) == 15129, "");
1939 constexpr int h() { // cxx11_20-error {{never produces a constant}}
1940 return ({ // expected-warning {{extension}}
1941 return 0; // cxx11_20-note {{not supported}}
1947 namespace VirtualFromBase
{
1949 virtual int f() const;
1954 template <typename T
> struct X
: T
{
1957 constexpr int f() { return sizeof(T
); } // cxx11-warning {{will not be implicitly 'const' in C++14}}
1960 // Virtual f(), not OK.
1961 constexpr X
<X
<S1
>> xxs1
;
1962 constexpr X
<S1
> *p
= const_cast<X
<X
<S1
>>*>(&xxs1
);
1963 static_assert(p
->f() == sizeof(X
<S1
>), "");
1964 // cxx11-error@-1 {{not an integral constant expression}}
1965 // cxx11-note@-2 {{call to virtual function}}
1966 // cxx20_23-error@-3 {{static assertion failed}}
1967 // cxx20_23-note@-4 {{8 == 16}}
1969 // Non-virtual f(), OK.
1970 constexpr X
<X
<S2
>> xxs2
;
1971 constexpr X
<S2
> *q
= const_cast<X
<X
<S2
>>*>(&xxs2
);
1972 static_assert(q
->f() == sizeof(S2
), ""); // cxx20_23-error {{static assertion failed}} \
1973 // cxx20_23-note {{16 == 8}}
1976 namespace ConstexprConstructorRecovery
{
1984 constexpr X() noexcept
{};
1986 E val
{0}; // cxx11-error {{cannot initialize a member subobject of type 'E' with an rvalue of type 'int'}} cxx11-note {{here}}
1988 // FIXME: We should avoid issuing this follow-on diagnostic.
1989 constexpr X x
{}; // cxx11-error {{constant expression}} cxx11-note {{not initialized}}
1992 namespace Lifetime
{
1994 constexpr int &n
= n
; // expected-error {{constant expression}} expected-note {{use of reference outside its lifetime}} expected-warning {{not yet bound to a value}}
1995 constexpr int m
= m
; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
1998 constexpr int &get(int &&n
) { return n
; }
1999 // cxx23-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
2000 constexpr int &&get_rv(int &&n
) { return static_cast<int&&>(n
); }
2005 constexpr S() : r(get_rv(0)), s(get(0)), t(r
) {} // cxx11_20-note {{read of object outside its lifetime}}
2006 constexpr S(int) : r(get_rv(0)), s(get(0)), t(s
) {} // cxx11_20-note {{read of object outside its lifetime}}
2008 constexpr int k1
= S().t
; // expected-error {{constant expression}} cxx11_20-note {{in call}}
2009 constexpr int k2
= S(0).t
; // expected-error {{constant expression}} cxx11_20-note {{in call}}
2013 constexpr int f() const { return 0; }
2015 constexpr Q
*out_of_lifetime(Q q
) { return &q
; } // expected-warning {{address of stack}}
2016 constexpr int k3
= out_of_lifetime({})->n
; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
2017 constexpr int k4
= out_of_lifetime({})->f(); // expected-error {{constant expression}} expected-note {{member call on object outside its lifetime}}
2019 constexpr int null
= ((Q
*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced null pointer}}
2023 constexpr int pte0
= (&q
)[0].f(); // ok
2024 constexpr int pte1
= (&q
)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
2025 constexpr int pte2
= qa
[2].f(); // ok
2026 constexpr int pte3
= qa
[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
2030 constexpr int cpte0
= (&cq
)[0].f(); // ok
2031 constexpr int cpte1
= (&cq
)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
2032 constexpr int cpte2
= cqa
[2].f(); // ok
2033 constexpr int cpte3
= cqa
[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
2035 // FIXME: There's no way if we can tell if the first call here is valid; it
2036 // depends on the active union member. Should we reject for that reason?
2042 constexpr U u2
= {0};
2043 constexpr int union_member1
= u1
.q
.f();
2044 constexpr int union_member2
= u2
.q
.f(); // expected-error {{constant expression}} expected-note {{member call on member 'q' of union with active member 'n'}}
2046 struct R
{ // expected-note {{field init}}
2047 struct Inner
{ constexpr int f() const { return 0; } };
2048 int a
= b
.f(); // expected-warning {{uninitialized}} expected-note 2{{member call on object outside its lifetime}}
2051 constexpr R r
; // expected-error {{constant expression}} expected-note {{in call}} expected-note {{implicit default constructor for 'Lifetime::R' first required here}}
2053 constexpr R r
; // expected-error {{constant expression}} expected-note {{in call}}
2057 namespace Bitfields
{
2063 unsigned u2
: 74; // expected-warning {{exceeds the width of its type}}
2064 int n2
: 81; // expected-warning {{exceeds the width of its type}}
2067 constexpr A a
= { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}}
2068 static_assert(a
.b
== 0 && a
.u
== 1 && a
.n
== -1 && a
.b2
== 0 &&
2069 a
.u2
+ 1 == 0 && a
.n2
== 0x7fffffff,
2070 "bad truncation of bitfield values");
2074 constexpr B(int k
) : n(k
) {}
2076 static_assert(B(3).n
== 3, "");
2077 static_assert(B(4).n
== -4, "");
2078 static_assert(B(7).n
== -1, "");
2079 static_assert(B(8).n
== 0, "");
2080 static_assert(B(-1).n
== -1, "");
2081 static_assert(B(-8889).n
== -1, "");
2086 constexpr static int f(int x
) {
2090 static_assert(X::f(3) == -1, "3 should truncate to -1");
2091 static_assert(X::f(1) == -1, "1 should truncate to -1");
2094 struct HasUnnamedBitfield
{
2099 constexpr HasUnnamedBitfield() : a(), b() {}
2100 constexpr HasUnnamedBitfield(unsigned a
, unsigned b
) : a(a
), b(b
) {}
2103 void testUnnamedBitfield() {
2104 const HasUnnamedBitfield zero
{};
2105 int a
= 1 / zero
.b
; // expected-warning {{division by zero is undefined}}
2106 const HasUnnamedBitfield oneZero
{1, 0};
2107 int b
= 1 / oneZero
.b
; // expected-warning {{division by zero is undefined}}
2110 union UnionWithUnnamedBitfield
{
2114 static_assert(UnionWithUnnamedBitfield().n
== 0, "");
2115 static_assert(UnionWithUnnamedBitfield
{}.n
== 0, "");
2116 static_assert(UnionWithUnnamedBitfield
{1}.n
== 1, "");
2119 namespace ZeroSizeTypes
{
2120 constexpr int (*p1
)[0] = 0, (*p2
)[0] = 0;
2121 constexpr int k
= p2
- p1
;
2122 // expected-error@-1 {{constexpr variable 'k' must be initialized by a constant expression}}
2123 // expected-note@-2 {{subtraction of pointers to type 'int[0]' of zero size}}
2126 constexpr int f() { // cxx11_20-error {{never produces a constant expression}}
2127 return &arr
[3] - &arr
[0]; // cxx11_20-note {{subtraction of pointers to type 'int[0]' of zero size}}
2131 namespace BadDefaultInit
{
2132 template<int N
> struct X
{ static const int n
= N
; };
2134 struct A
{ // expected-error {{default member initializer for 'k' needed within definition of enclosing class}}
2135 int k
= // expected-note {{default member initializer declared here}}
2136 X
<A().k
>::n
; // expected-note {{in evaluation of exception specification for 'BadDefaultInit::A::A' needed here}}
2141 int k
= X
<B().k
>::n
) : // expected-error {{default argument to function 'B' that is declared later}} expected-note {{here}}
2147 namespace NeverConstantTwoWays
{
2148 // If we see something non-constant but foldable followed by something
2149 // non-constant and not foldable, we want the first diagnostic, not the
2151 constexpr int f(int n
) { // cxx11_20-error {{never produces a constant expression}}
2152 return (int *)(long)&n
== &n
? // cxx11_20-note {{reinterpret_cast}}
2153 1 / 0 : // expected-warning {{division by zero}}
2157 constexpr int n
= // expected-error {{must be initialized by a constant expression}}
2158 (int *)(long)&n
== &n
? // expected-note {{reinterpret_cast}}
2165 constexpr int operator()() const { return 0; }
2167 template <typename
...T
> constexpr int sink(T
...) {
2170 template <int ...N
> constexpr int run() {
2171 return sink(A()() + N
...);
2173 constexpr int k
= run
<1, 2, 3>();
2176 namespace BuiltinStrlen
{
2177 constexpr const char *a
= "foo\0quux";
2178 constexpr char b
[] = "foo\0quux";
2179 constexpr int f() { return 'u'; }
2180 constexpr char c
[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 };
2182 static_assert(__builtin_strlen("foo") == 3, "");
2183 static_assert(__builtin_strlen("foo\0quux") == 3, "");
2184 static_assert(__builtin_strlen("foo\0quux" + 4) == 4, "");
2186 constexpr bool check(const char *p
) {
2187 return __builtin_strlen(p
) == 3 &&
2188 __builtin_strlen(p
+ 1) == 2 &&
2189 __builtin_strlen(p
+ 2) == 1 &&
2190 __builtin_strlen(p
+ 3) == 0 &&
2191 __builtin_strlen(p
+ 4) == 4 &&
2192 __builtin_strlen(p
+ 5) == 3 &&
2193 __builtin_strlen(p
+ 6) == 2 &&
2194 __builtin_strlen(p
+ 7) == 1 &&
2195 __builtin_strlen(p
+ 8) == 0;
2198 static_assert(check(a
), "");
2199 static_assert(check(b
), "");
2200 static_assert(check(c
), "");
2202 constexpr int over1
= __builtin_strlen(a
+ 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2203 constexpr int over2
= __builtin_strlen(b
+ 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2204 constexpr int over3
= __builtin_strlen(c
+ 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2206 constexpr int under1
= __builtin_strlen(a
- 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2207 constexpr int under2
= __builtin_strlen(b
- 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2208 constexpr int under3
= __builtin_strlen(c
- 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2210 // FIXME: The diagnostic here could be better.
2211 constexpr char d
[] = { 'f', 'o', 'o' }; // no nul terminator.
2212 constexpr int bad
= __builtin_strlen(d
); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2217 struct Empty2
: Empty
{};
2218 struct Test
: Empty2
{
2222 void test() { constexpr Test t
; }
2225 void PR21327(int a
, int b
) {
2226 static_assert(&a
+ 1 != &b
, ""); // expected-error {{constant expression}}
2227 // expected-note@-1 {{comparison against pointer '&a + 1' that points past the end of a complete object has unspecified value}}
2230 namespace EmptyClass
{
2232 union E2
{} e2
; // expected-note {{here}}
2233 struct E3
: E1
{} e3
;
2235 // The defaulted copy constructor for an empty class does not read any
2236 // members. The defaulted copy constructor for an empty union reads the
2237 // object representation.
2238 constexpr E1
e1b(e1
);
2239 constexpr E2
e2b(e2
); // expected-error {{constant expression}} expected-note{{read of non-const}} expected-note {{in call}}
2240 constexpr E3
e3b(e3
);
2244 extern void (*start
[])();
2245 extern void (*end
[])();
2246 static_assert(&start
!= &end
, ""); // expected-error {{constant expression}}
2247 // expected-note@-1 {{comparison of pointers '&start' and '&end' to unrelated zero-sized objects}}
2248 static_assert(&start
!= nullptr, "");
2255 static_assert(&Bar::x
!= nullptr, "");
2256 static_assert(&Bar::x
!= &Bar::y
, "");
2260 constexpr int Fun() { return; } // expected-error {{non-void constexpr function 'Fun' should return a value}}
2261 constexpr int Var
= Fun();
2263 template <typename T
> constexpr int FunT1() { return; } // expected-error {{non-void constexpr function 'FunT1' should return a value}}
2264 template <typename T
> constexpr int FunT2() { return 0; }
2265 template <> constexpr int FunT2
<double>() { return 0; }
2266 template <> constexpr int FunT2
<int>() { return; } // expected-error {{non-void constexpr function 'FunT2<int>' should return a value}}
2269 struct InvalidRedef
{
2270 int f
; // expected-note{{previous definition is here}}
2271 constexpr int f(void); // expected-error{{redefinition of 'f'}} cxx11-warning{{will not be implicitly 'const'}}
2275 template <typename T
> constexpr T
const &f(T
const &x
) { return x
; }
2279 struct Z
: Y
{ constexpr Z() {} };
2281 static constexpr auto z
= f(Z());
2287 constexpr A() : x(0), p(&x
) {}
2288 constexpr A(const A
&a
) : x(a
.x
), p(&x
) {}
2290 constexpr A
f() { return A(); }
2291 constexpr A
g() { return f(); }
2292 constexpr int a
= *f().p
;
2293 constexpr int b
= *g().p
;
2296 namespace IncompleteClass
{
2298 static constexpr int f(XX
*) { return 1; } // expected-note {{here}}
2299 friend constexpr int g(XX
*) { return 2; } // expected-note {{here}}
2301 static constexpr int i
= f(static_cast<XX
*>(nullptr)); // expected-error {{constexpr variable 'i' must be initialized by a constant expression}} expected-note {{undefined function 'f' cannot be used in a constant expression}}
2302 static constexpr int j
= g(static_cast<XX
*>(nullptr)); // expected-error {{constexpr variable 'j' must be initialized by a constant expression}} expected-note {{undefined function 'g' cannot be used in a constant expression}}
2306 namespace InheritedCtor
{
2307 struct A
{ constexpr A(int) {} };
2309 struct B
: A
{ int n
; using A::A
; }; // expected-note {{here}}
2310 constexpr B
b(0); // expected-error {{constant expression}} cxx11_20-note {{derived class}}\
2311 // cxx23-note {{not initialized}}
2313 struct C
: A
{ using A::A
; struct { union { int n
, m
= 0; }; union { int a
= 0; }; int k
= 0; }; struct {}; union {}; }; // expected-warning 6{{}}
2317 using A::A
; // cxx11-note {{here}}
2318 struct { // expected-warning {{extension}}
2319 union { // expected-warning {{extension}}
2324 constexpr D
d(0); // cxx11-error {{constant expression}} cxx11-note {{derived class}}
2326 struct E
: virtual A
{ using A::A
; }; // expected-note {{here}}
2327 // cxx20_23-note@-1 {{struct with virtual base class is not a literal type}}
2328 // We wrap a function around this to avoid implicit zero-initialization
2329 // happening first; the zero-initialization step would produce the same
2330 // error and defeat the point of this test.
2332 constexpr E
e(0); // cxx11-error {{constant expression}} cxx11-note {{derived class}}
2333 // cxx20_23-error@-1 {{constexpr variable cannot have non-literal type}}
2335 // FIXME: This produces a note with no source location.
2338 struct W
{ constexpr W(int n
) : w(n
) {} int w
; };
2339 struct X
: W
{ using W::W
; int x
= 2; };
2340 struct Y
: X
{ using X::X
; int y
= 3; };
2341 struct Z
: Y
{ using Y::Y
; int z
= 4; };
2343 static_assert(z
.w
== 1 && z
.x
== 2 && z
.y
== 3 && z
.z
== 4, "");
2350 void f(char c
) { //expected-note{{declared here}}
2351 //cxx11_20-note@-1{{declared here}}
2353 static constexpr char f() { // cxx11_20-error {{never produces a constant expression}}
2354 return c
; //expected-error{{reference to local}} cxx11_20-note{{function parameter}}
2362 static const int d
= 'd';
2364 static constexpr int f() {
2368 static_assert(X::f() == 'c' + 'd',"");
2376 namespace PointerArithmeticOverflow
{
2379 constexpr int *b
= &n
+ 1 + (long)-1;
2380 constexpr int *c
= &n
+ 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}}
2381 constexpr int *d
= &n
+ 1 - (unsigned long)1;
2382 constexpr int *e
= a
+ 1 + (long)-1;
2383 constexpr int *f
= a
+ 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}}
2384 constexpr int *g
= a
+ 1 - (unsigned long)1;
2386 constexpr int *p
= (&n
+ 1) + (unsigned __int128
)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}}
2387 constexpr int *q
= (&n
+ 1) - (unsigned __int128
)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element -3402}}
2388 constexpr int *r
= &(&n
+ 1)[(unsigned __int128
)-1]; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}}
2393 char c
[10] = "asdf";
2394 constexpr char foo() const { return c
[3]; }
2396 static_assert(S().foo() == 'f', "");
2400 struct e
{ operator int(); };
2403 f
&d
= reinterpret_cast<f
&>(a
);
2407 namespace array_size
{
2408 template<int N
> struct array
{
2409 static constexpr int size() { return N
; }
2411 template<typename T
> void f1(T t
) {
2412 constexpr int k
= t
.size();
2414 template<typename T
> void f2(const T
&t
) { // expected-note 2{{declared here}}
2415 constexpr int k
= t
.size(); // expected-error 2{{constant}} expected-note 2{{function parameter 't' with unknown value cannot be used in a constant expression}}
2417 template<typename T
> void f3(const T
&t
) {
2418 constexpr int k
= T::size();
2420 void g(array
<3> a
) {
2422 f2(a
); // expected-note {{instantiation of}}
2426 template<int N
> struct array_nonstatic
{
2427 constexpr int size() const { return N
; }
2429 void h(array_nonstatic
<3> a
) {
2431 f2(a
); // expected-note {{instantiation of}}
2435 namespace flexible_array
{
2436 struct A
{ int x
; char arr
[]; }; // expected-warning {{C99}} expected-note {{here}}
2437 constexpr A a
= {1};
2438 static_assert(a
.x
== 1, "");
2439 static_assert(&a
.arr
!= nullptr, "");
2440 static_assert(a
.arr
[0], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2441 static_assert(a
.arr
[1], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2443 constexpr A b
[] = {{1}, {2}, {3}}; // expected-warning {{flexible array member}}
2444 static_assert(b
[0].x
== 1, "");
2445 static_assert(b
[1].x
== 2, "");
2446 static_assert(b
[2].x
== 3, "");
2447 static_assert(b
[2].arr
[0], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2449 // Flexible array initialization is currently not supported by constant
2450 // evaluation. Make sure we emit an error message, for now.
2451 constexpr A c
= {1, 2, 3}; // expected-error {{constexpr variable 'c' must be initialized by a constant expression}}
2452 // expected-note@-1 {{flexible array initialization is not yet supported}}
2453 // expected-warning@-2 {{flexible array initialization is a GNU extension}}
2456 void local_constexpr_var() {
2457 constexpr int a
= 0; // expected-note {{address of non-static constexpr variable 'a' may differ on each invocation of the enclosing function; add 'static' to give it a constant address}}
2458 constexpr const int *p
= &a
; // expected-error {{constant expression}} expected-note {{pointer to 'a' is not a constant expression}}
2462 // Enums without fixed underlying type
2463 enum E1
{e11
=-4, e12
=4};
2464 enum E2
{e21
=0, e22
=4};
2465 enum E3
{e31
=-4, e32
=1024};
2467 // Empty but as-if it had a single enumerator with value 0
2470 // Enum with fixed underlying type because the underlying type is explicitly specified
2471 enum EFixed
: int {efixed1
=-4, efixed2
=4};
2472 // Enum with fixed underlying type because it is scoped
2473 enum class EScoped
{escoped1
=-4, escoped2
=4};
2475 enum EMaxInt
{emaxint1
=-1, emaxint2
=__INT_MAX__
};
2479 E2
testDefaultArgForParam(E2 e2Param
= (E2
)-1) { // ok, not a constant expression context
2480 E2 e2LocalInit
= e2Param
; // ok, not a constant expression context
2484 #include <enum-constexpr-conversion-system-header.h>
2486 void testValueInRangeOfEnumerationValues() {
2487 constexpr E1 x1
= static_cast<E1
>(-8);
2488 constexpr E1 x2
= static_cast<E1
>(8);
2489 // expected-error@-1 {{constexpr variable 'x2' must be initialized by a constant expression}}
2490 // expected-note@-2 {{integer value 8 is outside the valid range of values [-8, 7] for the enumeration type 'E1'}}
2491 E1 x2b
= static_cast<E1
>(8); // ok, not a constant expression context
2493 constexpr E2 x3
= static_cast<E2
>(-8);
2494 // expected-error@-1 {{constexpr variable 'x3' must be initialized by a constant expression}}
2495 // expected-note@-2 {{integer value -8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
2496 constexpr E2 x4
= static_cast<E2
>(0);
2497 constexpr E2 x5
= static_cast<E2
>(8);
2498 // expected-error@-1 {{constexpr variable 'x5' must be initialized by a constant expression}}
2499 // expected-note@-2 {{integer value 8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
2501 constexpr E3 x6
= static_cast<E3
>(-2048);
2502 constexpr E3 x7
= static_cast<E3
>(-8);
2503 constexpr E3 x8
= static_cast<E3
>(0);
2504 constexpr E3 x9
= static_cast<E3
>(8);
2505 constexpr E3 x10
= static_cast<E3
>(2048);
2506 // expected-error@-1 {{constexpr variable 'x10' must be initialized by a constant expression}}
2507 // expected-note@-2 {{integer value 2048 is outside the valid range of values [-2048, 2047] for the enumeration type 'E3'}}
2509 constexpr E4 x11
= static_cast<E4
>(0);
2510 constexpr E4 x12
= static_cast<E4
>(1);
2511 constexpr E4 x13
= static_cast<E4
>(2);
2512 // expected-error@-1 {{constexpr variable 'x13' must be initialized by a constant expression}}
2513 // expected-note@-2 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'E4'}}
2515 constexpr EEmpty x14
= static_cast<EEmpty
>(0);
2516 constexpr EEmpty x15
= static_cast<EEmpty
>(1);
2517 constexpr EEmpty x16
= static_cast<EEmpty
>(2);
2518 // expected-error@-1 {{constexpr variable 'x16' must be initialized by a constant expression}}
2519 // expected-note@-2 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'EEmpty'}}
2521 constexpr EFixed x17
= static_cast<EFixed
>(100);
2522 constexpr EScoped x18
= static_cast<EScoped
>(100);
2524 constexpr EMaxInt x19
= static_cast<EMaxInt
>(__INT_MAX__
-1);
2525 constexpr EMaxInt x20
= static_cast<EMaxInt
>((long)__INT_MAX__
+1);
2526 // expected-error@-1 {{constexpr variable 'x20' must be initialized by a constant expression}}
2527 // expected-note@-2 {{integer value 2147483648 is outside the valid range of values [-2147483648, 2147483647] for the enumeration type 'EMaxInt'}}
2529 const NumberType neg_one
= (NumberType
) ((NumberType
) 0 - (NumberType
) 1); // ok, not a constant expression context
2531 CONSTEXPR_CAST_TO_SYSTEM_ENUM_OUTSIDE_OF_RANGE
;
2532 // expected-error@-1 {{constexpr variable 'system_enum' must be initialized by a constant expression}}
2533 // expected-note@-2 {{integer value 123 is outside the valid range of values [0, 1] for the enumeration type 'SystemEnum'}}
2536 template<class T
, unsigned size
> struct Bitfield
{
2537 static constexpr T max
= static_cast<T
>((1 << size
) - 1);
2538 // cxx11-error@-1 {{constexpr variable 'max' must be initialized by a constant expression}}
2539 // cxx11-note@-2 {{integer value 15 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
2542 void testValueInRangeOfEnumerationValuesViaTemplate() {
2543 Bitfield
<E2
, 3> good
;
2544 Bitfield
<E2
, 4> bad
; // cxx11-note {{in instantiation}}
2553 static void f(SortOrder order
);
2556 void A::f(SortOrder order
) {
2557 if (order
== SortOrder(-1)) // ok, not a constant expression context
2562 GH50055::E2 GlobalInitNotCE1
= (GH50055::E2
)-1; // ok, not a constant expression context
2563 GH50055::E2 GlobalInitNotCE2
= GH50055::testDefaultArgForParam(); // ok, not a constant expression context
2564 constexpr GH50055::E2 GlobalInitCE
= (GH50055::E2
)-1;
2565 // expected-error@-1 {{constexpr variable 'GlobalInitCE' must be initialized by a constant expression}}
2566 // expected-note@-2 {{integer value -1 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
2568 namespace GH112140
{
2570 constexpr S(const int &a
= ) { } // expected-error {{expected expression}}
2574 constexpr S s
[2] = { }; // expected-error {{constexpr variable 's' must be initialized by a constant expression}}