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 -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 -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 namespace StaticAssertFoldTest
{
8 static_assert(++x
, "test"); // expected-error {{not an integral constant expression}}
9 // cxx20_23-note@-1 {{cannot modify an object that is visible outside that expression}}
10 static_assert(false, "test"); // expected-error {{test}}
14 int array
[(long)(char *)0]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
15 // expected-warning {{variable length array folded to constant array as an extension}} \
16 // expected-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
18 typedef decltype(sizeof(char)) size_t;
20 template<typename T
> constexpr T
id(const T
&t
) { return t
; }
21 template<typename T
> constexpr T
min(const T
&a
, const T
&b
) {
24 template<typename T
> constexpr T
max(const T
&a
, const T
&b
) {
27 template<typename T
, size_t N
> constexpr T
*begin(T (&xs
)[N
]) { return xs
; }
28 template<typename T
, size_t N
> constexpr T
*end(T (&xs
)[N
]) { return xs
+ N
; }
31 constexpr int zero() const { return 0; }
34 constexpr int arr
[]; // expected-error {{constexpr variable 'arr' must be initialized by a constant expression}}
35 constexpr int arr2
[2]; // expected-error {{constexpr variable 'arr2' must be initialized by a constant expression}}
36 constexpr int arr3
[2] = {};
38 namespace DerivedToVBaseCast
{
41 struct V
: U
{ int n
; };
42 struct A
: virtual V
{ int n
; };
44 struct B
: virtual A
, Aa
{};
45 struct C
: virtual A
, Aa
{};
52 static_assert((void*)p
!= (void*)q
, "");
53 static_assert((A
*)p
== (A
*)q
, "");
54 static_assert((Aa
*)p
!= (Aa
*)q
, "");
58 static_assert((void*)&pp
!= (void*)&qq
, "");
59 static_assert(&(A
&)pp
== &(A
&)qq
, "");
60 static_assert(&(Aa
&)pp
!= &(Aa
&)qq
, "");
64 constexpr V
*x
= (A
*)p
;
65 static_assert(v
== w
, "");
66 static_assert(v
== x
, "");
68 static_assert((U
*)&d
== p
, "");
69 static_assert((U
*)&d
== q
, "");
70 static_assert((U
*)&d
== v
, "");
71 static_assert((U
*)&d
== w
, "");
72 static_assert((U
*)&d
== x
, "");
75 struct Y1
: virtual X
{};
79 static_assert((X
*)(Y1
*)&z
!= (X
*)(Y2
*)&z
, "");
85 constexpr int n2
= const_cast<int&>(n1
);
86 constexpr int *n3
= const_cast<int*>(&n1
);
87 constexpr int n4
= *const_cast<int*>(&n1
);
88 constexpr const int * const *n5
= const_cast<const int* const*>(&n3
);
89 constexpr int **n6
= const_cast<int**>(&n3
);
90 constexpr int n7
= **n5
;
91 constexpr int n8
= **n6
;
93 // const_cast from prvalue to xvalue.
95 constexpr int n9
= (const_cast<A
&&>(A
{123})).n
;
96 static_assert(n9
== 123, "");
100 namespace TemplateArgumentConversion
{
101 template<int n
> struct IntParam
{};
103 using IntParam0
= IntParam
<0>;
104 using IntParam0
= IntParam
<id(0)>;
105 using IntParam0
= IntParam
<MemberZero().zero
>; // expected-error {{did you mean to call it with no arguments?}}
108 namespace CaseStatements
{
112 case MemberZero().zero
: // expected-error {{did you mean to call it with no arguments?}} expected-note {{previous}}
113 case id(0): // expected-error {{duplicate case value '0'}}
115 case __builtin_constant_p(true) ? (__SIZE_TYPE__
)&x
: 0:; // expected-error {{constant}}
120 extern int &Recurse1
;
121 int &Recurse2
= Recurse1
; // expected-note {{declared here}}
122 int &Recurse1
= Recurse2
;
123 constexpr int &Recurse3
= Recurse2
; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'Recurse2' is not a constant expression}}
125 extern const int RecurseA
;
126 const int RecurseB
= RecurseA
; // expected-note {{declared here}}
127 const int RecurseA
= 10;
128 constexpr int RecurseC
= RecurseB
; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'RecurseB' is not a constant expression}}
130 namespace MemberEnum
{
131 struct WithMemberEnum
{
135 static_assert(wme
.A
== 42, "");
138 namespace DefaultArguments
{
141 constexpr int Sum(int a
= 0, const int &b
= 0, const int *c
= &z
, char d
= 0) {
142 return a
+ b
+ *c
+ d
;
145 constexpr int eight
= 8;
146 constexpr const int twentyseven
= 27;
147 static_assert(Sum() == 0, "");
148 static_assert(Sum(1) == 1, "");
149 static_assert(Sum(1, four
) == 5, "");
150 static_assert(Sum(1, eight
, &twentyseven
) == 36, "");
151 static_assert(Sum(1, 2, &four
, eight
) == 15, "");
157 // Note, values passed through an ellipsis can't actually be used.
158 constexpr int F(int a
, ...) { return a
; }
159 static_assert(F(0) == 0, "");
160 static_assert(F(1, 0) == 1, "");
161 static_assert(F(2, "test") == 2, "");
162 static_assert(F(3, &F
) == 3, "");
163 int k
= 0; // expected-note {{here}}
164 static_assert(F(4, k
) == 3, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'k'}}
168 namespace Recursion
{
169 constexpr int fib(int n
) { return n
> 1 ? fib(n
-1) + fib(n
-2) : n
; }
170 static_assert(fib(11) == 89, "");
172 constexpr int gcd_inner(int a
, int b
) {
173 return b
== 0 ? a
: gcd_inner(b
, a
% b
);
175 constexpr int gcd(int a
, int b
) {
176 return gcd_inner(max(a
, b
), min(a
, b
));
179 static_assert(gcd(1749237, 5628959) == 7, "");
182 namespace FunctionCast
{
183 // When folding, we allow functions to be cast to different types. Such
184 // cast functions cannot be called, even if they're constexpr.
185 constexpr int f() { return 1; }
186 typedef double (*DoubleFn
)();
187 typedef int (*IntFn
)();
188 int a
[(int)DoubleFn(f
)()]; // expected-error {{variable length array}} expected-warning{{Clang extension}}
189 int b
[(int)IntFn(f
)()]; // ok
192 namespace StaticMemberFunction
{
194 static constexpr int k
= 42;
195 static constexpr int f(int n
) { return n
* k
+ 2; }
198 constexpr int n
= s
.f(19);
199 static_assert(S::f(19) == 800, "");
200 static_assert(s
.f(19) == 800, "");
201 static_assert(n
== 800, "");
203 constexpr int (*sf1
)(int) = &S::f
;
204 constexpr int (*sf2
)(int) = &s
.f
;
205 constexpr const int *sk
= &s
.k
;
207 // Note, out_of_lifetime returns an invalid pointer value, but we don't do
208 // anything with it (other than copy it around), so there's no UB there.
209 constexpr S
*out_of_lifetime(S s
) { return &s
; } // expected-warning {{address of stack}}
210 static_assert(out_of_lifetime({})->k
== 42, "");
211 static_assert(out_of_lifetime({})->f(3) == 128, "");
213 // Similarly, using an inactive union member breaks no rules.
219 static_assert(u
.s
.k
== 42, "");
220 static_assert(u
.s
.f(1) == 44, "");
222 // And likewise for a past-the-end pointer.
223 static_assert((&s
)[1].k
== 42, "");
224 static_assert((&s
)[1].f(1) == 44, "");
227 namespace ParameterScopes
{
230 constexpr const int &ObscureTheTruth(const int &a
) { return a
; }
231 constexpr const int &MaybeReturnJunk(bool b
, const int a
) {
232 return ObscureTheTruth(b
? a
: k
);
234 static_assert(MaybeReturnJunk(false, 0) == 42, ""); // ok
235 constexpr int a
= MaybeReturnJunk(true, 0); // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
237 constexpr const int MaybeReturnNonstaticRef(bool b
, const int a
) {
238 return ObscureTheTruth(b
? a
: k
);
240 static_assert(MaybeReturnNonstaticRef(false, 0) == 42, ""); // ok
241 constexpr int b
= MaybeReturnNonstaticRef(true, 0); // ok
243 constexpr int InternalReturnJunk(int n
) {
244 return MaybeReturnJunk(true, n
); // expected-note {{read of object outside its lifetime}}
246 constexpr int n3
= InternalReturnJunk(0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'InternalReturnJunk(0)'}}
248 constexpr int LToR(int &n
) { return n
; }
249 constexpr int GrabCallersArgument(bool which
, int a
, int b
) {
250 return LToR(which
? b
: a
);
252 static_assert(GrabCallersArgument(false, 1, 2) == 1, "");
253 static_assert(GrabCallersArgument(true, 4, 8) == 8, "");
259 constexpr int f(int n
, const int *a
, const int *b
, const int *c
) {
260 return n
== 0 ? 0 : *a
+ f(n
-1, b
, c
, a
);
263 const int x
= 1, y
= 10, z
= 100;
264 static_assert(f(23, &x
, &y
, &z
) == 788, "");
266 constexpr int g(int n
, int a
, int b
, int c
) {
267 return f(n
, &a
, &b
, &c
);
269 static_assert(g(23, x
, y
, z
) == 788, "");
273 namespace FunctionPointers
{
275 constexpr int Double(int n
) { return 2 * n
; }
276 constexpr int Triple(int n
) { return 3 * n
; }
277 constexpr int Twice(int (*F
)(int), int n
) { return F(F(n
)); }
278 constexpr int Quadruple(int n
) { return Twice(Double
, n
); }
279 constexpr auto Select(int n
) -> int (*)(int) {
280 return n
== 2 ? &Double
: n
== 3 ? &Triple
: n
== 4 ? &Quadruple
: 0;
282 constexpr int Apply(int (*F
)(int), int n
) { return F(n
); } // expected-note {{'F' evaluates to a null function pointer}}
284 static_assert(1 + Apply(Select(4), 5) + Apply(Select(3), 7) == 42, "");
286 constexpr int Invalid
= Apply(Select(0), 0); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'Apply(nullptr, 0)'}}
290 namespace PointerComparison
{
293 static_assert(&x
== &y
, "false"); // expected-error {{false}}
294 static_assert(&x
!= &y
, "");
295 constexpr bool g1
= &x
== &y
;
296 constexpr bool g2
= &x
!= &y
;
297 constexpr bool g3
= &x
<= &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
298 constexpr bool g4
= &x
>= &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
299 constexpr bool g5
= &x
< &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
300 constexpr bool g6
= &x
> &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
302 struct S
{ int x
, y
; } s
;
303 static_assert(&s
.x
== &s
.y
, "false"); // expected-error {{false}}
304 static_assert(&s
.x
!= &s
.y
, "");
305 static_assert(&s
.x
<= &s
.y
, "");
306 static_assert(&s
.x
>= &s
.y
, "false"); // expected-error {{false}}
307 static_assert(&s
.x
< &s
.y
, "");
308 static_assert(&s
.x
> &s
.y
, "false"); // expected-error {{false}}
310 static_assert(0 == &y
, "false"); // expected-error {{false}}
311 static_assert(0 != &y
, "");
312 constexpr bool n3
= (int*)0 <= &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
313 constexpr bool n4
= (int*)0 >= &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
314 constexpr bool n5
= (int*)0 < &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
315 constexpr bool n6
= (int*)0 > &y
; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
317 static_assert(&x
== 0, "false"); // expected-error {{false}}
318 static_assert(&x
!= 0, "");
319 constexpr bool n9
= &x
<= (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
320 constexpr bool n10
= &x
>= (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
321 constexpr bool n11
= &x
< (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
322 constexpr bool n12
= &x
> (int*)0; // expected-error {{must be initialized by a constant expression}} expected-note {{unspecified}}
324 static_assert(&x
== &x
, "");
325 static_assert(&x
!= &x
, "false"); // expected-error {{false}}
326 static_assert(&x
<= &x
, "");
327 static_assert(&x
>= &x
, "");
328 static_assert(&x
< &x
, "false"); // expected-error {{false}}
329 static_assert(&x
> &x
, "false"); // expected-error {{false}}
331 constexpr S
* sptr
= &s
;
332 constexpr bool dyncast
= sptr
== dynamic_cast<S
*>(sptr
); // cxx11-error {{constant expression}} cxx11-note {{dynamic_cast}}
336 int a
: dynamic_cast<S
*>(sptr
) == dynamic_cast<S
*>(sptr
); // \
337 cxx11
-warning
{{not an integral constant expression
}} \
338 cxx11
-note
{{dynamic_cast is
not allowed in a constant expression
}}
339 int b
: reinterpret_cast<S
*>(sptr
) == reinterpret_cast<S
*>(sptr
); // \
340 expected
-warning
{{not an integral constant expression
}} \
341 expected
-note
{{reinterpret_cast is
not allowed in a constant expression
}}
342 int c
: (S
*)(long)(sptr
) == (S
*)(long)(sptr
); // \
343 expected
-warning
{{not an integral constant expression
}} \
344 expected
-note
{{cast that performs the conversions of a
reinterpret_cast is
not allowed in a constant expression
}}
345 int d
: (S
*)(42) == (S
*)(42); // \
346 expected
-warning
{{not an integral constant expression
}} \
347 expected
-note
{{cast that performs the conversions of a
reinterpret_cast is
not allowed in a constant expression
}}
348 int e
: (Str
*)(sptr
) == (Str
*)(sptr
); // \
349 expected
-warning
{{not an integral constant expression
}} \
350 expected
-note
{{cast that performs the conversions of a
reinterpret_cast is
not allowed in a constant expression
}}
351 int f
: &(U
&)(*sptr
) == &(U
&)(*sptr
); // \
352 expected
-warning
{{not an integral constant expression
}} \
353 expected
-note
{{cast that performs the conversions of a
reinterpret_cast is
not allowed in a constant expression
}}
354 int g
: (S
*)(void*)(sptr
) == sptr
; // \
355 expected
-warning
{{not an integral constant expression
}} \
356 expected
-note
{{cast from
'void *' is
not allowed in a constant expression
}}
359 extern char externalvar
[];
360 constexpr bool constaddress
= (void *)externalvar
== (void *)0x4000UL
; // expected-error {{must be initialized by a constant expression}} expected-note {{reinterpret_cast}}
361 constexpr bool litaddress
= "foo" == "foo"; // expected-error {{must be initialized by a constant expression}}
362 // expected-note@-1 {{comparison of addresses of literals has unspecified value}}
363 // cxx20_23-warning@-2 {{comparison between two arrays is deprecated}}
364 static_assert(0 != "foo", "");
368 namespace MaterializeTemporary
{
370 constexpr int f(const int &r
) { return r
; }
371 constexpr int n
= f(1);
373 constexpr bool same(const int &a
, const int &b
) { return &a
== &b
; }
374 constexpr bool sameTemporary(const int &n
) { return same(n
, n
); }
376 static_assert(n
, "");
377 static_assert(!same(4, 4), "");
378 static_assert(same(n
, n
), "");
379 static_assert(sameTemporary(9), "");
381 struct A
{ int &&r
; };
382 struct B
{ A
&&a1
; A
&&a2
; };
384 constexpr B b1
{ { 1 }, { 2 } }; // expected-note {{temporary created here}}
385 static_assert(&b1
.a1
!= &b1
.a2
, "");
386 static_assert(&b1
.a1
.r
!= &b1
.a2
.r
, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
388 constexpr B
&&b2
{ { 3 }, { 4 } }; // expected-note {{temporary created here}}
389 static_assert(&b1
!= &b2
, "");
390 static_assert(&b1
.a1
!= &b2
.a1
, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
392 constexpr thread_local B b3
{ { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
394 constexpr static B b1
{ { 1 }, { 2 } }; // ok
395 constexpr thread_local B b2
{ { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
396 constexpr B b3
{ { 1 }, { 2 } }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
399 constexpr B
&&b4
= ((1, 2), 3, 4, B
{ {10}, {{20}} });
400 static_assert(&b4
!= &b2
, "");
402 // Proposed DR: copy-elision doesn't trigger lifetime extension.
403 // 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}}
404 constexpr B b5
= B
{ {0}, {0} }; // expected-error {{constant expression}} expected-note {{reference to temporary}} expected-note {{here}}
406 namespace NestedNonStatic
{
407 // Proposed DR: for a reference constant expression to refer to a static
408 // storage duration temporary, that temporary must itself be initialized
409 // by a constant expression (a core constant expression is not enough).
410 struct A
{ int &&r
; };
412 constexpr B a
= { A
{0} }; // ok
413 // cxx11-warning@+1 {{temporary bound to reference member of local variable 'b' will be destroyed at the end of the full-expression}}
414 constexpr B b
= { A(A
{0}) }; // cxx11-error {{constant expression}} cxx11-note {{reference to temporary}} cxx11-note {{here}}
417 namespace FakeInitList
{
418 struct init_list_3_ints
{ const int (&x
)[3]; };
419 struct init_list_2_init_list_3_ints
{ const init_list_3_ints (&x
)[2]; };
420 constexpr init_list_2_init_list_3_ints ils
= { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } };
423 namespace ConstAddedByReference
{
427 int &&r2
= 0; // expected-note {{created here}}
428 constexpr int n2
= r2
; // expected-error {{constant}} expected-note {{read of temporary}}
430 struct A
{ constexpr operator int() const { return 0; }};
431 struct B
{ constexpr operator const int() const { return 0; }};
434 constexpr int na
= ra
;
435 constexpr int nb
= rb
;
437 struct C
{ int &&r
; };
438 constexpr C c1
= {1};
439 constexpr int &c1r
= c1
.r
;
440 constexpr const C
&c2
= {2};
441 constexpr int &c2r
= c2
.r
;
442 constexpr C
&&c3
= {3}; // expected-note {{created here}}
443 constexpr int &c3r
= c3
.r
; // expected-error {{constant}} expected-note {{read of temporary}}
448 constexpr int strcmp_ce(const char *p
, const char *q
) {
449 return (!*p
|| *p
!= *q
) ? *p
- *q
: strcmp_ce(p
+1, q
+1);
452 namespace StringLiteral
{
454 template<typename Char
>
455 constexpr int MangleChars(const Char
*p
) {
456 return *p
+ 3 * (*p
? MangleChars(p
+1) : 0);
459 static_assert(MangleChars("constexpr!") == 1768383, "");
460 static_assert(MangleChars(u8
"constexpr!") == 1768383, "");
461 static_assert(MangleChars(L
"constexpr!") == 1768383, "");
462 static_assert(MangleChars(u
"constexpr!") == 1768383, "");
463 static_assert(MangleChars(U
"constexpr!") == 1768383, "");
465 constexpr char c0
= "nought index"[0];
466 constexpr char c1
= "nice index"[10];
467 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}}
468 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}}
469 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}}
471 constexpr const char *p
= "test" + 2;
472 static_assert(*p
== 's', "");
474 constexpr const char *max_iter(const char *a
, const char *b
) {
475 return *a
< *b
? b
: a
;
477 constexpr const char *max_element(const char *a
, const char *b
) {
478 return (a
+1 >= b
) ? a
: max_iter(a
, max_element(a
+1, b
));
481 constexpr char str
[] = "the quick brown fox jumped over the lazy dog";
482 constexpr const char *max
= max_element(begin(str
), end(str
));
483 static_assert(*max
== 'z', "");
484 static_assert(max
== str
+ 38, "");
486 static_assert(strcmp_ce("hello world", "hello world") == 0, "");
487 static_assert(strcmp_ce("hello world", "hello clang") > 0, "");
488 static_assert(strcmp_ce("constexpr", "test") < 0, "");
489 static_assert(strcmp_ce("", " ") < 0, "");
492 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}}
497 constexpr T() : c
{"foo"} {}
501 static_assert(t
.c
[0] == 'f', "");
502 static_assert(t
.c
[1] == 'o', "");
503 static_assert(t
.c
[2] == 'o', "");
504 static_assert(t
.c
[3] == 0, "");
505 static_assert(t
.c
[4] == 0, "");
506 static_assert(t
.c
[5] == 0, "");
507 static_assert(t
.c
[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
512 } constexpr u
= { { L
"test" }, 0 };
513 static_assert(u
.chars
[2] == L
's', "");
517 constexpr V() : c("hi!") {}
519 static_assert(V().c
[1] == "i"[0], "");
522 constexpr unsigned char a
[] = ("foo"), b
[] = {"foo"}, c
[] = {("foo")},
523 d
[4] = ("foo"), e
[5] = {"foo"}, f
[6] = {("foo")};
524 static_assert(a
[0] == 'f', "");
525 static_assert(b
[1] == 'o', "");
526 static_assert(c
[2] == 'o', "");
527 static_assert(d
[0] == 'f', "");
528 static_assert(e
[1] == 'o', "");
529 static_assert(f
[2] == 'o', "");
530 static_assert(f
[5] == 0, "");
531 static_assert(f
[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
538 template<typename Iter
>
539 constexpr auto Sum(Iter begin
, Iter end
) -> decltype(+*begin
) {
540 return begin
== end
? 0 : *begin
+ Sum(begin
+1, end
);
543 constexpr int xs
[] = { 1, 2, 3, 4, 5 };
544 constexpr int ys
[] = { 5, 4, 3, 2, 1 };
545 constexpr int sum_xs
= Sum(begin(xs
), end(xs
));
546 static_assert(sum_xs
== 15, "");
548 constexpr int ZipFoldR(int (*F
)(int x
, int y
, int c
), int n
,
549 const int *xs
, const int *ys
, int c
) {
551 *xs
, // expected-note {{read of dereferenced one-past-the-end pointer}}
553 ZipFoldR(F
, n
-1, xs
+1, ys
+1, c
)) // \
554 expected
-note
{{in call to
'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
555 expected
-note
{{in call to
'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
558 constexpr int MulAdd(int x
, int y
, int c
) { return x
* y
+ c
; }
559 constexpr int InnerProduct
= ZipFoldR(MulAdd
, 5, xs
, ys
, 0);
560 static_assert(InnerProduct
== 35, "");
562 constexpr int SubMul(int x
, int y
, int c
) { return (x
- y
) * c
; }
563 constexpr int DiffProd
= ZipFoldR(SubMul
, 2, xs
+3, ys
+3, 1);
564 static_assert(DiffProd
== 8, "");
565 static_assert(ZipFoldR(SubMul
, 3, xs
+3, ys
+3, 1), ""); // \
566 expected
-error
{{constant expression
}} \
567 expected
-note
{{in call to
'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
569 constexpr const int *p
= xs
+ 3;
570 constexpr int xs4
= p
[1]; // ok
571 constexpr int xs5
= p
[2]; // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
572 constexpr int xs6
= p
[3]; // expected-error {{constant expression}} expected-note {{cannot refer to element 6}}
573 constexpr int xs0
= p
[-3]; // ok
574 constexpr int xs_1
= p
[-4]; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
576 constexpr int zs
[2][2][2][2] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
577 static_assert(zs
[0][0][0][0] == 1, "");
578 static_assert(zs
[1][1][1][1] == 16, "");
579 static_assert(zs
[0][0][0][2] == 3, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
580 static_assert((&zs
[0][0][0][2])[-1] == 2, "");
581 static_assert(**(**(zs
+ 1) + 1) == 11, "");
582 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}}
583 static_assert(*(&(&(*(*&(&zs
[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2) == 11, "");
584 constexpr int err_zs_1_2_0_0
= zs
[1][2][0][0]; // \
585 expected
-error
{{constant expression
}} \
586 expected
-note
{{cannot access array element of pointer past the end
}}
588 constexpr int fail(const int &p
) {
589 return (&p
)[64]; // expected-note {{cannot refer to element 64 of array of 2 elements}}
591 static_assert(fail(*(&(&(*(*&(&zs
[2] - 1)[0] + 2 - 2))[2])[-1][2] - 2)) == 11, ""); // \
592 expected
-error
{{static assertion expression is
not an integral constant expression
}} \
593 expected
-note
{{in call to
'fail(zs[1][0][1][0])'}}
595 constexpr int arr
[40] = { 1, 2, 3, [8] = 4 };
596 constexpr int SumNonzero(const int *p
) {
597 return *p
+ (*p
? SumNonzero(p
+1) : 0);
599 constexpr int CountZero(const int *p
, const int *q
) {
600 return p
== q
? 0 : (*p
== 0) + CountZero(p
+1, q
);
602 static_assert(SumNonzero(arr
) == 6, "");
603 static_assert(CountZero(arr
, arr
+ 40) == 36, "");
606 constexpr ArrayElem() : n(0) {}
608 constexpr int f() const { return n
; }
611 constexpr ArrayRVal() {}
614 static_assert(ArrayRVal().elems
[3].f() == 0, "");
619 constexpr A(const A
&) {}
625 constexpr B b
{{}, {1, 2, 3, 4, 5}};
627 static_assert(c
.arr
[2] == 3, "");
628 static_assert(c
.arr
[7] == 0, "");
630 // OK: the copy ctor for X doesn't read any members.
631 struct X
{ struct Y
{} y
; } x1
;
635 constexpr int selfref
[2][2][2] = {
636 1, selfref
[0][0][0] + 1,
637 1, selfref
[0][1][0] + 1,
638 1, selfref
[0][1][1] + 1 };
639 static_assert(selfref
[0][0][0] == 1, "");
640 static_assert(selfref
[0][0][1] == 2, "");
641 static_assert(selfref
[0][1][0] == 1, "");
642 static_assert(selfref
[0][1][1] == 2, "");
643 static_assert(selfref
[1][0][0] == 1, "");
644 static_assert(selfref
[1][0][1] == 3, "");
645 static_assert(selfref
[1][1][0] == 0, "");
646 static_assert(selfref
[1][1][1] == 0, "");
648 constexpr int badselfref
[2][2][2] = { // expected-error {{constant expression}}
649 badselfref
[1][0][0] // expected-note {{outside its lifetime}}
652 struct TrivialDefCtor
{ int n
; };
653 typedef TrivialDefCtor TDCArray
[2][2];
654 static_assert(TDCArray
{}[1][1].n
== 0, "");
656 struct NonAggregateTDC
: TrivialDefCtor
{};
657 typedef NonAggregateTDC NATDCArray
[2][2];
658 static_assert(NATDCArray
{}[1][1].n
== 0, "");
662 // Per current CWG direction, we reject any cases where pointer arithmetic is
663 // not statically known to be valid.
664 namespace ArrayOfUnknownBound
{
666 constexpr int *a
= arr
;
667 constexpr int *b
= &arr
[0];
668 static_assert(a
== b
, "");
669 constexpr int *c
= &arr
[1]; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
670 constexpr int *d
= &a
[1]; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
671 constexpr int *e
= a
+ 1; // expected-error {{constant}} expected-note {{indexing of array without known bound}}
675 int b
[]; // expected-warning {{C99}}
678 constexpr int *xb
= x
.b
; // expected-error {{constant}} expected-note {{not supported}}
682 constexpr Y
*p
= yarr
;
683 constexpr int *q
= &p
->a
;
685 extern const int carr
[]; // expected-note {{here}}
686 constexpr int n
= carr
[0]; // expected-error {{constant}} expected-note {{non-constexpr variable}}
688 constexpr int local_extern
[] = {1, 2, 3};
689 void f() { extern const int local_extern
[]; }
690 static_assert(local_extern
[1] == 2, "");
693 namespace DependentValues
{
695 struct I
{ int n
; typedef I V
[10]; };
697 int g(); // expected-note {{declared here}}
698 template<bool B
, typename T
> struct S
: T
{
701 I::V
&cells
= B
? x
: y
;
705 constexpr int n
= g(); // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr function 'g'}}
707 constexpr int m
= this->g(); // ok, could be constexpr
711 extern const int n
; // expected-note {{declared here}}
712 template<typename T
> void f() {
713 // This is ill-formed, because a hypothetical instantiation at the point of
714 // template definition would be ill-formed due to a construct that does not
715 // depend on a template parameter.
716 constexpr int k
= n
; // expected-error {{must be initialized by a constant expression}} expected-note {{initializer of 'n' is unknown}}
718 // It doesn't matter that the instantiation could later become valid:
720 template void f
<int>();
726 struct A
{ constexpr A(int a
, int b
) : k(a
+ b
) {} int k
; };
727 constexpr int fn(const A
&a
) { return a
.k
; }
728 static_assert(fn(A(4,5)) == 9, "");
730 struct B
{ int n
; int m
; } constexpr b
= { 0, b
.n
};
732 constexpr C(C
*this_
) : m(42), n(this_
->m
) {} // ok
737 constexpr D() : c(&c
) {}
739 static_assert(D().c
.n
== 42, "");
742 constexpr E() : p(&p
) {}
745 constexpr const E
&e1
= E();
746 // This is a constant expression if we elide the copy constructor call, and
747 // is not a constant expression if we don't! But we do, so it is.
748 constexpr E e2
= E();
749 static_assert(e2
.p
== &e2
.p
, "");
751 static_assert(e3
.p
== &e3
.p
, "");
753 extern const class F f
;
755 constexpr F() : p(&f
.p
) {}
762 constexpr T(T
*p
) : u1(), u2(p
) {}
768 constexpr U2(T
*p
) : c(p
->u1
.b
) {}
772 constexpr G() : t(&t
) {}
775 static_assert(g
.t
.u1
.a
== 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'a' of union with active member 'b'}}
776 static_assert(g
.t
.u1
.b
== 42, "");
777 static_assert(g
.t
.u2
.c
== 42, "");
778 static_assert(g
.t
.u2
.d
== 42, ""); // expected-error {{constant expression}} expected-note {{read of member 'd' of union with active member 'c'}}
786 constexpr S(int n
, const S
*p
) : a(5), b(n
), p(p
), d(n
), q("hello") {}
789 S
global(43, &global
);
791 static_assert(S(15, &global
).b
== 15, "");
793 constexpr bool CheckS(const S
&s
) {
794 return s
.a
== 5 && s
.b
== 27 && s
.p
== &global
&& s
.d
== 27. && s
.q
[3] == 'l';
796 static_assert(CheckS(S(27, &global
)), "");
800 constexpr Arr() : arr
{'x', 'y', 'z'} {}
802 constexpr int hash(Arr
&&a
) {
803 return a
.arr
[0] + a
.arr
[1] * 0x100 + a
.arr
[2] * 0x10000;
805 constexpr int k
= hash(Arr());
806 static_assert(k
== 0x007a7978, "");
809 struct AggregateInit
{
817 constexpr AggregateInit agg1
= { "hello"[0] };
819 static_assert(strcmp_ce(&agg1
.c
, "hello") == 0, "");
820 static_assert(agg1
.n
== 0, "");
821 static_assert(agg1
.d
== 0.0, "");
822 static_assert(agg1
.arr
[-1] == 0, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
823 static_assert(agg1
.arr
[0] == 0, "");
824 static_assert(agg1
.arr
[4] == 0, "");
825 static_assert(agg1
.arr
[5] == 0, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end}}
826 static_assert(agg1
.p
== nullptr, "");
828 static constexpr const unsigned char uc
[] = { "foo" };
829 static_assert(uc
[0] == 'f', "");
830 static_assert(uc
[3] == 0, "");
832 namespace SimpleDerivedClass
{
835 constexpr B(int n
) : a(n
) {}
839 constexpr D(int n
) : B(n
) {}
842 static_assert(d
.a
== 3, "");
846 struct Bottom
{ constexpr Bottom() {} };
847 struct Base
: Bottom
{
848 constexpr Base(int a
= 42, const char *b
= "test") : a(a
), b(b
) {}
852 struct Base2
: Bottom
{
853 constexpr Base2(const int &r
) : r(r
) {}
857 struct Derived
: Base
, Base2
{
858 constexpr Derived() : Base(76), Base2(a
) {}
862 constexpr bool operator==(const Base
&a
, const Base
&b
) {
863 return a
.a
== b
.a
&& strcmp_ce(a
.b
, b
.b
) == 0;
867 constexpr Base
base2(76);
868 constexpr Derived derived
;
869 static_assert(derived
.a
== 76, "");
870 static_assert(derived
.b
[2] == 's', "");
871 static_assert(derived
.c
== 76 + 'e', "");
872 static_assert(derived
.q
== 123, "");
873 static_assert(derived
.r
== 76, "");
874 static_assert(&derived
.r
== &derived
.a
, "");
876 static_assert(!(derived
== base
), "");
877 static_assert(derived
== base2
, "");
879 constexpr Bottom
&bot1
= (Base
&)derived
;
880 constexpr Bottom
&bot2
= (Base2
&)derived
;
881 static_assert(&bot1
!= &bot2
, "");
883 constexpr Bottom
*pb1
= (Base
*)&derived
;
884 constexpr Bottom
*pb2
= (Base2
*)&derived
;
885 static_assert(&pb1
!= &pb2
, "");
886 static_assert(pb1
== &bot1
, "");
887 static_assert(pb2
== &bot2
, "");
889 constexpr Base2
&fail
= (Base2
&)bot1
; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base2'}}
890 constexpr Base
&fail2
= (Base
&)*pb2
; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base'}}
891 constexpr Base2
&ok2
= (Base2
&)bot2
;
892 static_assert(&ok2
== &derived
, "");
894 constexpr Base2
*pfail
= (Base2
*)pb1
; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base2'}}
895 constexpr Base
*pfail2
= (Base
*)&bot2
; // expected-error {{constant expression}} expected-note {{cannot cast object of dynamic type 'const Derived' to type 'Base'}}
896 constexpr Base2
*pok2
= (Base2
*)pb2
;
897 static_assert(pok2
== &derived
, "");
898 static_assert(&ok2
== pok2
, "");
899 static_assert((Base2
*)(Derived
*)(Base
*)pb1
== pok2
, "");
900 static_assert((Derived
*)(Base
*)pb1
== (Derived
*)pok2
, "");
902 // Core issue 903: we do not perform constant evaluation when checking for a
903 // null pointer in C++11. Just check for an integer literal with value 0.
904 constexpr Base
*nullB
= 42 - 6 * 7; // expected-error {{cannot initialize a variable of type 'Base *const' with an rvalue of type 'int'}}
905 constexpr Base
*nullB1
= 0;
906 static_assert((Bottom
*)nullB
== 0, "");
907 static_assert((Derived
*)nullB1
== 0, "");
908 static_assert((void*)(Bottom
*)nullB1
== (void*)(Derived
*)nullB1
, "");
909 Base
*nullB2
= '\0'; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'char'}}
911 Base
*nullB4
= false; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'bool'}}
912 Base
*nullB5
= ((0ULL));
913 Base
*nullB6
= 0.; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'double'}}
915 Base
*nullB7
= kNull
; // expected-error {{cannot initialize a variable of type 'Base *' with an rvalue of type 'Class::Null'}}
916 static_assert(nullB1
== (1 - 1), ""); // expected-error {{comparison between pointer and integer}}
920 namespace ConversionOperators
{
923 constexpr T(int n
) : k(5*n
- 3) {}
924 constexpr operator int() const { return k
; }
929 constexpr S(int n
) : k(2*n
+ 1) {}
930 constexpr operator int() const { return k
; }
931 constexpr operator T() const { return T(k
); }
935 constexpr bool check(T a
, T b
) { return a
== b
.k
; }
937 static_assert(S(5) == 11, "");
938 static_assert(check(S(5), 11), "");
943 constexpr (operator int)() const { return 0; }
945 static_assert(X() == 0, "");
952 constexpr int f() const { return 0; }
953 static constexpr int g() { return 0; }
955 constexpr int x
= f(); // expected-error {{must be initialized by a constant}}
956 // expected-note@-1 {{implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
957 constexpr int y
= this->f(); // expected-error {{must be initialized by a constant}}
958 // expected-note-re@-1 {{{{^}}use of 'this' pointer}}
959 constexpr int z
= g();
960 static_assert(z
== 0, "");
966 namespace Temporaries
{
970 constexpr int f() const;
971 constexpr int g() const;
974 constexpr T(int n
) : S(), n(n
) {}
977 constexpr int S::f() const {
978 return static_cast<const T
*>(this)->n
; // expected-note 5{{cannot cast}}
980 constexpr int S::g() const {
981 // FIXME: Better diagnostic for this.
982 return this->*(int(S::*))&T::n
; // expected-note {{subexpression}}
984 // The T temporary is implicitly cast to an S subobject, but we can recover the
985 // T full-object via a base-to-derived cast, or a derived-to-base-casted member
987 static_assert(S().f(), ""); // expected-error {{constant expression}} expected-note {{in call to 'S().f()'}}
988 static_assert(S().g(), ""); // expected-error {{constant expression}} expected-note {{in call to 'S().g()'}}
990 constexpr const S
& slref
= sobj
;
991 constexpr const S
&& srref
= S();
992 constexpr const S
*sptr
= &sobj
;
993 static_assert(sobj
.f(), ""); // expected-error {{constant expression}} \
994 expected
-note
{{in call to
'sobj.f()'}}
995 static_assert(sptr
->f(), ""); // expected-error {{constant expression}} \
996 expected
-note
{{in call to
'sptr->f()'}}
997 static_assert(slref
.f(), ""); // expected-error {{constant expression}} \
998 expected
-note
{{in call to
'slref.f()'}}
999 static_assert(srref
.f(), ""); // expected-error {{constant expression}} \
1000 expected
-note
{{in call to
'srref.f()'}}
1001 static_assert(T(3).f() == 3, "");
1002 static_assert(T(4).g() == 4, "");
1004 constexpr int f(const S
&s
) {
1005 return static_cast<const T
&>(s
).n
;
1007 constexpr int n
= f(T(5));
1008 static_assert(f(T(5)) == 5, "");
1010 constexpr bool b(int n
) { return &n
; }
1011 static_assert(b(0), "");
1017 constexpr int k
= NonLiteral().f(); // expected-error {{constant expression}} expected-note {{non-literal type 'NonLiteral'}}
1028 constexpr U u
[4] = { { .a
= 0 }, { .b
= 1 }, { .a
= 2 }, { .b
= 3 } };
1029 static_assert(u
[0].a
== 0, "");
1030 static_assert(u
[0].b
, ""); // expected-error {{constant expression}} expected-note {{read of member 'b' of union with active member 'a'}}
1031 static_assert(u
[1].b
== 1, "");
1032 static_assert((&u
[1].b
)[1] == 2, ""); // expected-error {{constant expression}} expected-note {{read of dereferenced one-past-the-end pointer}}
1033 static_assert(*(&(u
[1].b
) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}}
1034 static_assert((&(u
[1]) + 1 + 1)->b
== 3, "");
1037 static_assert(v
.a
== 0, "");
1040 constexpr Empty e
= {};
1042 // Make sure we handle trivial copy constructors for unions.
1043 constexpr U x
= {42};
1045 static_assert(y
.a
== 42, "");
1046 static_assert(y
.b
== 42, ""); // expected-error {{constant expression}} expected-note {{'b' of union with active member 'a'}}
1050 namespace MemberPointer
{
1052 constexpr A(int n
) : n(n
) {}
1054 constexpr int f() const { return n
+ 3; }
1057 static_assert(A(5).*&A::n
== 5, "");
1058 static_assert((&a
)->*&A::n
== 7, "");
1059 static_assert((A(8).*&A::f
)() == 11, "");
1060 static_assert(((&a
)->*&A::f
)() == 10, "");
1063 constexpr B(int n
, int m
) : A(n
), m(m
) {}
1065 constexpr int g() const { return n
+ m
+ 1; }
1067 constexpr B
b(9, 13);
1068 static_assert(B(4, 11).*&A::n
== 4, "");
1069 static_assert(B(4, 11).*&B::m
== 11, "");
1070 static_assert(B(4, 11).*(int(A::*))&B::m
== 11, "");
1071 static_assert((&b
)->*&A::n
== 9, "");
1072 static_assert((&b
)->*&B::m
== 13, "");
1073 static_assert((&b
)->*(int(A::*))&B::m
== 13, "");
1074 static_assert((B(4, 11).*&A::f
)() == 7, "");
1075 static_assert((B(4, 11).*&B::g
)() == 16, "");
1076 static_assert((B(4, 11).*(int(A::*)()const)&B::g
)() == 16, "");
1077 static_assert(((&b
)->*&A::f
)() == 12, "");
1078 static_assert(((&b
)->*&B::g
)() == 23, "");
1079 static_assert(((&b
)->*(int(A::*)()const)&B::g
)() == 23, "");
1082 constexpr S(int m
, int n
, int (S::*pf
)() const, int S::*pn
) :
1083 m(m
), n(n
), pf(pf
), pn(pn
) {}
1084 constexpr S() : m(), n(), pf(&S::f
), pn(&S::n
) {}
1086 constexpr int f() const { return this->*pn
; }
1087 virtual int g() const;
1090 int (S::*pf
)() const;
1094 constexpr int S::*pm
= &S::m
;
1095 constexpr int S::*pn
= &S::n
;
1096 constexpr int (S::*pf
)() const = &S::f
;
1097 constexpr int (S::*pg
)() const = &S::g
;
1099 constexpr S
s(2, 5, &S::f
, &S::m
);
1101 static_assert((s
.*&S::f
)() == 2, "");
1102 static_assert((s
.*s
.pf
)() == 2, "");
1104 static_assert(pf
== &S::f
, "");
1105 static_assert(pf
== s
.*&S::pf
, "");
1106 static_assert(pm
== &S::m
, "");
1107 static_assert(pm
!= pn
, "");
1108 static_assert(s
.pn
!= pn
, "");
1109 static_assert(s
.pn
== pm
, "");
1110 static_assert(pg
!= nullptr, "");
1111 static_assert(pf
!= nullptr, "");
1112 static_assert((int S::*)nullptr == nullptr, "");
1113 static_assert(pg
== pg
, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
1114 static_assert(pf
!= pg
, ""); // expected-error {{constant expression}} expected-note {{comparison of pointer to virtual member function 'g' has unspecified value}}
1116 template<int n
> struct T
: T
<n
-1> {};
1117 template<> struct T
<0> { int n
; };
1118 template<> struct T
<30> : T
<29> { int m
; };
1123 constexpr int (T
<10>::*deepn
) = &T
<0>::n
;
1124 static_assert(&(t17
.*deepn
) == &t17
.n
, "");
1125 static_assert(deepn
== &T
<2>::n
, "");
1127 constexpr int (T
<15>::*deepm
) = (int(T
<10>::*))&T
<30>::m
;
1128 constexpr int *pbad
= &(t17
.*deepm
); // expected-error {{constant expression}}
1129 static_assert(&(t30
.*deepm
) == &t30
.m
, "");
1130 static_assert(deepm
== &T
<50>::m
, "");
1131 static_assert(deepm
!= deepn
, "");
1133 constexpr T
<5> *p17_5
= &t17
;
1134 constexpr T
<13> *p17_13
= (T
<13>*)p17_5
;
1135 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>'}}
1136 static_assert(&(p17_5
->*(int(T
<3>::*))deepn
) == &t17
.n
, "");
1137 static_assert(&(p17_13
->*deepn
) == &t17
.n
, "");
1138 constexpr int *pbad2
= &(p17_13
->*(int(T
<9>::*))deepm
); // expected-error {{constant expression}}
1140 constexpr T
<5> *p30_5
= &t30
;
1141 constexpr T
<23> *p30_23
= (T
<23>*)p30_5
;
1142 constexpr T
<13> *p30_13
= p30_23
;
1143 static_assert(&(p30_5
->*(int(T
<3>::*))deepn
) == &t30
.n
, "");
1144 static_assert(&(p30_13
->*deepn
) == &t30
.n
, "");
1145 static_assert(&(p30_23
->*deepn
) == &t30
.n
, "");
1146 static_assert(&(p30_5
->*(int(T
<2>::*))deepm
) == &t30
.m
, "");
1147 static_assert(&(((T
<17>*)p30_13
)->*deepm
) == &t30
.m
, "");
1148 static_assert(&(p30_23
->*deepm
) == &t30
.m
, "");
1150 struct Base
{ int n
; };
1151 template<int N
> struct Mid
: Base
{};
1152 struct Derived
: Mid
<0>, Mid
<1> {};
1153 static_assert(&Mid
<0>::n
== &Mid
<1>::n
, "");
1154 static_assert((int Derived::*)(int Mid
<0>::*)&Mid
<0>::n
!=
1155 (int Derived::*)(int Mid
<1>::*)&Mid
<1>::n
, "");
1156 static_assert(&Mid
<0>::n
== (int Mid
<0>::*)&Base::n
, "");
1158 constexpr int apply(const A
&a
, int (A::*f
)() const) {
1161 static_assert(apply(A(2), &A::f
) == 5, "");
1164 namespace ArrayBaseDerived
{
1170 struct Derived
: Base
{
1171 constexpr Derived() {}
1172 constexpr const int *f() const { return &n
; }
1175 constexpr Derived a
[10];
1176 constexpr Derived
*pd3
= const_cast<Derived
*>(&a
[3]);
1177 constexpr Base
*pb3
= const_cast<Derived
*>(&a
[3]);
1178 static_assert(pb3
== pd3
, "");
1180 // pb3 does not point to an array element.
1181 constexpr Base
*pb4
= pb3
+ 1; // ok, one-past-the-end pointer.
1182 constexpr int pb4n
= pb4
->n
; // expected-error {{constant expression}} expected-note {{cannot access field of pointer past the end}}
1183 constexpr Base
*err_pb5
= pb3
+ 2; // expected-error {{constant expression}} expected-note {{cannot refer to element 2}} expected-note {{here}}
1184 constexpr int err_pb5n
= err_pb5
->n
; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb5' is not a constant expression}}
1185 constexpr Base
*err_pb2
= pb3
- 1; // expected-error {{constant expression}} expected-note {{cannot refer to element -1}} expected-note {{here}}
1186 constexpr int err_pb2n
= err_pb2
->n
; // expected-error {{constant expression}} expected-note {{initializer of 'err_pb2'}}
1187 constexpr Base
*pb3a
= pb4
- 1;
1189 // pb4 does not point to a Derived.
1190 constexpr Derived
*err_pd4
= (Derived
*)pb4
; // expected-error {{constant expression}} expected-note {{cannot access derived class of pointer past the end}}
1191 constexpr Derived
*pd3a
= (Derived
*)pb3a
;
1192 constexpr int pd3n
= pd3a
->n
;
1194 // pd3a still points to the Derived array.
1195 constexpr Derived
*pd6
= pd3a
+ 3;
1196 static_assert(pd6
== &a
[6], "");
1197 constexpr Derived
*pd9
= pd6
+ 3;
1198 constexpr Derived
*pd10
= pd6
+ 4;
1199 constexpr int pd9n
= pd9
->n
; // ok
1200 constexpr int err_pd10n
= pd10
->n
; // expected-error {{constant expression}} expected-note {{cannot access base class of pointer past the end}}
1201 constexpr int pd0n
= pd10
[-10].n
;
1202 constexpr int err_pdminus1n
= pd10
[-11].n
; // expected-error {{constant expression}} expected-note {{cannot refer to element -1 of}}
1204 constexpr Base
*pb9
= pd9
;
1205 constexpr const int *(Base::*pfb
)() const =
1206 static_cast<const int *(Base::*)() const>(&Derived::f
);
1207 static_assert((pb9
->*pfb
)() == &a
[9].n
, "");
1215 constexpr complex(int re
= 0, int im
= 0) : re(re
), im(im
) {}
1216 constexpr complex(const complex &o
) : re(o
.re
), im(o
.im
) {}
1217 constexpr complex operator-() const { return complex(-re
, -im
); }
1218 friend constexpr complex operator+(const complex &l
, const complex &r
) {
1219 return complex(l
.re
+ r
.re
, l
.im
+ r
.im
);
1221 friend constexpr complex operator-(const complex &l
, const complex &r
) {
1224 friend constexpr complex operator*(const complex &l
, const complex &r
) {
1225 return complex(l
.re
* r
.re
- l
.im
* r
.im
, l
.re
* r
.im
+ l
.im
* r
.re
);
1227 friend constexpr bool operator==(const complex &l
, const complex &r
) {
1228 return l
.re
== r
.re
&& l
.im
== r
.im
;
1230 constexpr bool operator!=(const complex &r
) const {
1231 return re
!= r
.re
|| im
!= r
.im
;
1233 constexpr int real() const { return re
; }
1234 constexpr int imag() const { return im
; }
1237 constexpr complex i
= complex(0, 1);
1238 constexpr complex k
= (3 + 4*i
) * (6 - 4*i
);
1239 static_assert(complex(1,0).real() == 1, "");
1240 static_assert(complex(1,0).imag() == 0, "");
1241 static_assert(((complex)1).imag() == 0, "");
1242 static_assert(k
.real() == 34, "");
1243 static_assert(k
.imag() == 12, "");
1244 static_assert(k
- 34 == 12*i
, "");
1245 static_assert((complex)1 == complex(1), "");
1246 static_assert((complex)1 != complex(0, 1), "");
1247 static_assert(complex(1) == complex(1), "");
1248 static_assert(complex(1) != complex(0, 1), "");
1249 constexpr complex makeComplex(int re
, int im
) { return complex(re
, im
); }
1250 static_assert(makeComplex(1,0) == complex(1), "");
1251 static_assert(makeComplex(1,0) != complex(0, 1), "");
1253 class complex_wrap
: public complex {
1255 constexpr complex_wrap(int re
, int im
= 0) : complex(re
, im
) {}
1256 constexpr complex_wrap(const complex_wrap
&o
) : complex(o
) {}
1259 static_assert((complex_wrap
)1 == complex(1), "");
1260 static_assert((complex)1 != complex_wrap(0, 1), "");
1261 static_assert(complex(1) == complex_wrap(1), "");
1262 static_assert(complex_wrap(1) != complex(0, 1), "");
1263 constexpr complex_wrap
makeComplexWrap(int re
, int im
) {
1264 return complex_wrap(re
, im
);
1266 static_assert(makeComplexWrap(1,0) == complex(1), "");
1267 static_assert(makeComplexWrap(1,0) != complex(0, 1), "");
1272 struct A
{ constexpr bool operator==(int x
) const { return true; } };
1273 struct B
{ B(); A
& x
; };
1274 static_assert(B().x
== 3, ""); // expected-error {{constant expression}} expected-note {{non-literal type 'B' cannot be used in a constant expression}}
1276 constexpr bool f(int k
) { // expected-error {{constexpr function never produces a constant expression}}
1277 return B().x
== k
; // expected-note {{non-literal type 'B' cannot be used in a constant expression}}
1281 namespace ExprWithCleanups
{
1282 struct A
{ A(); ~A(); int get(); };
1283 constexpr int get(bool FromA
) { return FromA
? A().get() : 1; }
1284 constexpr int n
= get(false);
1287 namespace Volatile
{
1289 volatile constexpr int n1
= 0; // expected-note {{here}}
1290 volatile const int n2
= 0; // expected-note {{here}}
1291 int n3
= 37; // expected-note {{declared here}}
1293 constexpr int m1
= n1
; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1294 constexpr int m2
= n2
; // expected-error {{constant expression}} expected-note {{read of volatile-qualified type 'const volatile int'}}
1295 constexpr int m1b
= const_cast<const int&>(n1
); // expected-error {{constant expression}} expected-note {{read of volatile object 'n1'}}
1296 constexpr int m2b
= const_cast<const int&>(n2
); // expected-error {{constant expression}} expected-note {{read of volatile object 'n2'}}
1298 struct T
{ int n
; };
1299 const T t
= { 42 }; // expected-note {{declared here}}
1301 constexpr int f(volatile int &&r
) {
1302 return r
; // expected-note {{read of volatile-qualified type 'volatile int'}}
1304 constexpr int g(volatile int &&r
) {
1305 return const_cast<int&>(r
); // expected-note {{read of volatile temporary is not allowed in a constant expression}}
1308 int j
: f(0); // expected-error {{constant expression}} expected-note {{in call to 'f(0)'}}
1309 int k
: g(0); // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'g(0)'}}
1310 int l
: n3
; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
1311 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}}
1316 namespace ExternConstexpr
{
1317 extern constexpr int n
= 0;
1318 extern constexpr int m
; // expected-error {{constexpr variable declaration must be a definition}}
1320 extern constexpr int i
; // expected-error {{constexpr variable declaration must be a definition}}
1321 constexpr int j
= 0;
1322 constexpr int k
; // expected-error {{constexpr variable 'k' must be initialized by a constant expression}}
1326 constexpr int g() { return q
; } // expected-note {{outside its lifetime}}
1327 constexpr int q
= g(); // expected-error {{constant expression}} expected-note {{in call}}
1329 extern int r
; // expected-note {{here}}
1330 constexpr int h() { return r
; } // expected-error {{never produces a constant}} expected-note {{read of non-const}}
1332 struct S
{ int n
; };
1334 constexpr int x() { return s
.n
; } // expected-note {{outside its lifetime}}
1335 constexpr S s
= {x()}; // expected-error {{constant expression}} expected-note {{in call}}
1338 namespace ComplexConstexpr
{
1339 constexpr _Complex
float test1
= {}; // expected-warning {{'_Complex' is a C99 extension}}
1340 constexpr _Complex
float test2
= {1}; // expected-warning {{'_Complex' is a C99 extension}}
1341 constexpr _Complex
double test3
= {1,2}; // expected-warning {{'_Complex' is a C99 extension}}
1342 constexpr _Complex
int test4
= {4}; // expected-warning {{'_Complex' is a C99 extension}}
1343 constexpr _Complex
int test5
= 4; // expected-warning {{'_Complex' is a C99 extension}}
1344 constexpr _Complex
int test6
= {5,6}; // expected-warning {{'_Complex' is a C99 extension}}
1345 typedef _Complex
float fcomplex
; // expected-warning {{'_Complex' is a C99 extension}}
1346 constexpr fcomplex test7
= fcomplex();
1348 constexpr const double &t2r
= __real test3
;
1349 constexpr const double &t2i
= __imag test3
;
1350 static_assert(&t2r
+ 1 == &t2i
, "");
1351 static_assert(t2r
== 1.0, "");
1352 static_assert(t2i
== 2.0, "");
1353 constexpr const double *t2p
= &t2r
;
1354 static_assert(t2p
[-1] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element -1 of array of 2 elements}}
1355 static_assert(t2p
[0] == 1.0, "");
1356 static_assert(t2p
[1] == 2.0, "");
1357 static_assert(t2p
[2] == 0.0, ""); // expected-error {{constant expr}} expected-note {{one-past-the-end pointer}}
1358 static_assert(t2p
[3] == 0.0, ""); // expected-error {{constant expr}} expected-note {{cannot refer to element 3 of array of 2 elements}}
1359 constexpr _Complex
float *p
= 0; // expected-warning {{'_Complex' is a C99 extension}}
1360 constexpr float pr
= __real
*p
; // expected-error {{constant expr}} expected-note {{cannot access real component of null}}
1361 constexpr float pi
= __imag
*p
; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of null}}
1362 constexpr const _Complex
double *q
= &test3
+ 1; // expected-warning {{'_Complex' is a C99 extension}}
1363 constexpr double qr
= __real
*q
; // expected-error {{constant expr}} expected-note {{cannot access real component of pointer past the end}}
1364 constexpr double qi
= __imag
*q
; // expected-error {{constant expr}} expected-note {{cannot access imaginary component of pointer past the end}}
1366 static_assert(__real test6
== 5, "");
1367 static_assert(__imag test6
== 6, "");
1368 static_assert(&__imag test6
== &__real test6
+ 1, "");
1371 // _Atomic(T) is exactly like T for the purposes of constant expression
1374 constexpr _Atomic
int n
= 3; // expected-warning {{'_Atomic' is a C11 extension}}
1376 struct S
{ _Atomic(double) d
; }; // expected-warning {{'_Atomic' is a C11 extension}}
1377 constexpr S s
= { 0.5 };
1378 constexpr double d1
= s
.d
;
1379 constexpr double d2
= n
;
1380 constexpr _Atomic
double d3
= n
; // expected-warning {{'_Atomic' is a C11 extension}}
1382 constexpr _Atomic(int) n2
= d3
; // expected-warning {{'_Atomic' is a C11 extension}}
1383 static_assert(d1
== 0.5, "");
1384 static_assert(d3
== 3.0, "");
1388 _Atomic(int) value
; // expected-warning {{'_Atomic' is a C11 extension}}
1389 constexpr TestVar(int value
) : value(value
) {}
1391 constexpr TestVar testVar
{-1};
1392 static_assert(testVar
.value
== -1, "");
1397 struct B
{ _Atomic(A
) a
; }; // expected-warning {{'_Atomic' is a C11 extension}}
1398 constexpr int n
= (B(), B(), 0);
1400 struct C
{ constexpr C() {} void *self
= this; };
1401 constexpr _Atomic(C
) c
= C(); // expected-warning {{'_Atomic' is a C11 extension}}
1405 namespace InstantiateCaseStmt
{
1406 template<int x
> constexpr int f() { return x
; }
1407 template<int x
> int g(int c
) { switch(c
) { case f
<x
>(): return 1; } return 0; }
1408 int gg(int c
) { return g
<4>(c
); }
1411 namespace ConvertedConstantExpr
{
1413 extern int &n
; // expected-note 2{{declared here}}
1415 constexpr int k
= 4;
1416 int &m
= const_cast<int&>(k
);
1418 // If we have nothing more interesting to say, ensure we don't produce a
1419 // useless note and instead just point to the non-constant subexpression.
1422 en
= n
, // expected-error {{not a constant expression}} expected-note {{initializer of 'n' is unknown}}
1423 eo
= (m
+ // expected-error {{not a constant expression}}
1424 n
// expected-note {{initializer of 'n' is unknown}}
1426 eq
= reinterpret_cast<long>((int*)0) // expected-error {{not a constant expression}} expected-note {{reinterpret_cast}}
1430 namespace IndirectField
{
1432 struct { // expected-warning {{GNU extension}}
1433 union { // expected-warning {{declared in an anonymous struct}}
1434 struct { // expected-warning {{GNU extension}} expected-warning {{declared in an anonymous union}}
1446 constexpr S(int a
, int b
, int d
, int e
) : a(a
), b(b
), d(d
), e(e
) {}
1447 constexpr S(int c
, int d
, int f
) : c(c
), d(d
), f(f
) {}
1450 constexpr S
s1(1, 2, 3, 4);
1451 constexpr S
s2(5, 6, 7);
1453 // FIXME: The diagnostics here do a very poor job of explaining which unnamed
1454 // member is active and which is requested.
1455 static_assert(s1
.a
== 1, "");
1456 static_assert(s1
.b
== 2, "");
1457 static_assert(s1
.c
== 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1458 static_assert(s1
.d
== 3, "");
1459 static_assert(s1
.e
== 4, "");
1460 static_assert(s1
.f
== 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1462 static_assert(s2
.a
== 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1463 static_assert(s2
.b
== 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1464 static_assert(s2
.c
== 5, "");
1465 static_assert(s2
.d
== 6, "");
1466 static_assert(s2
.e
== 0, ""); // expected-error {{constant expression}} expected-note {{union with active member}}
1467 static_assert(s2
.f
== 7, "");
1470 // DR1405: don't allow reading mutable members in constant expressions.
1471 namespace MutableMembers
{
1473 mutable int n
; // expected-note 3{{declared here}}
1474 } constexpr mm
= { 4 };
1475 constexpr int mmn
= mm
.n
; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1476 int x
= (mm
.n
= 1, 3);
1477 constexpr int mmn2
= mm
.n
; // expected-error {{constant expression}} expected-note {{read of mutable member 'n' is not allowed in a constant expression}}
1479 // Here's one reason why allowing this would be a disaster...
1480 template<int n
> struct Id
{ int k
= n
; };
1482 constexpr MM m
= { 0 };
1484 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}}
1487 struct A
{ int n
; };
1488 struct B
{ mutable A a
; }; // expected-note {{here}}
1490 constexpr C c
[3] = {};
1491 constexpr int k
= c
[1].b
.a
.n
; // expected-error {{constant expression}} expected-note {{mutable}}
1493 struct D
{ int x
; mutable int y
; }; // expected-note {{here}}
1494 constexpr D d1
= { 1, 2 };
1496 constexpr D d2
= d1
; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1501 mutable int b
; // expected-note {{here}}
1504 constexpr E e1
= {{1}};
1505 constexpr E e2
= e1
; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1512 struct Y
: X
{ X x
; U u
; };
1516 // This is OK; we don't actually read any mutable state here.
1517 constexpr F f1
= {};
1518 constexpr F f2
= f1
;
1523 mutable U u
; // expected-note {{here}}
1525 constexpr G g1
= {};
1526 constexpr G g2
= g1
; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1527 constexpr G::U gu1
= {};
1528 constexpr G::U gu2
= gu1
;
1531 mutable G::X gx
; // expected-note {{here}}
1533 constexpr H h1
= {};
1534 constexpr H h2
= h1
; // expected-error {{constant}} expected-note {{mutable}} expected-note {{in call}}
1539 // This macro forces its argument to be constant-folded, even if it's not
1540 // otherwise a constant expression.
1541 #define fold(x) (__builtin_constant_p(x) ? (x) : (x))
1543 constexpr int n
= (long)(char*)123; // expected-error {{constant expression}} expected-note {{reinterpret_cast}}
1544 constexpr int m
= fold((long)(char*)123); // ok
1545 static_assert(m
== 123, "");
1553 constexpr const int &f(const int &n
) { return n
; }
1554 constexpr int k1
= f(0); // ok
1559 constexpr const Wrap
&g(const Wrap
&w
) { return w
; }
1560 constexpr int k2
= g({0}).value
; // ok
1562 // The temporary here has static storage duration, so we can bind a constexpr
1564 constexpr const int &i
= 1;
1565 constexpr const int j
= i
;
1566 static_assert(j
== 1, "");
1568 // The temporary here is not const, so it can't be read outside the expression
1569 // in which it was created (per the C++14 rules, which we use to avoid a C++11
1571 constexpr int &&k
= 1; // expected-note {{temporary created here}}
1572 constexpr const int l
= k
; // expected-error {{constant expression}} expected-note {{read of temporary}}
1575 // The temporary here has automatic storage duration, so we can't bind a
1576 // constexpr reference to it.
1577 constexpr const int &i
= 1; // expected-error {{constant expression}} expected-note 2{{temporary}}
1582 namespace RecursiveOpaqueExpr
{
1583 template<typename Iter
>
1584 constexpr auto LastNonzero(Iter p
, Iter q
) -> decltype(+*p
) {
1585 return p
!= q
? (LastNonzero(p
+1, q
) ?: *p
) : 0; // expected-warning {{GNU}}
1588 constexpr int arr1
[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 0 };
1589 static_assert(LastNonzero(begin(arr1
), end(arr1
)) == 4, "");
1591 constexpr int arr2
[] = { 1, 0, 0, 3, 0, 2, 0, 4, 0, 5 };
1592 static_assert(LastNonzero(begin(arr2
), end(arr2
)) == 5, "");
1594 constexpr int arr3
[] = {
1595 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,
1596 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,
1597 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,
1598 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,
1599 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,
1600 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,
1601 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,
1602 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 };
1603 static_assert(LastNonzero(begin(arr3
), end(arr3
)) == 2, "");
1606 namespace VLASizeof
{
1608 void f(int k
) { // expected-note {{here}}
1609 int arr
[k
]; // expected-warning {{Clang extension}} expected-note {{function parameter 'k'}}
1610 constexpr int n
= 1 +
1611 sizeof(arr
) // expected-error {{constant expression}}
1616 namespace CompoundLiteral
{
1617 // Matching GCC, file-scope array compound literals initialized by constants
1618 // are lifetime-extended.
1619 constexpr int *p
= (int*)(int[1]){3}; // expected-warning {{C99}}
1620 static_assert(*p
== 3, ""); // expected-error {{static assertion expression is not an integral constant expression}}
1621 // expected-note@-1 {{subexpression not valid}}
1622 // expected-note@-3 {{declared here}}
1623 static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}}
1624 // expected-error@-1 {{static assertion expression is not an integral constant expression}}
1625 // expected-note@-2 {{subexpression not valid}}
1626 // expected-note@-3 {{declared here}}
1628 // Other kinds are not.
1629 struct X
{ int a
[2]; };
1630 constexpr int *n
= (X
){1, 2}.a
; // expected-warning {{C99}} expected-warning {{temporary}}
1631 // expected-error@-1 {{constant expression}}
1632 // expected-note@-2 {{pointer to subobject of temporary}}
1633 // expected-note@-3 {{temporary created here}}
1636 static constexpr int *p
= (int*)(int[1]){3}; // expected-warning {{C99}} expected-warning {{temporary}}
1637 // expected-error@-1 {{constant expression}}
1638 // expected-note@-2 {{pointer to subobject of temporary}}
1639 // expected-note@-3 {{temporary created here}}
1640 static_assert((int[2]){1, 2}[1] == 2, ""); // expected-warning {{C99}}
1645 typedef int __attribute__((vector_size(16))) VI4
;
1646 constexpr VI4
f(int n
) {
1647 return VI4
{ n
* 3, n
+ 4, n
- 5, n
/ 6 };
1649 constexpr auto v1
= f(10);
1651 typedef double __attribute__((vector_size(32))) VD4
;
1652 constexpr VD4
g(int n
) {
1653 return (VD4
) { n
/ 2.0, n
+ 1.5, n
- 5.4, n
* 0.9 }; // expected-warning {{C99}}
1655 constexpr auto v2
= g(4);
1659 namespace InvalidClasses
{
1661 struct X
; // expected-note {{forward declaration}}
1662 struct Y
{ bool b
; X x
; }; // expected-error {{field has incomplete type}}
1668 namespace NamespaceAlias
{
1670 namespace NS
= NamespaceAlias
; // cxx11-warning {{use of this statement in a constexpr function is a C++14 extension}}
1671 return &NS::f
!= nullptr;
1675 // Constructors can be implicitly constexpr, even for a non-literal type.
1676 namespace ImplicitConstexpr
{
1677 struct Q
{ Q() = default; Q(const Q
&) = default; Q(Q
&&) = default; ~Q(); }; // expected-note 3{{here}}
1678 struct R
{ constexpr R() noexcept
; constexpr R(const R
&) noexcept
; constexpr R(R
&&) noexcept
; ~R() noexcept
; };
1679 struct S
{ R r
; }; // expected-note 3{{here}}
1680 struct T
{ T(const T
&) noexcept
; T(T
&&) noexcept
; ~T() noexcept
; };
1681 struct U
{ T t
; }; // expected-note 3{{here}}
1682 static_assert(!__is_literal_type(Q
), "");
1683 static_assert(!__is_literal_type(R
), "");
1684 static_assert(!__is_literal_type(S
), "");
1685 static_assert(!__is_literal_type(T
), "");
1686 static_assert(!__is_literal_type(U
), "");
1688 friend Q::Q() noexcept
; // expected-error {{follows constexpr}}
1689 friend Q::Q(Q
&&) noexcept
; // expected-error {{follows constexpr}}
1690 friend Q::Q(const Q
&) noexcept
; // expected-error {{follows constexpr}}
1691 friend S::S() noexcept
; // expected-error {{follows constexpr}}
1692 friend S::S(S
&&) noexcept
; // expected-error {{follows constexpr}}
1693 friend S::S(const S
&) noexcept
; // expected-error {{follows constexpr}}
1694 friend constexpr U::U() noexcept
; // expected-error {{follows non-constexpr}}
1695 friend constexpr U::U(U
&&) noexcept
; // expected-error {{follows non-constexpr}}
1696 friend constexpr U::U(const U
&) noexcept
; // expected-error {{follows non-constexpr}}
1700 // Indirectly test that an implicit lvalue to xvalue conversion performed for
1701 // an NRVO move operation isn't implemented as CK_LValueToRValue.
1704 constexpr Foo
id(Foo x
) { return x
; }
1705 constexpr Foo
res(id(Foo()));
1718 // S's default constructor isn't constexpr, because U's default constructor
1719 // doesn't initialize 't', but it's trivial, so value-initialization doesn't
1720 // actually call it.
1721 static_assert(S
{}.t
== 0, "");
1726 constexpr S(int a0
) : m(a0
) {}
1727 constexpr S() : m(6) {}
1730 constexpr S x
[3] = { {4}, 5 };
1731 static_assert(x
[0].m
== 4, "");
1732 static_assert(x
[1].m
== 5, "");
1733 static_assert(x
[2].m
== 6, "");
1736 // Indirectly test that an implicit lvalue-to-rvalue conversion is performed
1737 // when a conditional operator has one argument of type void and where the other
1738 // is a glvalue of class type.
1739 namespace ConditionalLValToRVal
{
1741 constexpr A(int a
) : v(a
) {}
1745 constexpr A
f(const A
&a
) {
1746 return a
.v
== 0 ? throw a
: a
;
1750 static_assert(f(a
).v
== 4, "");
1757 constexpr bool b
= &n
== &n
;
1759 constexpr int *p
= &n
; // expected-error{{constexpr variable 'p' must be initialized by a constant expression}}
1761 constexpr int *f() { return &n
; }
1762 constexpr int *q
= f(); // expected-error{{constexpr variable 'q' must be initialized by a constant expression}}
1763 constexpr bool c
= f() == f();
1765 constexpr int *g() { return &m
; }
1766 constexpr int *r
= g();
1770 constexpr void f() { return; } // cxx11-error{{constexpr function's return type 'void' is not a literal type}}
1772 void assert_failed(const char *msg
, const char *file
, int line
); // expected-note {{declared here}}
1773 #define ASSERT(expr) ((expr) ? static_cast<void>(0) : assert_failed(#expr, __FILE__, __LINE__))
1774 template<typename T
, size_t S
>
1775 constexpr T
get(T (&a
)[S
], size_t k
) {
1776 return ASSERT(k
> 0 && k
< S
), a
[k
]; // expected-note{{non-constexpr function 'assert_failed'}}
1779 template int get(int (&a
)[4], size_t);
1780 constexpr int arr
[] = { 4, 1, 2, 3, 4 };
1781 static_assert(get(arr
, 1) == 1, "");
1782 static_assert(get(arr
, 4) == 4, "");
1783 static_assert(get(arr
, 0) == 4, ""); // expected-error{{not an integral constant expression}} \
1784 // expected-note{{in call to 'get<const int, 5UL>(arr, 0)'}}
1787 namespace std
{ struct type_info
; }
1790 struct A
{ virtual ~A(); };
1792 A
&g(); // cxx20_23-note {{declared here}}
1793 constexpr auto &x
= typeid(f());
1794 constexpr auto &y
= typeid(g()); // expected-error{{constant expression}}
1795 // cxx11-note@-1 {{typeid applied to expression of polymorphic type 'A' is not allowed in a constant expression}}
1796 // expected-warning@-2 {{expression with side effects will be evaluated despite being used as an operand to 'typeid'}}
1797 // cxx20_23-note@-3 {{non-constexpr function 'g' cannot be used in a constant expression}}
1802 constexpr duration() {}
1803 constexpr operator int() const { return 0; }
1805 // These are valid per P0859R0 (moved as DR).
1806 template<typename T
> void f() {
1807 constexpr duration d
= duration();
1809 int n
= sizeof(short{duration(duration())});
1812 namespace ArrayEltInit
{
1814 constexpr A() : p(&p
) {}
1818 static_assert(a
[0].p
== &a
[0].p
, "");
1819 static_assert(a
[9].p
== &a
[9].p
, "");
1820 static_assert(a
[0].p
!= &a
[9].p
, "");
1821 static_assert(a
[9].p
!= &a
[0].p
, "");
1823 constexpr A b
[10] = {};
1824 static_assert(b
[0].p
== &b
[0].p
, "");
1825 static_assert(b
[9].p
== &b
[9].p
, "");
1826 static_assert(b
[0].p
!= &b
[9].p
, "");
1827 static_assert(b
[9].p
!= &b
[0].p
, "");
1832 constexpr S
f() { return {}; }
1833 constexpr S
*p
= &f();
1834 // expected-error@-1 {{taking the address of a temporary}}
1835 // expected-error@-2 {{constexpr variable 'p' must be initialized by a constant expression}}
1836 // expected-note@-3 {{pointer to temporary is not a constant expression}}
1837 // expected-note@-4 {{temporary created here}}
1840 namespace AfterError
{
1841 constexpr int error() {
1842 return foobar
; // expected-error {{undeclared identifier}}
1844 constexpr int k
= error(); // expected-error {{constexpr variable 'k' must be initialized by a constant expression}}
1848 typedef decltype(sizeof(int)) size_t;
1851 class initializer_list
1856 constexpr initializer_list(const _E
* __b
, size_t __s
)
1862 typedef _E value_type
;
1863 typedef const _E
& reference
;
1864 typedef const _E
& const_reference
;
1865 typedef size_t size_type
;
1867 typedef const _E
* iterator
;
1868 typedef const _E
* const_iterator
;
1870 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
1872 constexpr size_t size() const {return __size_
;}
1873 constexpr const _E
* begin() const {return __begin_
;}
1874 constexpr const _E
* end() const {return __begin_
+ __size_
;}
1878 namespace InitializerList
{
1879 constexpr int sum(const int *b
, const int *e
) {
1880 return b
!= e
? *b
+ sum(b
+1, e
) : 0;
1882 constexpr int sum(std::initializer_list
<int> ints
) {
1883 return sum(ints
.begin(), ints
.end());
1885 static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
1887 static_assert(*std::initializer_list
<int>{1, 2, 3}.begin() == 1, "");
1888 static_assert(std::initializer_list
<int>{1, 2, 3}.begin()[2] == 3, "");
1891 constexpr std::initializer_list
<float> il
= {1.0, 2.0, 3.0};
1892 static_assert(il
.begin()[1] == 2.0, "");
1896 namespace StmtExpr
{
1897 struct A
{ int k
; };
1899 static_assert(({ const int x
= 5; x
* 3; }) == 15, ""); // expected-warning {{extension}}
1900 constexpr auto a
= ({ A(); }); // expected-warning {{extension}}
1902 constexpr int g(int k
) {
1903 return ({ // expected-warning {{extension}}
1908 static_assert(g(123) == 15129, "");
1909 constexpr int h() { // expected-error {{never produces a constant}}
1910 return ({ // expected-warning {{extension}}
1911 return 0; // expected-note {{not supported}}
1917 namespace VirtualFromBase
{
1919 virtual int f() const;
1924 template <typename T
> struct X
: T
{
1927 constexpr int f() { return sizeof(T
); } // cxx11-warning {{will not be implicitly 'const' in C++14}}
1930 // Virtual f(), not OK.
1931 constexpr X
<X
<S1
>> xxs1
;
1932 constexpr X
<S1
> *p
= const_cast<X
<X
<S1
>>*>(&xxs1
);
1933 static_assert(p
->f() == sizeof(X
<S1
>), "");
1934 // cxx11-error@-1 {{not an integral constant expression}}
1935 // cxx11-note@-2 {{call to virtual function}}
1936 // cxx20_23-error@-3 {{static assertion failed}}
1937 // cxx20_23-note@-4 {{8 == 16}}
1939 // Non-virtual f(), OK.
1940 constexpr X
<X
<S2
>> xxs2
;
1941 constexpr X
<S2
> *q
= const_cast<X
<X
<S2
>>*>(&xxs2
);
1942 static_assert(q
->f() == sizeof(S2
), ""); // cxx20_23-error {{static assertion failed}} \
1943 // cxx20_23-note {{16 == 8}}
1946 namespace ConstexprConstructorRecovery
{
1954 constexpr X() noexcept
{};
1956 E val
{0}; // cxx11-error {{cannot initialize a member subobject of type 'E' with an rvalue of type 'int'}} cxx11-note {{here}}
1958 // FIXME: We should avoid issuing this follow-on diagnostic.
1959 constexpr X x
{}; // cxx11-error {{constant expression}} cxx11-note {{not initialized}}
1962 namespace Lifetime
{
1964 constexpr int &n
= n
; // expected-error {{constant expression}} expected-note {{use of reference outside its lifetime}} expected-warning {{not yet bound to a value}}
1965 constexpr int m
= m
; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
1968 constexpr int &get(int &&n
) { return n
; }
1969 // cxx23-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
1970 constexpr int &&get_rv(int &&n
) { return static_cast<int&&>(n
); }
1975 constexpr S() : r(get_rv(0)), s(get(0)), t(r
) {} // cxx11_20-note {{read of object outside its lifetime}}
1976 constexpr S(int) : r(get_rv(0)), s(get(0)), t(s
) {} // cxx11_20-note {{read of object outside its lifetime}}
1978 constexpr int k1
= S().t
; // expected-error {{constant expression}} cxx11_20-note {{in call}}
1979 constexpr int k2
= S(0).t
; // expected-error {{constant expression}} cxx11_20-note {{in call}}
1983 constexpr int f() const { return 0; }
1985 constexpr Q
*out_of_lifetime(Q q
) { return &q
; } // expected-warning {{address of stack}}
1986 constexpr int k3
= out_of_lifetime({})->n
; // expected-error {{constant expression}} expected-note {{read of object outside its lifetime}}
1987 constexpr int k4
= out_of_lifetime({})->f(); // expected-error {{constant expression}} expected-note {{member call on object outside its lifetime}}
1989 constexpr int null
= ((Q
*)nullptr)->f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced null pointer}}
1993 constexpr int pte0
= (&q
)[0].f(); // ok
1994 constexpr int pte1
= (&q
)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
1995 constexpr int pte2
= qa
[2].f(); // ok
1996 constexpr int pte3
= qa
[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
2000 constexpr int cpte0
= (&cq
)[0].f(); // ok
2001 constexpr int cpte1
= (&cq
)[1].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
2002 constexpr int cpte2
= cqa
[2].f(); // ok
2003 constexpr int cpte3
= cqa
[3].f(); // expected-error {{constant expression}} expected-note {{member call on dereferenced one-past-the-end pointer}}
2005 // FIXME: There's no way if we can tell if the first call here is valid; it
2006 // depends on the active union member. Should we reject for that reason?
2012 constexpr U u2
= {0};
2013 constexpr int union_member1
= u1
.q
.f();
2014 constexpr int union_member2
= u2
.q
.f(); // expected-error {{constant expression}} expected-note {{member call on member 'q' of union with active member 'n'}}
2016 struct R
{ // expected-note {{field init}}
2017 struct Inner
{ constexpr int f() const { return 0; } };
2018 int a
= b
.f(); // expected-warning {{uninitialized}} expected-note 2{{member call on object outside its lifetime}}
2021 constexpr R r
; // expected-error {{constant expression}} expected-note {{in call}} expected-note {{implicit default constructor for 'Lifetime::R' first required here}}
2023 constexpr R r
; // expected-error {{constant expression}} expected-note {{in call}}
2027 namespace Bitfields
{
2033 unsigned u2
: 74; // expected-warning {{exceeds the width of its type}}
2034 int n2
: 81; // expected-warning {{exceeds the width of its type}}
2037 constexpr A a
= { false, 33, 31, false, 0xffffffff, 0x7fffffff }; // expected-warning 2{{truncation}}
2038 static_assert(a
.b
== 0 && a
.u
== 1 && a
.n
== -1 && a
.b2
== 0 &&
2039 a
.u2
+ 1 == 0 && a
.n2
== 0x7fffffff,
2040 "bad truncation of bitfield values");
2044 constexpr B(int k
) : n(k
) {}
2046 static_assert(B(3).n
== 3, "");
2047 static_assert(B(4).n
== -4, "");
2048 static_assert(B(7).n
== -1, "");
2049 static_assert(B(8).n
== 0, "");
2050 static_assert(B(-1).n
== -1, "");
2051 static_assert(B(-8889).n
== -1, "");
2056 constexpr static int f(int x
) {
2060 static_assert(X::f(3) == -1, "3 should truncate to -1");
2061 static_assert(X::f(1) == -1, "1 should truncate to -1");
2064 struct HasUnnamedBitfield
{
2069 constexpr HasUnnamedBitfield() : a(), b() {}
2070 constexpr HasUnnamedBitfield(unsigned a
, unsigned b
) : a(a
), b(b
) {}
2073 void testUnnamedBitfield() {
2074 const HasUnnamedBitfield zero
{};
2075 int a
= 1 / zero
.b
; // expected-warning {{division by zero is undefined}}
2076 const HasUnnamedBitfield oneZero
{1, 0};
2077 int b
= 1 / oneZero
.b
; // expected-warning {{division by zero is undefined}}
2080 union UnionWithUnnamedBitfield
{
2084 static_assert(UnionWithUnnamedBitfield().n
== 0, "");
2085 static_assert(UnionWithUnnamedBitfield
{}.n
== 0, "");
2086 static_assert(UnionWithUnnamedBitfield
{1}.n
== 1, "");
2089 namespace ZeroSizeTypes
{
2090 constexpr int (*p1
)[0] = 0, (*p2
)[0] = 0;
2091 constexpr int k
= p2
- p1
;
2092 // expected-error@-1 {{constexpr variable 'k' must be initialized by a constant expression}}
2093 // expected-note@-2 {{subtraction of pointers to type 'int[0]' of zero size}}
2096 constexpr int f() { // expected-error {{never produces a constant expression}}
2097 return &arr
[3] - &arr
[0]; // expected-note {{subtraction of pointers to type 'int[0]' of zero size}}
2101 namespace BadDefaultInit
{
2102 template<int N
> struct X
{ static const int n
= N
; };
2104 struct A
{ // expected-error {{default member initializer for 'k' needed within definition of enclosing class}}
2105 int k
= // expected-note {{default member initializer declared here}}
2106 X
<A().k
>::n
; // expected-note {{in evaluation of exception specification for 'BadDefaultInit::A::A' needed here}}
2111 int k
= X
<B().k
>::n
) : // expected-error {{default argument to function 'B' that is declared later}} expected-note {{here}}
2117 namespace NeverConstantTwoWays
{
2118 // If we see something non-constant but foldable followed by something
2119 // non-constant and not foldable, we want the first diagnostic, not the
2121 constexpr int f(int n
) { // expected-error {{never produces a constant expression}}
2122 return (int *)(long)&n
== &n
? // expected-note {{reinterpret_cast}}
2123 1 / 0 : // expected-warning {{division by zero}}
2127 constexpr int n
= // expected-error {{must be initialized by a constant expression}}
2128 (int *)(long)&n
== &n
? // expected-note {{reinterpret_cast}}
2135 constexpr int operator()() const { return 0; }
2137 template <typename
...T
> constexpr int sink(T
...) {
2140 template <int ...N
> constexpr int run() {
2141 return sink(A()() + N
...);
2143 constexpr int k
= run
<1, 2, 3>();
2146 namespace BuiltinStrlen
{
2147 constexpr const char *a
= "foo\0quux";
2148 constexpr char b
[] = "foo\0quux";
2149 constexpr int f() { return 'u'; }
2150 constexpr char c
[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 };
2152 static_assert(__builtin_strlen("foo") == 3, "");
2153 static_assert(__builtin_strlen("foo\0quux") == 3, "");
2154 static_assert(__builtin_strlen("foo\0quux" + 4) == 4, "");
2156 constexpr bool check(const char *p
) {
2157 return __builtin_strlen(p
) == 3 &&
2158 __builtin_strlen(p
+ 1) == 2 &&
2159 __builtin_strlen(p
+ 2) == 1 &&
2160 __builtin_strlen(p
+ 3) == 0 &&
2161 __builtin_strlen(p
+ 4) == 4 &&
2162 __builtin_strlen(p
+ 5) == 3 &&
2163 __builtin_strlen(p
+ 6) == 2 &&
2164 __builtin_strlen(p
+ 7) == 1 &&
2165 __builtin_strlen(p
+ 8) == 0;
2168 static_assert(check(a
), "");
2169 static_assert(check(b
), "");
2170 static_assert(check(c
), "");
2172 constexpr int over1
= __builtin_strlen(a
+ 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2173 constexpr int over2
= __builtin_strlen(b
+ 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2174 constexpr int over3
= __builtin_strlen(c
+ 9); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2176 constexpr int under1
= __builtin_strlen(a
- 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2177 constexpr int under2
= __builtin_strlen(b
- 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2178 constexpr int under3
= __builtin_strlen(c
- 1); // expected-error {{constant expression}} expected-note {{cannot refer to element -1}}
2180 // FIXME: The diagnostic here could be better.
2181 constexpr char d
[] = { 'f', 'o', 'o' }; // no nul terminator.
2182 constexpr int bad
= __builtin_strlen(d
); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
2187 struct Empty2
: Empty
{};
2188 struct Test
: Empty2
{
2192 void test() { constexpr Test t
; }
2195 void PR21327(int a
, int b
) {
2196 static_assert(&a
+ 1 != &b
, ""); // expected-error {{constant expression}}
2197 // expected-note@-1 {{comparison against pointer '&a + 1' that points past the end of a complete object has unspecified value}}
2200 namespace EmptyClass
{
2202 union E2
{} e2
; // expected-note {{here}}
2203 struct E3
: E1
{} e3
;
2205 // The defaulted copy constructor for an empty class does not read any
2206 // members. The defaulted copy constructor for an empty union reads the
2207 // object representation.
2208 constexpr E1
e1b(e1
);
2209 constexpr E2
e2b(e2
); // expected-error {{constant expression}} expected-note{{read of non-const}} expected-note {{in call}}
2210 constexpr E3
e3b(e3
);
2214 extern void (*start
[])();
2215 extern void (*end
[])();
2216 static_assert(&start
!= &end
, ""); // expected-error {{constant expression}}
2217 // expected-note@-1 {{comparison of pointers '&start' and '&end' to unrelated zero-sized objects}}
2218 static_assert(&start
!= nullptr, "");
2225 static_assert(&Bar::x
!= nullptr, "");
2226 static_assert(&Bar::x
!= &Bar::y
, "");
2230 constexpr int Fun() { return; } // expected-error {{non-void constexpr function 'Fun' should return a value}}
2231 constexpr int Var
= Fun();
2233 template <typename T
> constexpr int FunT1() { return; } // expected-error {{non-void constexpr function 'FunT1' should return a value}}
2234 template <typename T
> constexpr int FunT2() { return 0; }
2235 template <> constexpr int FunT2
<double>() { return 0; }
2236 template <> constexpr int FunT2
<int>() { return; } // expected-error {{non-void constexpr function 'FunT2<int>' should return a value}}
2239 struct InvalidRedef
{
2240 int f
; // expected-note{{previous definition is here}}
2241 constexpr int f(void); // expected-error{{redefinition of 'f'}} cxx11-warning{{will not be implicitly 'const'}}
2245 template <typename T
> constexpr T
const &f(T
const &x
) { return x
; }
2249 struct Z
: Y
{ constexpr Z() {} };
2251 static constexpr auto z
= f(Z());
2257 constexpr A() : x(0), p(&x
) {}
2258 constexpr A(const A
&a
) : x(a
.x
), p(&x
) {}
2260 constexpr A
f() { return A(); }
2261 constexpr A
g() { return f(); }
2262 constexpr int a
= *f().p
;
2263 constexpr int b
= *g().p
;
2266 namespace IncompleteClass
{
2268 static constexpr int f(XX
*) { return 1; } // expected-note {{here}}
2269 friend constexpr int g(XX
*) { return 2; } // expected-note {{here}}
2271 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}}
2272 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}}
2276 namespace InheritedCtor
{
2277 struct A
{ constexpr A(int) {} };
2279 struct B
: A
{ int n
; using A::A
; }; // expected-note {{here}}
2280 constexpr B
b(0); // expected-error {{constant expression}} expected-note {{derived class}}
2282 struct C
: A
{ using A::A
; struct { union { int n
, m
= 0; }; union { int a
= 0; }; int k
= 0; }; struct {}; union {}; }; // expected-warning 6{{}}
2286 using A::A
; // cxx11-note {{here}}
2287 struct { // expected-warning {{extension}}
2288 union { // expected-warning {{extension}}
2293 constexpr D
d(0); // cxx11-error {{constant expression}} cxx11-note {{derived class}}
2295 struct E
: virtual A
{ using A::A
; }; // expected-note {{here}}
2296 // cxx20_23-note@-1 {{struct with virtual base class is not a literal type}}
2297 // We wrap a function around this to avoid implicit zero-initialization
2298 // happening first; the zero-initialization step would produce the same
2299 // error and defeat the point of this test.
2301 constexpr E
e(0); // cxx11-error {{constant expression}} cxx11-note {{derived class}}
2302 // cxx20_23-error@-1 {{constexpr variable cannot have non-literal type}}
2304 // FIXME: This produces a note with no source location.
2307 struct W
{ constexpr W(int n
) : w(n
) {} int w
; };
2308 struct X
: W
{ using W::W
; int x
= 2; };
2309 struct Y
: X
{ using X::X
; int y
= 3; };
2310 struct Z
: Y
{ using Y::Y
; int z
= 4; };
2312 static_assert(z
.w
== 1 && z
.x
== 2 && z
.y
== 3 && z
.z
== 4, "");
2319 void f(char c
) { //expected-note2{{declared here}}
2321 static constexpr char f() { //expected-error{{never produces a constant expression}}
2322 return c
; //expected-error{{reference to local}} expected-note{{function parameter}}
2330 static const int d
= 'd';
2332 static constexpr int f() {
2336 static_assert(X::f() == 'c' + 'd',"");
2344 namespace PointerArithmeticOverflow
{
2347 constexpr int *b
= &n
+ 1 + (long)-1;
2348 constexpr int *c
= &n
+ 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}}
2349 constexpr int *d
= &n
+ 1 - (unsigned long)1;
2350 constexpr int *e
= a
+ 1 + (long)-1;
2351 constexpr int *f
= a
+ 1 + (unsigned long)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 1844}}
2352 constexpr int *g
= a
+ 1 - (unsigned long)1;
2354 constexpr int *p
= (&n
+ 1) + (unsigned __int128
)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}}
2355 constexpr int *q
= (&n
+ 1) - (unsigned __int128
)-1; // expected-error {{constant expression}} expected-note {{cannot refer to element -3402}}
2356 constexpr int *r
= &(&n
+ 1)[(unsigned __int128
)-1]; // expected-error {{constant expression}} expected-note {{cannot refer to element 3402}}
2361 char c
[10] = "asdf";
2362 constexpr char foo() const { return c
[3]; }
2364 static_assert(S().foo() == 'f', "");
2368 struct e
{ operator int(); };
2371 f
&d
= reinterpret_cast<f
&>(a
);
2375 namespace array_size
{
2376 template<int N
> struct array
{
2377 static constexpr int size() { return N
; }
2379 template<typename T
> void f1(T t
) {
2380 constexpr int k
= t
.size();
2382 template<typename T
> void f2(const T
&t
) { // expected-note 2{{declared here}}
2383 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}}
2385 template<typename T
> void f3(const T
&t
) {
2386 constexpr int k
= T::size();
2388 void g(array
<3> a
) {
2390 f2(a
); // expected-note {{instantiation of}}
2394 template<int N
> struct array_nonstatic
{
2395 constexpr int size() const { return N
; }
2397 void h(array_nonstatic
<3> a
) {
2399 f2(a
); // expected-note {{instantiation of}}
2403 namespace flexible_array
{
2404 struct A
{ int x
; char arr
[]; }; // expected-warning {{C99}} expected-note {{here}}
2405 constexpr A a
= {1};
2406 static_assert(a
.x
== 1, "");
2407 static_assert(&a
.arr
!= nullptr, "");
2408 static_assert(a
.arr
[0], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2409 static_assert(a
.arr
[1], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2411 constexpr A b
[] = {{1}, {2}, {3}}; // expected-warning {{flexible array member}}
2412 static_assert(b
[0].x
== 1, "");
2413 static_assert(b
[1].x
== 2, "");
2414 static_assert(b
[2].x
== 3, "");
2415 static_assert(b
[2].arr
[0], ""); // expected-error {{constant expression}} expected-note {{array member without known bound}}
2417 // Flexible array initialization is currently not supported by constant
2418 // evaluation. Make sure we emit an error message, for now.
2419 constexpr A c
= {1, 2, 3}; // expected-error {{constexpr variable 'c' must be initialized by a constant expression}}
2420 // expected-note@-1 {{flexible array initialization is not yet supported}}
2421 // expected-warning@-2 {{flexible array initialization is a GNU extension}}
2424 void local_constexpr_var() {
2425 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}}
2426 constexpr const int *p
= &a
; // expected-error {{constant expression}} expected-note {{pointer to 'a' is not a constant expression}}
2430 // Enums without fixed underlying type
2431 enum E1
{e11
=-4, e12
=4};
2432 enum E2
{e21
=0, e22
=4};
2433 enum E3
{e31
=-4, e32
=1024};
2435 // Empty but as-if it had a single enumerator with value 0
2438 // Enum with fixed underlying type because the underlying type is explicitly specified
2439 enum EFixed
: int {efixed1
=-4, efixed2
=4};
2440 // Enum with fixed underlying type because it is scoped
2441 enum class EScoped
{escoped1
=-4, escoped2
=4};
2443 enum EMaxInt
{emaxint1
=-1, emaxint2
=__INT_MAX__
};
2447 E2
testDefaultArgForParam(E2 e2Param
= (E2
)-1) { // ok, not a constant expression context
2448 E2 e2LocalInit
= e2Param
; // ok, not a constant expression context
2452 #include <enum-constexpr-conversion-system-header.h>
2454 void testValueInRangeOfEnumerationValues() {
2455 constexpr E1 x1
= static_cast<E1
>(-8);
2456 constexpr E1 x2
= static_cast<E1
>(8);
2457 // expected-error@-1 {{integer value 8 is outside the valid range of values [-8, 7] for the enumeration type 'E1'}}
2458 E1 x2b
= static_cast<E1
>(8); // ok, not a constant expression context
2460 constexpr E2 x3
= static_cast<E2
>(-8);
2461 // expected-error@-1 {{integer value -8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
2462 constexpr E2 x4
= static_cast<E2
>(0);
2463 constexpr E2 x5
= static_cast<E2
>(8);
2464 // expected-error@-1 {{integer value 8 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
2466 constexpr E3 x6
= static_cast<E3
>(-2048);
2467 constexpr E3 x7
= static_cast<E3
>(-8);
2468 constexpr E3 x8
= static_cast<E3
>(0);
2469 constexpr E3 x9
= static_cast<E3
>(8);
2470 constexpr E3 x10
= static_cast<E3
>(2048);
2471 // expected-error@-1 {{integer value 2048 is outside the valid range of values [-2048, 2047] for the enumeration type 'E3'}}
2473 constexpr E4 x11
= static_cast<E4
>(0);
2474 constexpr E4 x12
= static_cast<E4
>(1);
2475 constexpr E4 x13
= static_cast<E4
>(2);
2476 // expected-error@-1 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'E4'}}
2478 constexpr EEmpty x14
= static_cast<EEmpty
>(0);
2479 constexpr EEmpty x15
= static_cast<EEmpty
>(1);
2480 constexpr EEmpty x16
= static_cast<EEmpty
>(2);
2481 // expected-error@-1 {{integer value 2 is outside the valid range of values [0, 1] for the enumeration type 'EEmpty'}}
2483 constexpr EFixed x17
= static_cast<EFixed
>(100);
2484 constexpr EScoped x18
= static_cast<EScoped
>(100);
2486 constexpr EMaxInt x19
= static_cast<EMaxInt
>(__INT_MAX__
-1);
2487 constexpr EMaxInt x20
= static_cast<EMaxInt
>((long)__INT_MAX__
+1);
2488 // expected-error@-1 {{integer value 2147483648 is outside the valid range of values [-2147483648, 2147483647] for the enumeration type 'EMaxInt'}}
2490 const NumberType neg_one
= (NumberType
) ((NumberType
) 0 - (NumberType
) 1); // ok, not a constant expression context
2492 CONSTEXPR_CAST_TO_SYSTEM_ENUM_OUTSIDE_OF_RANGE
;
2493 // expected-error@-1 {{integer value 123 is outside the valid range of values [0, 1] for the enumeration type 'SystemEnum'}}
2496 template<class T
, unsigned size
> struct Bitfield
{
2497 static constexpr T max
= static_cast<T
>((1 << size
) - 1); // #enum
2500 void testValueInRangeOfEnumerationValuesViaTemplate() {
2501 Bitfield
<E2
, 3> good
;
2502 Bitfield
<E2
, 4> bad
; // cxx11-error@#enum {{integer value 15 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}
2511 static void f(SortOrder order
);
2514 void A::f(SortOrder order
) {
2515 if (order
== SortOrder(-1)) // ok, not a constant expression context
2520 GH50055::E2 GlobalInitNotCE1
= (GH50055::E2
)-1; // ok, not a constant expression context
2521 GH50055::E2 GlobalInitNotCE2
= GH50055::testDefaultArgForParam(); // ok, not a constant expression context
2522 constexpr GH50055::E2 GlobalInitCE
= (GH50055::E2
)-1;
2523 // expected-error@-1 {{integer value -1 is outside the valid range of values [0, 7] for the enumeration type 'E2'}}