1 // RUN: %clang_cc1 -std=c++2a -emit-llvm-only -Wno-unused-value -Wno-vla %s -verify
3 typedef __SIZE_TYPE__
size_t;
7 consteval
int f1(int i
) {
11 consteval
constexpr int f2(int i
) {
12 //expected-error@-1 {{cannot combine}}
16 constexpr auto l_eval
= [](int i
) consteval
{
17 // expected-note@-1+ {{declared here}}
22 constexpr consteval
int f3(int i
) {
23 //expected-error@-1 {{cannot combine}}
28 consteval
int f1(int i
) const {
29 // expected-note@-1 {{declared here}}
33 consteval
A() = default;
34 consteval
~A() = default; // expected-error {{destructor cannot be declared consteval}}
37 consteval
struct B
{}; // expected-error {{struct cannot be marked consteval}}
39 consteval
typedef B b
; // expected-error {{typedef cannot be consteval}}
41 consteval
int redecl() {return 0;} // expected-note {{previous declaration is here}}
42 constexpr int redecl() {return 0;} // expected-error {{constexpr declaration of 'redecl' follows consteval declaration}}
44 consteval
int i
= 0; // expected-error {{consteval can only be used in function declarations}}
46 consteval
int; // expected-error {{consteval can only be used in function declarations}}
48 consteval
int f1() {} // expected-error {{no return statement in consteval function}}
57 consteval
D() = default; // expected-error {{cannot be marked consteval}}
58 consteval
~D() = default; // expected-error {{destructor cannot be declared consteval}}
62 consteval
~E() {} // expected-error {{cannot be declared consteval}}
66 consteval
int main() { // expected-error {{'main' is not allowed to be declared consteval}}
70 consteval
int f_eval(int i
) {
71 // expected-note@-1+ {{declared here}}
75 namespace taking_address
{
77 using func_type
= int(int);
79 func_type
* p1
= (&f_eval
);
80 // expected-error@-1 {{take address}}
81 func_type
* p7
= __builtin_addressof(f_eval
);
82 // expected-error@-1 {{take address}}
85 // expected-error@-1 {{take address}}
87 auto m1
= &basic_sema::A::f1
;
88 // expected-error@-1 {{take address}}
89 auto l1
= &decltype(basic_sema::l_eval
)::operator();
90 // expected-error@-1 {{take address}}
92 consteval
int f(int i
) {
93 // expected-note@-1+ {{declared here}}
98 // expected-error@-1 {{take address}}
102 // expected-error@-1 {{take address}}
107 namespace invalid_function
{
110 consteval
void *operator new(size_t count
);
111 // expected-error@-1 {{'operator new' cannot be declared consteval}}
112 consteval
void *operator new[](size_t count
);
113 // expected-error@-1 {{'operator new[]' cannot be declared consteval}}
114 consteval
void operator delete(void* ptr
);
115 // expected-error@-1 {{'operator delete' cannot be declared consteval}}
116 consteval
void operator delete[](void* ptr
);
117 // expected-error@-1 {{'operator delete[]' cannot be declared consteval}}
119 // expected-error@-1 {{destructor cannot be declared consteval}}
129 consteval
int f1(...) {
137 consteval
auto operator+ (E
, int(*a
)()) {
142 auto i
= f1(E() + &f
);
145 auto l0
= [](auto) consteval
{
153 int i2
= f1(&f1
, &f1
, &f1
, &f1
, &f1
, &f1
, &f1
);
155 int i3
= f1(f1(f1(&f1
, &f1
), f1(&f1
, &f1
), f1(f1(&f1
, &f1
), &f1
)));
159 namespace user_defined_literal
{
161 consteval
int operator""_test(unsigned long long i
) {
162 // expected-note@-1+ {{declared here}}
168 auto ptr
= &operator""_test
;
169 // expected-error@-1 {{take address}}
171 consteval
auto operator""_test1(unsigned long long i
) {
175 auto i1
= 0_test1
; // expected-error {{is not a constant expression}}
176 // expected-note@-1 {{is not a constant expression}}
180 namespace return_address
{
183 // expected-note@-1 {{declared here}}
187 consteval
int(*ret1(int i
))() {
192 // expected-error@-1 {{is not a constant expression}}
193 // expected-note@-2 {{pointer to a consteval}}
196 consteval
int f(int) {
197 // expected-note@-1+ {{declared here}}
202 using mem_ptr_type
= int (A::*)(int);
204 template<mem_ptr_type ptr
>
208 // expected-error@-1 {{is not a constant expression}}
209 // expected-note@-2 {{pointer to a consteval}}
211 consteval mem_ptr_type
ret2() {
216 // expected-error@-1 {{is not a constant expression}}
217 // expected-note@-2 {{pointer to a consteval}}
224 // expected-note@-1 {{declared here}}
226 consteval
int f(int) {
230 constexpr int c_i
= 0;
233 // expected-error@-1 {{is not a constant expression}}
234 // expected-note@-2 {{read of non-const variable}}
237 constexpr int f_c(int i
) {
238 // expected-note@-1 {{declared here}}
240 // expected-error@-1 {{is not a constant expression}}
241 // expected-note@-2 {{function parameter}}
245 consteval
int f_eval(int i
) {
249 auto l0
= [](int i
) consteval
{
253 auto l1
= [](int i
) constexpr { // expected-error{{cannot take address of immediate call operator}} \
254 // expected-note {{declared here}}
259 int(*test
)(int) = l1
;
263 namespace consteval_lambda_in_template
{
266 constexpr S(int v
) : value(new int {v
}) {}
267 constexpr ~S() { delete value
; }
269 consteval S
fn() { return S(5); }
271 template <typename T
>
273 (void)[]() consteval
-> int {
274 return *(fn().value
); // OK, immediate context
285 template <typename T
> struct remove_reference
{ using type
= T
; };
286 template <typename T
> struct remove_reference
<T
&> { using type
= T
; };
287 template <typename T
> struct remove_reference
<T
&&> { using type
= T
; };
289 template <typename T
>
290 constexpr typename
std::remove_reference
<T
>::type
&& move(T
&&t
) noexcept
{
291 return static_cast<typename
std::remove_reference
<T
>::type
&&>(t
);
296 namespace temporaries
{
299 consteval
int ret_i() const { return 0; }
300 consteval A
ret_a() const { return A
{}; }
304 consteval
int by_value_a(A a
) { return a
.ret_i(); }
306 consteval
int const_a_ref(const A
&a
) {
310 consteval
int rvalue_ref(const A
&&a
) {
314 consteval
const A
&to_lvalue_ref(const A
&&a
) {
320 { int k
= A().ret_i(); }
321 { A k
= A().ret_a(); }
322 { A k
= to_lvalue_ref(A()); }// expected-error {{is not a constant expression}}
323 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
324 { A k
= to_lvalue_ref(A().ret_a()); } // expected-error {{is not a constant expression}}
325 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
326 { int k
= A().ret_a().ret_i(); }
327 { int k
= by_value_a(A()); }
328 { int k
= const_a_ref(A()); }
329 { int k
= const_a_ref(a
); }
330 { int k
= rvalue_ref(A()); }
331 { int k
= rvalue_ref(std::move(a
)); }
332 { int k
= const_a_ref(A().ret_a()); }
333 { int k
= const_a_ref(to_lvalue_ref(A().ret_a())); }
334 { int k
= const_a_ref(to_lvalue_ref(std::move(a
))); }
335 { int k
= by_value_a(A().ret_a()); }
336 { int k
= by_value_a(to_lvalue_ref(std::move(a
))); }
337 { int k
= (A().ret_a(), A().ret_i()); }
338 { int k
= (const_a_ref(A().ret_a()), A().ret_i()); }//
347 // expected-note@-1+ {{allocation performed here was not deallocated}}
351 int i1
= f(); // expected-error {{is not a constant expression}}
354 int* p
= new int(42);
355 // expected-note@-1+ {{heap allocation performed here}}
356 consteval
int ret_i() const { return p
? *p
: 0; }
357 consteval A
ret_a() const { return A
{}; }
358 constexpr ~A() { delete p
; }
361 consteval
int by_value_a(A a
) { return a
.ret_i(); }
363 consteval
int const_a_ref(const A
&a
) {
367 consteval
int rvalue_ref(const A
&&a
) {
371 consteval
const A
&to_lvalue_ref(const A
&&a
) {
376 constexpr A a
{ nullptr };
377 { int k
= A().ret_i(); }
378 { A k
= A().ret_a(); } // expected-error {{is not a constant expression}}
379 // expected-note@-1 {{is not a constant expression}}
380 { A k
= to_lvalue_ref(A()); } // expected-error {{is not a constant expression}}
381 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
382 { A k
= to_lvalue_ref(A().ret_a()); }
383 // expected-note@-1 {{reference to temporary is not a constant expression}}
384 // expected-error@-2 {{'alloc::to_lvalue_ref' is not a constant expression}}
385 // expected-note@-3 {{temporary created here}}
386 { int k
= A().ret_a().ret_i(); }
387 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
388 // expected-note@-2 {{heap-allocated object is not a constant expression}}
389 { int k
= by_value_a(A()); }
390 { int k
= const_a_ref(A()); }
391 { int k
= const_a_ref(a
); }
392 { int k
= rvalue_ref(A()); }
393 { int k
= rvalue_ref(std::move(a
)); }
394 { int k
= const_a_ref(A().ret_a()); }
395 { int k
= const_a_ref(to_lvalue_ref(A().ret_a())); }
396 { int k
= const_a_ref(to_lvalue_ref(std::move(a
))); }
397 { int k
= by_value_a(A().ret_a()); }
398 { int k
= by_value_a(to_lvalue_ref(static_cast<const A
&&>(a
))); }
399 { int k
= (A().ret_a(), A().ret_i()); }// expected-error {{is not a constant expression}}
400 // expected-note@-1 {{is not a constant expression}}
401 { int k
= (const_a_ref(A().ret_a()), A().ret_i()); }
406 namespace self_referencing
{
410 constexpr S(int i
) : ptr(this) {
411 if (this == ptr
&& i
)
417 consteval S
f(int i
) {
424 s
= f(0); // expected-error {{is not a constant expression}}
425 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
430 consteval
S1(int i
) : ptr(this) {
431 if (this == ptr
&& i
)
440 s
= S1(0); // expected-error {{is not a constant expression}}
441 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
447 consteval
int f_eval() { // expected-note+ {{declared here}}
452 struct strong_ordering
{
454 static const strong_ordering less
, equal
, greater
;
456 constexpr strong_ordering
strong_ordering::less
= {-1};
457 constexpr strong_ordering
strong_ordering::equal
= {0};
458 constexpr strong_ordering
strong_ordering::greater
= {1};
459 constexpr bool operator!=(strong_ordering
, int);
464 virtual consteval
void f(); // expected-note {{overridden}}
465 virtual void g(); // expected-note {{overridden}}
472 void f(); // expected-error {{non-consteval function 'f' cannot override a consteval function}}
473 consteval
void g(); // expected-error {{consteval function 'g' cannot override a non-consteval function}}
476 namespace implicit_equals_1
{
479 std::strong_ordering
operator<=>(const X
&) const;
480 constexpr bool operator==(const X
&) const;
481 virtual consteval
bool operator==(const Y
&) const; // expected-note {{here}}
484 std::strong_ordering
operator<=>(const Y
&) const = default;
485 // expected-error@-1 {{non-consteval function 'operator==' cannot override a consteval function}}
489 namespace implicit_equals_2
{
492 constexpr std::strong_ordering
operator<=>(const X
&) const;
493 constexpr bool operator==(const X
&) const;
494 virtual bool operator==(const Y
&) const; // expected-note {{here}}
497 consteval
std::strong_ordering
operator<=>(const Y
&) const = default;
498 // expected-error@-1 {{consteval function 'operator==' cannot override a non-consteval function}}
503 namespace operator_rewrite
{
505 friend consteval
int operator<=>(const A
&, const A
&) { return 0; }
507 const bool k
= A() < A();
511 bool k2
= A() < a
; // OK, does not access 'a'.
514 friend consteval
int operator<=>(const B
&l
, const B
&r
) { return r
.n
- l
.n
; } // expected-note {{read of }}
517 static_assert(B() >= B());
518 B b
; // expected-note {{here}}
519 bool k3
= B() < b
; // expected-error-re {{call to consteval function '{{.*}}::operator<=>' is not a constant expression}} expected-note {{in call}}
524 consteval
A(int(*p
)() = nullptr) : ptr(p
) {}
529 B() : ptr(nullptr) {}
530 consteval
B(int(*p
)(), int) : ptr(p
) {}
535 { A
a(&f_eval
); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
537 { B
b(&f_eval
, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
539 { A a
{&f_eval
}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
541 { B b
{&f_eval
, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
543 { A a
= A(&f_eval
); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
544 { B b
= B(nullptr, 0); }
545 { B b
= B(&f_eval
, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
547 { A a
= A
{&f_eval
}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
548 { B b
= B
{nullptr, 0}; }
549 { B b
= B
{&f_eval
, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
551 { A a
; a
= A(&f_eval
); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
552 { B b
; b
= B(nullptr, 0); }
553 { B b
; b
= B(&f_eval
, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
555 { A a
; a
= A
{&f_eval
}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
556 { B b
; b
= B
{nullptr, 0}; }
557 { B b
; b
= B
{&f_eval
, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
558 { A
* a
; a
= new A(); }
559 { A
* a
; a
= new A(&f_eval
); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
560 { B
* b
; b
= new B(nullptr, 0); }
561 { B
* b
; b
= new B(&f_eval
, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
562 { A
* a
; a
= new A
{}; }
563 { A
* a
; a
= new A
{&f_eval
}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
564 { B
* b
; b
= new B
{nullptr, 0}; }
565 { B
* b
; b
= new B
{&f_eval
, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
570 namespace copy_ctor
{
572 consteval
int f_eval() { // expected-note+ {{declared here}}
578 constexpr Copy(int(*p
)() = nullptr) : ptr(p
) {}
579 consteval
Copy(const Copy
&) = default;
582 constexpr const Copy
&to_lvalue_ref(const Copy
&&a
) {
587 constexpr const Copy C
;
588 // there is no the copy constructor call when its argument is a prvalue because of garanteed copy elision.
589 // so we need to test with both prvalue and xvalues.
591 { Copy
c((Copy(&f_eval
))); }// expected-error {{cannot take address of consteval}}
592 { Copy
c(std::move(C
)); }
593 { Copy
c(std::move(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
594 { Copy
c(to_lvalue_ref((Copy(&f_eval
)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
595 { Copy
c(to_lvalue_ref(std::move(C
))); }
596 { Copy
c(to_lvalue_ref(std::move(Copy(&f_eval
)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
597 { Copy c
= Copy(C
); }
598 { Copy c
= Copy(Copy(&f_eval
)); }// expected-error {{cannot take address of consteval}}
599 { Copy c
= Copy(std::move(C
)); }
600 { Copy c
= Copy(std::move(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
601 { Copy c
= Copy(to_lvalue_ref(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
602 { Copy c
= Copy(to_lvalue_ref(std::move(C
))); }
603 { Copy c
= Copy(to_lvalue_ref(std::move(Copy(&f_eval
)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
604 { Copy c
; c
= Copy(C
); }
605 { Copy c
; c
= Copy(Copy(&f_eval
)); }// expected-error {{cannot take address of consteval}}
606 { Copy c
; c
= Copy(std::move(C
)); }
607 { Copy c
; c
= Copy(std::move(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
608 { Copy c
; c
= Copy(to_lvalue_ref(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
609 { Copy c
; c
= Copy(to_lvalue_ref(std::move(C
))); }
610 { Copy c
; c
= Copy(to_lvalue_ref(std::move(Copy(&f_eval
)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
611 { Copy
* c
; c
= new Copy(C
); }
612 { Copy
* c
; c
= new Copy(Copy(&f_eval
)); }// expected-error {{cannot take address of consteval}}
613 { Copy
* c
; c
= new Copy(std::move(C
)); }
614 { Copy
* c
; c
= new Copy(std::move(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
615 { Copy
* c
; c
= new Copy(to_lvalue_ref(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
616 { Copy
* c
; c
= new Copy(to_lvalue_ref(std::move(C
))); }
617 { Copy
* c
; c
= new Copy(to_lvalue_ref(std::move(Copy(&f_eval
)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
620 } // namespace special_ctor
622 namespace unevaluated
{
624 template <typename T
, typename U
> struct is_same
{ static const bool value
= false; };
625 template <typename T
> struct is_same
<T
, T
> { static const bool value
= true; };
627 long f(); // expected-note {{declared here}}
628 auto consteval
g(auto a
) {
632 auto e
= g(f()); // expected-error {{is not a constant expression}}
633 // expected-note@-1 {{non-constexpr function 'f' cannot be used in a constant expression}}
635 using T
= decltype(g(f()));
636 static_assert(is_same
<long, T
>::value
);
638 } // namespace unevaluated
640 namespace value_dependent
{
642 consteval
int foo(int x
) {
646 template <int X
> constexpr int bar() {
647 // Previously this call was rejected as value-dependent constant expressions
648 // can't be immediately evaluated. Now we show that we don't immediately
649 // evaluate them until they are instantiated.
653 template <typename T
> constexpr int baz() {
654 constexpr int t
= sizeof(T
);
655 // Previously this call was rejected as `t` is value-dependent and its value
656 // is unknown until the function is instantiated. Now we show that we don't
657 // reject such calls.
661 static_assert(bar
<15>() == 15);
662 static_assert(baz
<int>() == sizeof(int));
663 } // namespace value_dependent
665 // https://github.com/llvm/llvm-project/issues/55601
666 namespace issue_55601
{
669 consteval
static T
x() { return 5; } // expected-note {{non-constexpr constructor 'derp' cannot be used in a constant expression}}
671 Bar() : a(x()) {} // expected-error {{call to consteval function 'issue_55601::Bar<issue_55601::derp>::x' is not a constant expression}}
672 // expected-error@-1 {{call to consteval function 'issue_55601::derp::operator int' is not a constant expression}}
673 // expected-note@-2 {{in call to 'x()'}}
674 // expected-note@-3 {{non-literal type 'issue_55601::derp' cannot be used in a constant expression}}
682 // Can't be used in a constant expression
683 derp(int); // expected-note {{declared here}}
684 consteval
operator int() const { return 5; }
686 Bar
<derp
> a
; // expected-note {{in instantiation of member function 'issue_55601::Bar<issue_55601::derp>::Bar' requested here}}
688 struct constantDerp
{
689 // Can be used in a constant expression.
690 consteval
constantDerp(int) {}
691 consteval
operator int() const { return 5; }
695 } // namespace issue_55601
697 namespace default_argument
{
699 // Previously calls of consteval functions in default arguments were rejected.
700 // Now we show that we don't reject such calls.
701 consteval
int foo() { return 1; }
702 consteval
int bar(int i
= foo()) { return i
* i
; }
705 Test1(int i
= bar(13)) {}
706 void v(int i
= bar(13) * 2 + bar(15)) {}
711 constexpr Test2(int i
= bar()) {}
712 constexpr void v(int i
= bar(bar(bar(foo())))) {}
716 } // namespace default_argument
726 consteval
int operator[](int i
) const { return {}; }
727 consteval
const derp
* operator->() const { return &d
; }
728 consteval
int f() const { return 12; } // expected-note 2{{declared here}}
733 // We previously rejected both of these overloaded operators as taking the
734 // address of a consteval function outside of an immediate context, but we
735 // accepted direct calls to the overloaded operator. Now we show that we accept
737 constexpr int s
= a
.operator[](1);
738 constexpr int t
= a
[1];
739 constexpr int u
= a
.operator->()->b
;
740 constexpr int v
= a
->b
;
741 // FIXME: I believe this case should work, but we currently reject.
742 constexpr int w
= (a
.*&test::f
)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}}
743 constexpr int x
= a
.f();
745 // Show that we reject when not in an immediate context.
746 int w2
= (a
.*&test::f
)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}}
755 consteval
int a() const { return 1; }
758 this->a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \
759 // expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
763 a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \
764 // expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
769 namespace NamespaceScopeConsteval
{
771 int Val
; // expected-note {{subobject declared here}}
775 S s1
; // expected-error {{call to consteval function 'NamespaceScopeConsteval::S::S' is not a constant expression}} \
776 expected
-note
{{subobject
'Val' is
not initialized
}}
778 template <typename Ty
>
780 Ty Val
; // expected-note {{subobject declared here}}
784 T
<int> t
; // expected-error {{call to consteval function 'NamespaceScopeConsteval::T<int>::T' is not a constant expression}} \
785 expected
-note
{{subobject
'Val' is
not initialized
}}
787 } // namespace NamespaceScopeConsteval
789 namespace Issue54578
{
790 // We expect the user-defined literal to be resovled entirely at compile time
791 // despite being instantiated through a template.
792 inline consteval
unsigned char operator""_UC(const unsigned long long n
) {
793 return static_cast<unsigned char>(n
);
796 inline constexpr char f1(const auto octet
) {
800 template <typename Ty
>
801 inline constexpr char f2(const Ty octet
) {
806 static_assert(f1('a') == 4);
807 static_assert(f2('a') == 4);
808 constexpr int c
= f1('a') + f2('a');
809 static_assert(c
== 8);
813 namespace defaulted_special_member_template
{
814 template <typename T
>
815 struct default_ctor
{
817 consteval
default_ctor() = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}}
820 template <typename T
>
824 consteval
copy(const copy
&) = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}}
825 consteval copy
&operator=(const copy
&) = default; // expected-note {{non-constexpr function 'operator=' cannot be used in a constant expression}}
829 template <typename T
>
833 consteval
move(move
&&) = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}}
834 consteval move
&operator=(move
&&) = default; // expected-note {{non-constexpr function 'operator=' cannot be used in a constant expression}}
839 foo() {} // expected-note {{declared here}}
840 foo(const foo
&) {} // expected-note {{declared here}}
841 foo(foo
&&) {} // expected-note {{declared here}}
843 foo
& operator=(const foo
&) { return *this; } // expected-note {{declared here}}
844 foo
& operator=(foo
&&) { return *this; } // expected-note {{declared here}}
848 default_ctor
<foo
> fail0
; // expected-error {{call to consteval function 'defaulted_special_member_template::default_ctor<defaulted_special_member_template::foo>::default_ctor' is not a constant expression}} \
849 expected
-note
{{in call to
'default_ctor()'}}
852 copy
<foo
> fail1
{good0
}; // expected-error {{call to consteval function 'defaulted_special_member_template::copy<defaulted_special_member_template::foo>::copy' is not a constant expression}} \
853 expected
-note
{{in call to
'copy(good0)'}}
854 fail1
= good0
; // expected-error {{call to consteval function 'defaulted_special_member_template::copy<defaulted_special_member_template::foo>::operator=' is not a constant expression}} \
855 expected
-note
{{in call to
'fail1.operator=(good0)'}}
858 move
<foo
> fail2
{static_cast<move
<foo
>&&>(good1
)}; // expected-error {{call to consteval function 'defaulted_special_member_template::move<defaulted_special_member_template::foo>::move' is not a constant expression}} \
859 expected
-note
{{in call to
'move(good1)'}}
860 fail2
= static_cast<move
<foo
>&&>(good1
); // expected-error {{call to consteval function 'defaulted_special_member_template::move<defaulted_special_member_template::foo>::operator=' is not a constant expression}} \
861 expected
-note
{{in call to
'fail2.operator=(good1)'}}
863 } // namespace defaulted_special_member_template
865 namespace multiple_default_constructors
{
867 Foo() {} // expected-note {{declared here}}
876 template <typename T
, unsigned N
>
879 S() requires (N
==1) = default;
880 // This cannot be used in constexpr context.
881 S() requires (N
==2) {} // expected-note {{declared here}}
882 consteval
S() requires (N
==3) = default; // expected-note {{non-constexpr constructor 'Foo' cannot be used in a constant expression}}
886 // Explicitly defaulted constructor.
889 // User provided constructor.
892 // Consteval explicitly defaulted constructor.
893 S
<Foo
, 3> s5
; // expected-error {{call to consteval function 'multiple_default_constructors::S<multiple_default_constructors::Foo, 3>::S' is not a constant expression}} \
894 expected
-note
{{in call to
'S()'}}
899 consteval
int aConstevalFunction() { // expected-error {{consteval function never produces a constant expression}}
900 // Defaulted default constructors are implicitly consteval.
903 S
<Baz
, 2> s4
; // expected-note {{non-constexpr constructor 'S' cannot be used in a constant expression}}
910 } // namespace multiple_default_constructors
914 consteval
int testDefaultArgForParam(E eParam
= (E
)-1) {
915 // expected-note@-1 {{integer value -1 is outside the valid range of values [0, 1] for the enumeration type 'E'}}
920 return testDefaultArgForParam() + testDefaultArgForParam((E
)1);
921 // expected-error@-1 {{call to consteval function 'GH50055::testDefaultArgForParam' is not a constant expression}}
926 // Nested consteval function.
927 consteval
int f(int v
) {
931 template <typename T
>
932 consteval
int g(T a
) {
933 // An immediate function context.
937 static_assert(g(100) == 100);
938 // --------------------------------------
939 template <typename T
>
940 consteval T
max(const T
& a
, const T
& b
) {
941 return (a
> b
) ? a
: b
;
943 template <typename T
>
944 consteval T
mid(const T
& a
, const T
& b
, const T
& c
) {
945 T m
= max(max(a
, b
), c
);
952 static_assert(max(1,2)==2);
953 static_assert(mid(1,2,3)==2);
954 } // namespace GH51182
956 // https://github.com/llvm/llvm-project/issues/56183
958 consteval
auto Foo(auto c
) { return c
; }
959 consteval
auto Bar(auto f
) { return f(); }
961 constexpr auto x
= Foo(Bar([] { return 'a'; }));
962 static_assert(x
== 'a');
964 } // namespace GH56183
966 // https://github.com/llvm/llvm-project/issues/51695
968 // Original ========================================
969 template <typename T
>
972 template <typename
...>
975 template <typename T
, typename
... Ts
>
976 consteval
auto pop_front(list_t
<T
, Ts
...>) -> auto {
977 return list_t
<Ts
...>{};
980 template <typename
... Ts
, typename F
>
981 consteval
auto apply(list_t
<Ts
...>, F fn
) -> auto {
982 return fn(type_t
<Ts
>{}...);
986 constexpr auto x
= apply(pop_front(list_t
<char, char>{}),
987 []<typename
... Us
>(type_t
<Us
>...) { return 42; });
988 static_assert(x
== 42);
990 // Reduced 1 ========================================
991 consteval
bool zero() { return false; }
993 template <typename F
>
994 consteval
bool foo(bool, F f
) {
999 constexpr auto x
= foo(zero(), []() { return true; });
1003 // Reduced 2 ========================================
1004 template <typename F
>
1005 consteval
auto bar(F f
) { return f
;}
1008 constexpr auto t1
= bar(bar(bar(bar([]() { return true; }))))();
1011 int a
= 1; // expected-note {{declared here}}
1012 auto t2
= bar(bar(bar(bar([=]() { return a
; }))))(); // expected-error-re {{call to consteval function 'GH51695::bar<(lambda at {{.*}})>' is not a constant expression}}
1013 // expected-note@-1 {{read of non-const variable 'a' is not allowed in a constant expression}}
1015 constexpr auto t3
= bar(bar([x
=bar(42)]() { return x
; }))();
1016 static_assert(t3
==42);
1017 constexpr auto t4
= bar(bar([x
=bar(42)]() consteval
{ return x
; }))();
1018 static_assert(t4
==42);
1021 } // namespace GH51695
1023 // https://github.com/llvm/llvm-project/issues/50455
1026 []() consteval
{ int i
{}; }();
1027 []() consteval
{ int i
{}; ++i
; }();
1030 (void)[](int i
) consteval
{ return i
; }(0);
1031 (void)[](int i
) consteval
{ return i
; }(0);
1033 } // namespace GH50455
1038 consteval
operator int() { return 1;}
1048 constexpr auto l1
= []() consteval
{ // expected-error {{cannot take address of consteval call operator of '(lambda at}} \
1049 // expected-note 2{{declared here}}
1052 constexpr int (*f1
)(void) = l1
; // expected-error {{constexpr variable 'f1' must be initialized by a constant expression}} \
1053 // expected-note {{pointer to a consteval declaration is not a constant expression}}
1056 constexpr auto lstatic
= []() static consteval
{ // expected-error {{cannot take address of consteval call operator of '(lambda at}} \
1057 // expected-note 2{{declared here}} \
1058 // expected-warning {{extension}}
1061 constexpr int (*f2
)(void) = lstatic
; // expected-error {{constexpr variable 'f2' must be initialized by a constant expression}} \
1062 // expected-note {{pointer to a consteval declaration is not a constant expression}}
1064 int (*f3
)(void) = []() consteval
{ return 3; }; // expected-error {{cannot take address of consteval call operator of '(lambda at}} \
1065 // expected-note {{declared here}}
1068 consteval
void consteval_test() {
1069 constexpr auto l1
= []() consteval
{ return 3; };
1071 int (*f1
)(void) = l1
; // ok
1081 A(const A
&) { i
= 1; }
1082 consteval
int f() { return i
; }
1085 constexpr auto B
= A
{A
{}}.f();
1086 static_assert(B
== 0);
1092 consteval
tester(const char* name
) noexcept
{ }
1094 consteval
const char* make_name(const char* name
) { return name
;}
1095 consteval
const char* pad(int P
) { return "thestring"; }
1097 int bad
= 10; // expected-note 6{{declared here}}
1099 tester
glob1(make_name("glob1"));
1100 tester
glob2(make_name("glob2"));
1101 constexpr tester
cglob(make_name("cglob"));
1102 tester
paddedglob(make_name(pad(bad
))); // expected-error {{call to consteval function 'GH58207::tester::tester' is not a constant expression}} \
1103 // expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}}
1105 constexpr tester glob3
= { make_name("glob3") };
1106 constexpr tester glob4
= { make_name(pad(bad
)) }; // expected-error {{call to consteval function 'GH58207::tester::tester' is not a constant expression}} \
1107 // expected-error {{constexpr variable 'glob4' must be initialized by a constant expression}} \
1108 // expected-note 2{{read of non-const variable 'bad' is not allowed in a constant expression}}
1110 auto V
= make_name(pad(3));
1111 auto V1
= make_name(pad(bad
)); // expected-error {{call to consteval function 'GH58207::make_name' is not a constant expression}} \
1112 // expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}}
1116 static tester
loc1(make_name("loc1"));
1117 static constexpr tester
loc2(make_name("loc2"));
1118 static tester
paddedloc(make_name(pad(bad
))); // expected-error {{call to consteval function 'GH58207::tester::tester' is not a constant expression}} \
1119 // expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}}
1123 static tester
paddedloc(make_name(pad(bad
))); // expected-error {{call to consteval function 'GH58207::tester::tester' is not a constant expression}} \
1124 // expected-note {{read of non-const variable 'bad' is not allowed in a constant expression}}
1130 int g
; // expected-note 2{{subobject declared here}}
1136 consteval
h(char *) {}
1137 consteval
operator int() const { return 1; }
1141 void test() { (int)h
{nullptr}; }
1142 // expected-error@-1 {{call to consteval function 'GH64949::h::h' is not a constant expression}}
1143 // expected-note@-2 {{subobject 'g' is not initialized}}
1145 int test2() { return h
{nullptr}; }
1146 // expected-error@-1 {{call to consteval function 'GH64949::h::h' is not a constant expression}}
1147 // expected-note@-2 {{subobject 'g' is not initialized}}
1154 int consteval
operator""_foo(unsigned long long V
) {
1157 int consteval
operator""_bar(unsigned long long V
); // expected-note 3{{here}}
1163 int consteval
g(); // expected-note {{here}}
1167 static const int a
= 1_foo
;
1168 static constexpr int b
= 1_foo
;
1169 static const int c
= 1_bar
; // expected-error {{call to consteval function 'GH65985::operator""_bar' is not a constant expression}} \
1170 // expected-note {{undefined function 'operator""_bar' cannot be used in a constant expression}} \
1171 // expected-error {{in-class initializer for static data member is not a constant expression}}
1173 // FIXME: remove duplicate diagnostics
1174 static constexpr int d
= 1_bar
; // expected-error {{call to consteval function 'GH65985::operator""_bar' is not a constant expression}} \
1175 // expected-note {{undefined function 'operator""_bar' cannot be used in a constant expression}} \
1176 // expected-error {{constexpr variable 'd' must be initialized by a constant expression}} \
1177 // expected-note {{undefined function 'operator""_bar' cannot be used in a constant expression}}
1179 static const int e
= f();
1180 static const int f
= g(); // expected-error {{call to consteval function 'GH65985::g' is not a constant expression}} \
1181 // expected-error {{in-class initializer for static data member is not a constant expression}} \
1182 // expected-note {{undefined function 'g' cannot be used in a constant expression}}
1191 consteval
int foo(int x
) { return 1; } // expected-note {{declared here}} \
1192 // expected-note {{passing argument to parameter 'x' here}}
1197 static constexpr auto xx
= ns::foo(A
{}); // expected-error {{cannot take address of consteval function 'foo' outside of an immediate invocation}} \
1198 // expected-error {{cannot initialize a parameter of type 'int' with an rvalue of type 'char *'}}
1201 template class T
<char*>; // expected-note {{in instantiation}}
1207 consteval
int bar (int i
) { if (i
!= 1) return 1/0; return 0; }
1208 // expected-note@-1{{division by zero}}
1214 int a_err
[bar(3)]; // expected-error {{call to consteval function 'GH65520::bar' is not a constant expression}} \
1215 // expected-note {{in call to 'bar(3)'}}
1218 consteval
int undefined(); // expected-note {{declared here}}
1220 consteval
void immediate() {
1221 int a
[undefined()]; // expected-note {{undefined function 'undefined' cannot be used in a constant expression}} \
1222 // expected-error {{call to consteval function 'GH65520::undefined' is not a constant expression}} \
1223 // expected-error {{variable of non-literal type 'int[undefined()]' cannot be defined in a constexpr function before C++23}}
1229 namespace GH105558
{
1231 consteval
int* alloc() { return new int(0); }
1232 consteval
void f(int* p
) { delete p
; }
1233 consteval
void g1(int*&& p
) { delete p
; }
1234 consteval
void g2(const int* p
) { delete p
; }
1235 consteval
void g3(int*const& p
) { delete p
; }
1238 explicit(false) constexpr X(int* p
) : p(p
) {}
1240 consteval
void g4(X x
) { delete x
.p
; }
1252 // Test that we don't redundantly instantiate the friend declaration in
1253 // RemoveNestedImmediateInvocation(). Otherwise, we would end up with spurious
1254 // redefinition errors.
1255 namespace GH107175
{
1257 consteval
void consteval_func() {}
1259 template <auto> struct define_f
{
1260 friend void foo() {}
1263 template <auto = [] {}> struct A
{};
1266 template <auto T
> consteval
void func() { (void)define_f
<T
>{}; }
1274 } // namespace GH107175