1 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify=expected,cxx20_23,cxx23 %s -fcxx-exceptions -triple=x86_64-linux-gnu
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected,cxx14_20,cxx20_23,cxx20 %s -fcxx-exceptions -triple=x86_64-linux-gnu
3 // RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify=expected,cxx14_20,cxx14 %s -fcxx-exceptions -triple=x86_64-linux-gnu
6 // dummy ctor to make this a literal type
13 constexpr int &get(int n
) { return arr
[n
]; }
14 constexpr const int &get(int n
) const { return arr
[n
]; }
19 static_assert(&s
.get(4) - &sr
.get(2) == 2, "");
21 // Compound-statements can be used in constexpr functions.
22 constexpr int e() {{{{}} return 5; }}
23 static_assert(e() == 5, "");
25 // Types can be defined in constexpr functions.
27 enum E
{ e1
, e2
, e3
};
30 constexpr S(E e
) : e(e
) {}
31 constexpr int get() { return e
; }
37 static_assert(f() == 1, "");
39 // Variables can be declared in constexpr functions.
40 constexpr int g(int k
) {
44 return 3 * k3
+ 5 * k2
+ n
* k
- 20;
46 static_assert(g(2) == 42, "");
47 constexpr int h(int n
) { // expected-error {{constexpr function never produces a constant expression}}
48 static const int m
= n
; // expected-note {{control flows through the definition of a static variable}} \
49 // cxx14_20-warning {{definition of a static variable in a constexpr function is a C++23 extension}}
52 constexpr int i(int n
) { // expected-error {{constexpr function never produces a constant expression}}
53 thread_local
const int m
= n
; // expected-note {{control flows through the definition of a thread_local variable}} \
54 // cxx14_20-warning {{definition of a thread_local variable in a constexpr function is a C++23 extension}}
58 // if-statements can be used in constexpr functions.
59 constexpr int j(int k
) {
65 if (int n
= 2 * k
- 4) {
70 } // expected-note 2{{control reached end of constexpr function}}
71 static_assert(j(0) == -3, "");
72 static_assert(j(1) == 5, "");
73 static_assert(j(2), ""); // expected-error {{constant expression}} expected-note {{in call to 'j(2)'}}
74 static_assert(j(3) == 3, "");
75 static_assert(j(4) == 5, "");
76 static_assert(j(5) == 1, "");
78 // There can be 0 return-statements.
82 // If the return type is not 'void', no return statements => never a constant
83 // expression, so still diagnose that case.
84 [[noreturn
]] constexpr int fn() { // expected-error {{no return statement in constexpr function}}
88 // We evaluate the body of a constexpr constructor, to check for side-effects.
91 if (j(n
)) {} // expected-note {{in call to 'j(2)'}}
95 constexpr U u2
{2}; // expected-error {{constant expression}} expected-note {{in call to 'U(2)'}}
97 // We allow expression-statements.
98 constexpr int l(bool b
) {
100 throw "invalid value for b!"; // expected-note {{subexpression not valid}}
103 static_assert(l(false) == 5, "");
104 static_assert(l(true), ""); // expected-error {{constant expression}} expected-note {{in call to 'l(true)'}}
106 // Potential constant expression checking is still applied where possible.
107 constexpr int htonl(int x
) { // expected-error {{never produces a constant expression}}
108 typedef unsigned char uchar
;
109 uchar arr
[4] = { uchar(x
>> 24), uchar(x
>> 16), uchar(x
>> 8), uchar(x
) };
110 return *reinterpret_cast<int*>(arr
); // expected-note {{reinterpret_cast is not allowed in a constant expression}}
113 constexpr int maybe_htonl(bool isBigEndian
, int x
) {
117 typedef unsigned char uchar
;
118 uchar arr
[4] = { uchar(x
>> 24), uchar(x
>> 16), uchar(x
>> 8), uchar(x
) };
119 return *reinterpret_cast<int*>(arr
); // expected-note {{reinterpret_cast is not allowed in a constant expression}}
122 constexpr int swapped
= maybe_htonl(false, 123); // expected-error {{constant expression}} expected-note {{in call}}
127 constexpr int namespace_alias() {
135 int c
= 0; // expected-note {{here}}
137 constexpr void set(const int &a
, int b
) {
138 const_cast<int&>(a
) = b
; // expected-note 3{{constant expression cannot modify an object that is visible outside that expression}}
140 constexpr int wrap(int a
, int b
) {
145 static_assert((set(a
, 1), a
) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(a, 1)'}}
146 static_assert((set(b
, 1), b
) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(b, 1)'}}
147 static_assert((set(c
, 1), c
) == 1, ""); // expected-error {{constant expression}} expected-note {{in call to 'set(c, 1)'}}
149 static_assert(wrap(a
, 1) == 1, "");
150 static_assert(wrap(b
, 1) == 1, "");
151 static_assert(wrap(c
, 1) == 1, ""); // expected-error {{constant expression}} expected-note {{read of non-const variable 'c'}}
154 namespace string_assign
{
156 constexpr void swap(T
&a
, T
&b
) {
161 template<typename Iterator
>
162 constexpr void reverse(Iterator begin
, Iterator end
) {
163 while (begin
!= end
&& begin
!= --end
)
164 swap(*begin
++, *end
);
166 template<typename Iterator1
, typename Iterator2
>
167 constexpr bool equal(Iterator1 a
, Iterator1 ae
, Iterator2 b
, Iterator2 be
) {
168 while (a
!= ae
&& b
!= be
)
171 return a
== ae
&& b
== be
;
173 constexpr bool test1(int n
) {
174 char stuff
[100] = "foobarfoo";
175 const char stuff2
[100] = "oofraboof";
176 reverse(stuff
, stuff
+ n
); // expected-note {{cannot refer to element 101 of array of 100 elements}}
177 return equal(stuff
, stuff
+ n
, stuff2
, stuff2
+ n
);
179 static_assert(!test1(1), "");
180 static_assert(test1(3), "");
181 static_assert(!test1(6), "");
182 static_assert(test1(9), "");
183 static_assert(!test1(100), "");
184 static_assert(!test1(101), ""); // expected-error {{constant expression}} expected-note {{in call to 'test1(101)'}}
186 constexpr void f() { // expected-error{{constexpr function never produces a constant expression}} expected-note@+2{{assignment to dereferenced one-past-the-end pointer is not allowed in a constant expression}}
187 char foo
[10] = { "z" }; // expected-note {{here}}
188 foo
[10] = 'x'; // expected-warning {{past the end}}
192 namespace array_resize
{
193 constexpr int do_stuff(int k1
, int k2
) {
194 int arr
[1234] = { 1, 2, 3, 4 };
195 arr
[k1
] = 5; // expected-note {{past-the-end}} expected-note {{cannot refer to element 1235}} expected-note {{cannot refer to element -1}}
198 static_assert(do_stuff(1, 2) == 3, "");
199 static_assert(do_stuff(0, 0) == 5, "");
200 static_assert(do_stuff(1233, 1233) == 5, "");
201 static_assert(do_stuff(1233, 0) == 1, "");
202 static_assert(do_stuff(1234, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
203 static_assert(do_stuff(1235, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
204 static_assert(do_stuff(-1, 0) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
207 namespace potential_const_expr
{
208 constexpr void set(int &n
) { n
= 1; }
209 constexpr int div_zero_1() { int z
= 0; set(z
); return 100 / z
; } // no error
210 constexpr int div_zero_2() { // expected-error {{never produces a constant expression}}
212 return 100 / (set(z
), 0); // expected-note {{division by zero}}
214 int n
; // expected-note {{declared here}}
215 constexpr int ref() { // expected-error {{never produces a constant expression}}
217 return r
; // expected-note {{read of non-const variable 'n'}}
221 namespace subobject
{
222 union A
{ constexpr A() : y(5) {} int x
, y
; };
225 union D
{ constexpr D() : c() {} constexpr D(int n
) : n(n
) {} C c
; int n
; };
226 constexpr void f(D
&d
) {
228 // expected-note@-1 {{cannot modify an object that is visible outside}}
229 // expected-note@-2 {{assignment to member 'c' of union with active member 'n'}}
231 constexpr bool check(D
&d
) { return d
.c
.a
.y
== 3; }
232 // cxx20_23-note@-1 {{read of member 'y' of union with active member 'x'}}
234 constexpr bool g() { D d
; f(d
); return d
.c
.a
.y
== 3; }
235 static_assert(g(), "");
238 constexpr bool h() { f(d
); return check(d
); } // expected-note {{in call}}
239 static_assert(h(), ""); // expected-error {{constant expression}} expected-note {{in call}}
241 constexpr bool i() { D
d(0); f(d
); return check(d
); } // expected-note {{in call}}
242 static_assert(i(), ""); // expected-error {{constant expression}} expected-note {{in call}}
244 constexpr bool j() { D d
; d
.c
.a
.x
= 3; return check(d
); } // cxx14-note {{assignment to member 'x' of union with active member 'y'}}
245 // cxx20_23-note@-1 {{in call to 'check(d)'}}
246 static_assert(j(), ""); // expected-error {{constant expression}} expected-note {{in call}}
250 constexpr int &&id(int &&n
) { return static_cast<int&&>(n
); }
251 constexpr int &&dead() { return id(0); } // expected-note {{temporary created here}}
252 constexpr int bad() { int &&n
= dead(); n
= 1; return n
; } // expected-note {{assignment to temporary whose lifetime has ended}}
253 static_assert(bad(), ""); // expected-error {{constant expression}} expected-note {{in call}}
256 namespace const_modify
{
257 constexpr int modify(int &n
) { return n
= 1; } // expected-note 2 {{modification of object of const-qualified type 'const int'}}
258 constexpr int test1() { int k
= 0; return modify(k
); }
259 constexpr int test2() { const int k
= 0; return modify(const_cast<int&>(k
)); } // expected-note 2 {{in call}}
260 static_assert(test1() == 1, "");
261 static_assert(test2() == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
262 constexpr int i
= test2(); // expected-error {{constant expression}} expected-note {{in call}}
266 constexpr int test(int *p
) {
267 return *p
= 123; // expected-note {{assignment to dereferenced null pointer}}
269 static_assert(test(0), ""); // expected-error {{constant expression}} expected-note {{in call}}
273 template<typename T
> constexpr T
&ref(T
&&r
) { return r
; }
274 // cxx23-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
275 template<typename T
> constexpr T
postinc(T
&&r
) { return (r
++, r
); }
276 template<typename T
> constexpr T
postdec(T
&&r
) { return (r
--, r
); }
278 template int &ref
<int>(int &&);
279 // cxx23-note@-1 {{in instantiation of function template specialization}}
281 static_assert(postinc(0) == 1, "");
282 static_assert(postdec(0) == -1, "");
283 #if __cplusplus <= 202002L
284 static_assert(++ref(0) == 1, "");
285 static_assert(ref(0)++ == 0, "");
286 static_assert(--ref(0) == -1, "");
287 static_assert(ref(0)-- == 0, "");
290 #if __cplusplus <= 202002L
291 constexpr int overflow_int_inc_1
= ref(0x7fffffff)++; // expected-error {{constant}} expected-note {{2147483648}}
292 constexpr int overflow_int_inc_1_ok
= ref(0x7ffffffe)++;
293 constexpr int overflow_int_inc_2
= ++ref(0x7fffffff); // expected-error {{constant}} expected-note {{2147483648}}
294 constexpr int overflow_int_inc_2_ok
= ++ref(0x7ffffffe);
296 // inc/dec on short can't overflow because we promote to int first
297 static_assert(++ref
<short>(0x7fff) == (int)0xffff8000u
, "");
298 static_assert(--ref
<short>(0x8000) == 0x7fff, "");
300 // inc on bool sets to true
301 static_assert(++ref(false), "");
302 // cxx14-warning@-1 {{incrementing expression of type bool}}
303 // cxx20-error@-2 {{incrementing expression of type bool}}
304 static_assert(++ref(true), "");
305 // cxx14-warning@-1 {{incrementing expression of type bool}}
306 // cxx20-error@-2 {{incrementing expression of type bool}}
310 static_assert(postinc(&arr
[0]) == &arr
[1], "");
311 static_assert(postdec(&arr
[1]) == &arr
[0], "");
312 #if __cplusplus <= 202002L
313 static_assert(++ref(&arr
[0]) == &arr
[1], "");
314 static_assert(++ref(&arr
[9]) == &arr
[10], "");
315 static_assert(++ref(&arr
[10]) == &arr
[11], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}}
316 static_assert(ref(&arr
[0])++ == &arr
[0], "");
317 static_assert(ref(&arr
[10])++ == &arr
[10], ""); // expected-error {{constant}} expected-note {{cannot refer to element 11}}
318 static_assert(--ref(&arr
[10]) == &arr
[9], "");
319 static_assert(--ref(&arr
[1]) == &arr
[0], "");
320 static_assert(--ref(&arr
[0]) != &arr
[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}}
321 static_assert(ref(&arr
[1])-- == &arr
[1], "");
322 static_assert(ref(&arr
[0])-- == &arr
[0], ""); // expected-error {{constant}} expected-note {{cannot refer to element -1}}
325 static_assert(postinc(0.0) == 1.0, "");
326 static_assert(postdec(0.0) == -1.0, "");
327 #if __cplusplus <= 202002L
329 static_assert(++ref(&x
) == &x
+ 1, "");
331 static_assert(++ref(0.0) == 1.0, "");
332 static_assert(ref(0.0)++ == 0.0, "");
333 static_assert(--ref(0.0) == -1.0, "");
334 static_assert(ref(0.0)-- == 0.0, "");
336 static_assert(++ref(1e100
) == 1e100
, "");
337 static_assert(--ref(1e100
) == 1e100
, "");
343 constexpr int f(U u
) {
344 return ++u
.b
; // expected-note {{increment of member 'b' of union with active member 'a'}}
346 constexpr int wrong_member
= f({0}); // expected-error {{constant}} expected-note {{in call to 'f({.a = 0})'}}
347 constexpr int vol
= --ref
<volatile int>(0); // expected-error {{constant}} expected-note {{decrement of volatile-qualified}}
348 // cxx20_23-warning@-1 {{decrement of object of volatile-qualified type 'volatile int' is deprecated}}
350 constexpr int incr(int k
) {
356 static_assert(incr(0) == 101, "");
359 namespace compound_assign
{
360 constexpr bool test_int() {
363 if (a
!= 9) return false;
365 if (a
!= 7) return false;
367 if (a
!= 21) return false;
368 if (&(a
/= 10) != &a
) return false;
369 if (a
!= 2) return false;
371 if (a
!= 16) return false;
373 if (a
!= 4) return false;
375 if (a
!= 2) return false;
377 if (a
!= 8) return false;
379 if (a
!= 13) return false;
381 if (a
!= 12) return false;
383 if (a
!= 10) return false;
385 if (a
!= 6) return false;
387 if (a
!= 13) return false;
388 if (&(a
/= 1.5) != &a
) return false;
389 if (a
!= 8) return false;
392 static_assert(test_int(), "");
394 constexpr bool test_float() {
397 if (f
!= 246.) return false;
398 if ((f
-= 0.5) != 245.5) return false;
399 if (f
!= 245.5) return false;
401 if (f
!= 491.) return false;
403 if (f
!= 451.) return false;
406 static_assert(test_float(), "");
408 constexpr bool test_bool() {
411 if (b
!= true) return false;
413 if (b
!= true) return false;
415 if (b
!= true) return false;
417 if (b
!= false) return false;
419 if (b
!= true) return false;
421 if (b
!= false) return false;
423 if (b
!= true) return false;
425 if (b
!= true) return false;
427 if (b
!= false) return false;
430 static_assert(test_bool(), "");
432 constexpr bool test_ptr() {
435 if ((p
+= 4) != &arr
[4]) return false;
436 if (p
!= &arr
[4]) return false;
438 if (p
!= &arr
[3]) return false;
439 if ((p
-= -10) != &arr
[13]) return false;
440 if (p
!= &arr
[13]) return false;
442 if (p
!= &arr
[2]) return false;
445 static_assert(test_ptr(), "");
448 constexpr bool test_overflow() {
451 a
*= 2; // expected-note {{value 2147483648 is outside the range}} expected-note {{ 9223372036854775808 }}
455 static_assert(test_overflow
<int>(), ""); // expected-error {{constant}} expected-note {{call}}
456 static_assert(test_overflow
<unsigned>(), ""); // ok, unsigned overflow is defined
457 static_assert(test_overflow
<short>(), ""); // ok, short is promoted to int before multiplication
458 static_assert(test_overflow
<unsigned short>(), ""); // ok
459 static_assert(test_overflow
<unsigned long long>(), ""); // ok
460 static_assert(test_overflow
<long long>(), ""); // expected-error {{constant}} expected-note {{call}}
461 static_assert(test_overflow
<float>(), ""); // ok
462 static_assert(test_overflow
<double>(), ""); // ok
464 constexpr short test_promotion(short k
) {
469 static_assert(test_promotion(100) == 10000, "");
470 static_assert(test_promotion(200) == -25536, "");
471 static_assert(test_promotion(256) == 0, "");
473 constexpr const char *test_bounds(const char *p
, int o
) {
474 return p
+= o
; // expected-note {{element 5 of}} expected-note {{element -1 of}} expected-note {{element 1000 of}}
476 static_assert(test_bounds("foo", 0)[0] == 'f', "");
477 static_assert(test_bounds("foo", 3)[0] == 0, "");
478 static_assert(test_bounds("foo", 4)[-3] == 'o', "");
479 static_assert(test_bounds(&"foo"[4], -4)[0] == 'f', "");
480 static_assert(test_bounds("foo", 5) != 0, ""); // expected-error {{constant}} expected-note {{call}}
481 static_assert(test_bounds("foo", -1) != 0, ""); // expected-error {{constant}} expected-note {{call}}
482 static_assert(test_bounds("foo", 1000) != 0, ""); // expected-error {{constant}} expected-note {{call}}
486 constexpr int fib_loop(int a
) {
487 int f_k
= 0, f_k_plus_one
= 1;
488 for (int k
= 1; k
!= a
; ++k
) {
489 int f_k_plus_two
= f_k
+ f_k_plus_one
;
491 f_k_plus_one
= f_k_plus_two
;
495 static_assert(fib_loop(46) == 1836311903, "");
497 constexpr bool breaks_work() {
499 for (int n
= 0; n
!= 100; ++n
) {
501 if (a
== 5) continue;
502 if ((a
% 5) == 0) break;
508 if (b
== 6) continue;
509 if ((b
% 6) == 0) break;
515 if (c
== 7) continue;
516 if ((c
% 7) == 0) break;
519 return a
== 10 && b
== 12 && c
== 14;
521 static_assert(breaks_work(), "");
523 void not_constexpr();
524 constexpr bool no_cont_after_break() {
539 static_assert(no_cont_after_break(), "");
541 constexpr bool cond() {
542 for (int a
= 1; bool b
= a
!= 3; ++a
) {
546 while (bool b
= true) {
552 static_assert(cond(), "");
554 constexpr int range_for() {
555 int arr
[] = { 1, 2, 3, 4, 5 };
561 static_assert(range_for() == 15, "");
563 template<int...N
> struct ints
{};
564 template<typename A
, typename B
> struct join_ints
;
565 template<int...As
, int...Bs
> struct join_ints
<ints
<As
...>, ints
<Bs
...>> {
566 using type
= ints
<As
..., sizeof...(As
) + Bs
...>;
568 template<unsigned N
> struct make_ints
{
569 using type
= typename join_ints
<typename make_ints
<N
/2>::type
, typename make_ints
<(N
+1)/2>::type
>::type
;
571 template<> struct make_ints
<0> { using type
= ints
<>; };
572 template<> struct make_ints
<1> { using type
= ints
<0>; };
574 struct ignore
{ template<typename
...Ts
> constexpr ignore(Ts
&&...) {} };
576 template<typename T
, unsigned N
> struct array
{
577 constexpr array() : arr
{} {}
578 template<typename
...X
>
579 constexpr array(X
...x
) : arr
{} {
580 init(typename make_ints
<sizeof...(X
)>::type
{}, x
...);
582 template<int ...I
, typename
...X
> constexpr void init(ints
<I
...>, X
...x
) {
583 ignore
{arr
[I
] = x
...};
588 constexpr explicit iterator(T
*p
) : p(p
) {}
589 constexpr bool operator!=(iterator o
) { return p
!= o
.p
; }
590 constexpr iterator
&operator++() { ++p
; return *this; }
591 constexpr T
&operator*() { return *p
; }
593 constexpr iterator
begin() { return iterator(arr
); }
594 constexpr iterator
end() { return iterator(arr
+ N
); }
597 constexpr int range_for_2() {
598 array
<int, 5> arr
{ 1, 2, 3, 4, 5 };
606 static_assert(range_for_2() == 10, "");
609 namespace assignment_op
{
611 constexpr A() : n(5) {}
616 constexpr U() : y(4) {}
622 constexpr bool testA() {
627 return b
.n
== 7 && b
.b
.u
.y
== 5 && b
.b
.k
== 1;
629 static_assert(testA(), "");
632 bool assigned
= false;
633 constexpr B
&operator=(const B
&) {
642 constexpr bool testC() {
647 return d
.n
== 7 && d
.assigned
&& d
.b
.assigned
;
649 static_assert(testC(), "");
652 namespace switch_stmt
{
653 constexpr bool no_such_case(int n
) {
654 switch (n
) { case 1: return false; }
657 static_assert(no_such_case(0), "");
659 constexpr int f(char k
) {
683 } else if (false) case 8: z
= 8;
696 static_assert(f(0) == 0, "");
697 static_assert(f(1) == 1, "");
698 static_assert(f(2) == 2, "");
699 static_assert(f(3) == 3, "");
700 static_assert(f(4) == 4, "");
701 static_assert(f(5) == 5, "");
702 static_assert(f(6) == 6, "");
703 static_assert(f(7) == 7, "");
704 static_assert(f(8) == 8, "");
705 static_assert(f(9) == 9, "");
706 static_assert(f(10) == 10, "");
708 // Check that we can continue an outer loop from within a switch.
709 constexpr bool contin() {
710 for (int n
= 0; n
!= 10; ++n
) {
723 static_assert(contin(), "");
725 constexpr bool switch_into_for() {
728 for (; n
== 1; ++n
) {
735 static_assert(switch_into_for(), "");
737 constexpr void duff_copy(char *a
, const char *b
, int n
) {
738 switch ((n
- 1) % 8 + 1) {
739 for ( ; n
; n
= (n
- 1) & ~7) {
740 case 8: a
[n
-8] = b
[n
-8];
741 case 7: a
[n
-7] = b
[n
-7];
742 case 6: a
[n
-6] = b
[n
-6];
743 case 5: a
[n
-5] = b
[n
-5];
744 case 4: a
[n
-4] = b
[n
-4];
745 case 3: a
[n
-3] = b
[n
-3];
746 case 2: a
[n
-2] = b
[n
-2];
747 case 1: a
[n
-1] = b
[n
-1];
753 constexpr bool test_copy(const char *str
, int n
) {
754 char buffer
[16] = {};
755 duff_copy(buffer
, str
, n
);
756 for (int i
= 0; i
!= sizeof(buffer
); ++i
)
757 if (buffer
[i
] != (i
< n
? str
[i
] : 0))
761 static_assert(test_copy("foo", 0), "");
762 static_assert(test_copy("foo", 1), "");
763 static_assert(test_copy("foo", 2), "");
764 static_assert(test_copy("hello world", 0), "");
765 static_assert(test_copy("hello world", 7), "");
766 static_assert(test_copy("hello world", 8), "");
767 static_assert(test_copy("hello world", 9), "");
768 static_assert(test_copy("hello world", 10), "");
769 static_assert(test_copy("hello world", 10), "");
772 namespace deduced_return_type
{
773 constexpr auto f() { return 0; }
774 template<typename T
> constexpr auto g(T t
) { return t
; }
775 static_assert(f() == 0, "");
776 static_assert(g(true), "");
779 namespace modify_temporary_during_construction
{
780 struct A
{ int &&temporary
; int x
; int y
; };
781 constexpr int f(int &r
) { r
*= 9; return r
- 12; }
782 constexpr A a
= { 6, f(a
.temporary
), a
.temporary
}; // expected-note {{temporary created here}}
783 static_assert(a
.x
== 42, "");
784 static_assert(a
.y
== 54, "");
785 constexpr int k
= a
.temporary
++; // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
789 typedef decltype(sizeof(int)) size_t;
792 class initializer_list
797 constexpr initializer_list(const _E
* __b
, size_t __s
)
803 typedef _E value_type
;
804 typedef const _E
& reference
;
805 typedef const _E
& const_reference
;
806 typedef size_t size_type
;
808 typedef const _E
* iterator
;
809 typedef const _E
* const_iterator
;
811 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
813 constexpr size_t size() const {return __size_
;}
814 constexpr const _E
* begin() const {return __begin_
;}
815 constexpr const _E
* end() const {return __begin_
+ __size_
;}
819 namespace InitializerList
{
820 constexpr int sum(std::initializer_list
<int> ints
) {
822 for (int n
: ints
) total
+= n
;
825 static_assert(sum({1, 2, 3, 4, 5}) == 15, "");
829 constexpr int f(int k
) {
834 ({ // expected-note {{jump enters a statement expression}}
835 case 1:// expected-error {{cannot jump from switch statement to this case label}} \
836 // expected-note {{not supported}}
841 static_assert(f(1) == 1, ""); // expected-error {{constant expression}} expected-note {{in call}}
844 return ({ int n
; n
; }); // expected-note {{read of uninitialized object}}
846 static_assert(g() == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
848 // FIXME: We should handle the void statement expression case.
849 constexpr int h() { // expected-error {{never produces a constant}}
850 ({ if (true) {} }); // expected-note {{not supported}}
855 namespace VirtualFromBase
{
857 virtual int f() const;
862 template <typename T
> struct X
: T
{
865 constexpr int f() { return sizeof(T
); }
868 // Non-virtual f(), OK.
869 constexpr X
<X
<S1
>> xxs1
;
870 constexpr X
<S1
> *p
= const_cast<X
<X
<S1
>>*>(&xxs1
);
871 static_assert(p
->f() == sizeof(S1
), "");
873 // Virtual f(), not OK.
874 constexpr X
<X
<S2
>> xxs2
;
875 constexpr X
<S2
> *q
= const_cast<X
<X
<S2
>>*>(&xxs2
);
876 static_assert(q
->f() == sizeof(X
<S2
>), ""); // cxx14-error {{constant expression}} cxx14-note {{virtual function}}
880 constexpr int &get(int &&r
) { return r
; }
881 // cxx23-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
885 // cxx14_20-note@-1 {{read of object outside its lifetime}}
887 static_assert(f() == 123, ""); // expected-error {{constant expression}} cxx14_20-note {{in call}}
896 *p
= 123; // expected-note {{assignment to object outside its lifetime}}
899 static_assert(g() == 42, ""); // expected-error {{constant expression}} expected-note {{in call}}
901 constexpr int h(int n
) {
907 for (int b
= 1; int c
= 1; ) {
908 p
[2] = &b
, p
[3] = &c
;
913 *p
[n
] = 0; // expected-note 3{{assignment to object outside its lifetime}}
916 static_assert(h(0) == 0, ""); // ok, lifetime-extended
917 static_assert(h(1) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
918 static_assert(h(2) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
919 static_assert(h(3) == 0, ""); // expected-error {{constant expression}} expected-note {{in call}}
921 constexpr void lifetime_versus_loops() {
923 for (int i
= 0; i
!= 2; ++i
) {
928 // This modifies the 'n' from the previous iteration of the loop outside
930 ++*q
; // expected-note {{increment of object outside its lifetime}}
933 static_assert((lifetime_versus_loops(), true), ""); // expected-error {{constant expression}} expected-note {{in call}}
936 namespace Bitfields
{
942 constexpr bool test() {
948 return a
.b
== true && a
.n
== 3 && a
.u
== 31;
950 static_assert(test(), "");
956 constexpr A(int &&r
) : r(static_cast<int &&>(r
)) {}
957 constexpr A() : A(0) {
958 (void)+r
; // expected-note {{outside its lifetime}}
961 constexpr int k
= A().r
; // expected-error {{constant expression}} expected-note {{in call to}}
965 template<typename T
, unsigned int N
>
966 constexpr T
sum(const T (&arr
)[N
]) {
973 constexpr int ARR
[] = { 1, 2, 3, 4, 5 };
974 static_assert(sum(ARR
) == 15, "");
977 namespace EmptyClass
{
979 union E2
{} e2
; // expected-note 4{{here}}
980 struct E3
: E1
{} e3
;
983 constexpr int f(E
&a
, int kind
) {
985 case 0: { E
e(a
); return 0; } // expected-note {{read}} expected-note {{in call}}
986 case 1: { E
e(static_cast<E
&&>(a
)); return 0; } // expected-note {{read}} expected-note {{in call}}
987 case 2: { E e
; e
= a
; return 0; } // expected-note {{read}} expected-note {{in call}}
988 case 3: { E e
; e
= static_cast<E
&&>(a
); return 0; } // expected-note {{read}} expected-note {{in call}}
991 constexpr int test1
= f(e1
, 0);
992 constexpr int test2
= f(e2
, 0); // expected-error {{constant expression}} expected-note {{in call}}
993 constexpr int test3
= f(e3
, 0);
994 constexpr int test4
= f(e1
, 1);
995 constexpr int test5
= f(e2
, 1); // expected-error {{constant expression}} expected-note {{in call}}
996 constexpr int test6
= f(e3
, 1);
997 constexpr int test7
= f(e1
, 2);
998 constexpr int test8
= f(e2
, 2); // expected-error {{constant expression}} expected-note {{in call}}
999 constexpr int test9
= f(e3
, 2);
1000 constexpr int testa
= f(e1
, 3);
1001 constexpr int testb
= f(e2
, 3); // expected-error {{constant expression}} expected-note {{in call}}
1002 constexpr int testc
= f(e3
, 3);
1005 namespace SpeculativeEvalWrites
{
1006 // Ensure that we don't try to speculatively evaluate writes.
1010 // __builtin_object_size speculatively evaluates its first argument.
1011 __builtin_object_size((i
= 1, &a
), 0);
1015 static_assert(!f(), "");
1019 constexpr int f(int n
) {
1023 static_assert(f(0) == 1, "");
1026 namespace const_char
{
1028 constexpr int sum(const char (&Arr
)[N
]) {
1030 for (unsigned I
= 0; I
!= N
; ++I
)
1031 S
+= Arr
[I
]; // expected-note 2{{read of non-constexpr variable 'Cs' is not allowed}}
1035 // As an extension, we support evaluating some things that are `const` as though
1036 // they were `constexpr` when folding, but it should not be allowed in normal
1037 // constexpr evaluation.
1038 const char Cs
[] = {'a', 'b'}; // expected-note 2{{declared here}}
1039 void foo() __attribute__((enable_if(sum(Cs
) == 'a' + 'b', "")));
1040 void run() { foo(); }
1042 static_assert(sum(Cs
) == 'a' + 'b', ""); // expected-error{{not an integral constant expression}} expected-note{{in call to 'sum<2>(Cs)'}}
1043 constexpr int S
= sum(Cs
); // expected-error{{must be initialized by a constant expression}} expected-note{{in call}}
1046 constexpr void PR28739(int n
) { // expected-error {{never produces a constant}}
1047 int *p
= &n
; // expected-note {{array 'p' declared here}}
1048 p
+= (__int128
)(unsigned long)-1; // expected-note {{cannot refer to element 18446744073709551615 of non-array object in a constant expression}}
1049 // expected-warning@-1 {{the pointer incremented by 18446744073709551615 refers past the last possible element for an array in 64-bit address space containing 32-bit (4-byte) elements (max possible 4611686018427387904 elements)}}
1052 constexpr void Void(int n
) {
1056 constexpr int void_test
= (Void(0), 1);
1059 constexpr void addone(int &m
) { m
++; }
1063 constexpr S() { addone(m
); }
1065 constexpr bool evalS() {
1069 static_assert(evalS(), "");
1072 struct First
{ int x
= 42; };
1078 constexpr Nested(int x
) : first(), x(x
) { x
= 4; }
1079 constexpr Nested() : Nested(42) {
1084 constexpr bool evalNested() {
1086 return N
.first
.x
== 43;
1088 static_assert(evalNested(), "");
1089 } // namespace PR19741
1092 struct A
{ mutable int n
; }; // expected-note 2{{here}}
1093 constexpr int k
= A
{123}.n
; // ok
1094 static_assert(k
== 123, "");
1096 struct Q
{ A
&&a
; int b
= a
.n
; };
1097 constexpr Q q
= { A
{456} }; // expected-note {{temporary}}
1098 static_assert(q
.b
== 456, "");
1099 static_assert(q
.a
.n
== 456, ""); // expected-error {{constant expression}} expected-note {{outside the expression that created the temporary}}
1101 constexpr A a
= {123};
1102 constexpr int m
= a
.n
; // expected-error {{constant expression}} expected-note {{mutable}}
1104 constexpr Q r
= { static_cast<A
&&>(const_cast<A
&>(a
)) }; // expected-error {{constant expression}} expected-note@-8 {{mutable}}
1107 mutable int n
; // expected-note {{here}}
1109 constexpr B() : n(1), m(n
) {} // ok
1112 constexpr int p
= b
.n
; // expected-error {{constant expression}} expected-note {{mutable}}
1115 namespace IndirectFields
{
1117 // Reference indirect field.
1121 int x
= x
= 3; // cxx14-note {{outside its lifetime}}
1126 static_assert(A().x
== 3, ""); // cxx14-error{{not an integral constant expression}} cxx14-note{{in call to 'A()'}}
1128 // Reference another indirect field, with different 'this'.
1138 static_assert(B().y
== 3, "");
1140 // Nested evaluation of indirect field initializers.
1152 static_assert(D().y
== 2, "");
1162 constexpr E e1
= E();
1163 static_assert(e1
.x
!= e1
.y
, "");
1164 constexpr E e2
= E
{0};
1165 static_assert(e2
.x
!= e2
.y
, "");
1167 } // namespace IndirectFields
1169 constexpr bool indirect_builtin_constant_p(const char *__s
) {
1170 return __builtin_constant_p(*__s
);
1172 constexpr bool n
= indirect_builtin_constant_p("a");
1174 __attribute__((enable_if(indirect_builtin_constant_p("a") == n
, "OK")))
1175 int test_in_enable_if() { return 0; }
1176 int n2
= test_in_enable_if();
1178 template <bool n
= indirect_builtin_constant_p("a")>
1179 int test_in_template_param() { return 0; }
1180 int n3
= test_in_template_param();
1182 void test_in_case(int n
) {
1184 case indirect_builtin_constant_p("abc"):
1189 ONE
= indirect_builtin_constant_p("abc")
1191 enum InEnum2
: int {
1192 TWO
= indirect_builtin_constant_p("abc")
1194 enum class InEnum3
{
1195 THREE
= indirect_builtin_constant_p("abc")
1199 // A constructor can be invoked for a const, volatile or const volatile
1200 // object. const and volatile semantics are not applied on an object under
1201 // construction. They come into effect when the constructor for the most
1202 // derived object ends.
1203 namespace ObjectsUnderConstruction
{
1206 constexpr A() : n(1) { n
= 2; }
1210 constexpr B(bool mutate
) {
1212 const_cast<A
&>(a
).n
= 3; // expected-note {{modification of object of const-qualified type 'const int'}}
1215 constexpr B
b(false);
1216 static_assert(b
.a
.n
== 2, "");
1217 constexpr B
bad(true); // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'B(true)'}}
1221 constexpr C() : n(1) { n
= 2; }
1223 constexpr int f(bool get
) {
1224 volatile C c
; // expected-note {{here}}
1225 return get
? const_cast<int&>(c
.n
) : 0; // expected-note {{read of volatile object 'c'}}
1227 static_assert(f(false) == 0, ""); // ok, can modify volatile c.n during c's initialization: it's not volatile then
1228 static_assert(f(true) == 2, ""); // expected-error {{constant}} expected-note {{in call}}
1234 constexpr Aggregate aggr1
;
1235 static_assert(aggr1
.x
== 1 && aggr1
.y
== 1, "");
1236 // FIXME: This is not specified by the standard, but sanity requires it.
1237 constexpr Aggregate aggr2
= {};
1238 static_assert(aggr2
.x
== 1 && aggr2
.y
== 1, "");
1240 // The lifetime of 'n' begins at the initialization, not before.
1241 constexpr int n
= ++const_cast<int&>(n
); // expected-error {{constant expression}} expected-note {{increment of object outside its lifetime}}
1246 Comment0
&operator=(const Comment0
&) = default;
1247 ~Comment0() = default;
1249 constexpr void f() {
1253 static_assert((f(), true), "");
1255 constexpr Comment1
&operator=(const Comment1
&) = default; // OK
1256 ~Comment1() = default;
1260 namespace TemporaryWithBadPointer
{
1261 constexpr int *get_bad_pointer() {
1262 int n
= 0; // expected-note 2{{here}}
1263 return &n
; // expected-warning {{stack}}
1265 constexpr int *bad_pointer
= get_bad_pointer(); // expected-error {{constant expression}} expected-note {{pointer to 'n' is not a constant expression}}
1267 struct DoBadThings
{ int *&&wp
; int n
; };
1268 constexpr DoBadThings dbt
= { // expected-error {{constant expression}}
1269 nullptr, // expected-note {{pointer to 'n' is not a constant expression}}
1270 (dbt
.wp
= get_bad_pointer(), 0)
1273 constexpr DoBadThings dbt2
= { // ok
1275 (dbt2
.wp
= nullptr, 0)
1279 namespace UninitCompoundAssign
{
1280 constexpr int scalar(int a
) {
1281 int sum
; // cxx14-warning {{uninitialized variable in a constexpr function is a C++20 extension}}
1282 sum
+= a
; // expected-note {{read of uninitialized object}};
1285 static_assert(scalar(3), ""); // expected-error {{constant expression}} \
1286 // expected-note {{in call to 'scalar(3)'}}
1288 constexpr int array(int a
) {
1289 int arr
[3]; // cxx14-warning {{uninitialized variable in a constexpr function is a C++20 extension}}
1290 arr
[1] += a
; // expected-note {{read of uninitialized object}};
1293 static_assert(array(3), ""); // expected-error {{constant expression}} \
1294 // expected-note {{in call to 'array(3)'}}
1297 int val
; // cxx14-note{{member not initialized by constructor}}
1298 constexpr Foo() {} // cxx14-warning {{constexpr constructor that does not initialize all members is a C++20 extension}}
1300 constexpr int field(int a
) {
1302 f
.val
+= a
; // expected-note {{read of uninitialized object}};
1305 static_assert(field(3), ""); // expected-error {{constant expression}} \
1306 // expected-note {{in call to 'field(3)'}}