1 // RUN: %clang_cc1 -std=c++2a -emit-llvm-only -Wno-unused-value %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 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 {
254 // expected-note@-1 {{declared here}}
256 // expected-error@-1 {{is not a constant expression}}
257 // expected-note@-2 {{function parameter}}
265 template <typename T
> struct remove_reference
{ using type
= T
; };
266 template <typename T
> struct remove_reference
<T
&> { using type
= T
; };
267 template <typename T
> struct remove_reference
<T
&&> { using type
= T
; };
269 template <typename T
>
270 constexpr typename
std::remove_reference
<T
>::type
&& move(T
&&t
) noexcept
{
271 return static_cast<typename
std::remove_reference
<T
>::type
&&>(t
);
276 namespace temporaries
{
279 consteval
int ret_i() const { return 0; }
280 consteval A
ret_a() const { return A
{}; }
284 consteval
int by_value_a(A a
) { return a
.ret_i(); }
286 consteval
int const_a_ref(const A
&a
) {
290 consteval
int rvalue_ref(const A
&&a
) {
294 consteval
const A
&to_lvalue_ref(const A
&&a
) {
300 { int k
= A().ret_i(); }
301 { A k
= A().ret_a(); }
302 { A k
= to_lvalue_ref(A()); }// expected-error {{is not a constant expression}}
303 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
304 { A k
= to_lvalue_ref(A().ret_a()); } // expected-error {{is not a constant expression}}
305 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
306 { int k
= A().ret_a().ret_i(); }
307 { int k
= by_value_a(A()); }
308 { int k
= const_a_ref(A()); }
309 { int k
= const_a_ref(a
); }
310 { int k
= rvalue_ref(A()); }
311 { int k
= rvalue_ref(std::move(a
)); }
312 { int k
= const_a_ref(A().ret_a()); }
313 { int k
= const_a_ref(to_lvalue_ref(A().ret_a())); }
314 { int k
= const_a_ref(to_lvalue_ref(std::move(a
))); }
315 { int k
= by_value_a(A().ret_a()); }
316 { int k
= by_value_a(to_lvalue_ref(std::move(a
))); }
317 { int k
= (A().ret_a(), A().ret_i()); }
318 { int k
= (const_a_ref(A().ret_a()), A().ret_i()); }//
327 // expected-note@-1+ {{allocation performed here was not deallocated}}
331 int i1
= f(); // expected-error {{is not a constant expression}}
334 int* p
= new int(42);
335 // expected-note@-1+ {{heap allocation performed here}}
336 consteval
int ret_i() const { return p
? *p
: 0; }
337 consteval A
ret_a() const { return A
{}; }
338 constexpr ~A() { delete p
; }
341 consteval
int by_value_a(A a
) { return a
.ret_i(); }
343 consteval
int const_a_ref(const A
&a
) {
347 consteval
int rvalue_ref(const A
&&a
) {
351 consteval
const A
&to_lvalue_ref(const A
&&a
) {
356 constexpr A a
{ nullptr };
357 { int k
= A().ret_i(); }
358 { A k
= A().ret_a(); } // expected-error {{is not a constant expression}}
359 // expected-note@-1 {{is not a constant expression}}
360 { A k
= to_lvalue_ref(A()); } // expected-error {{is not a constant expression}}
361 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
362 { A k
= to_lvalue_ref(A().ret_a()); }
363 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
364 // expected-note@-2 {{heap-allocated object is not a constant expression}}
365 // expected-error@-3 {{'alloc::to_lvalue_ref' is not a constant expression}}
366 // expected-note@-4 {{reference to temporary is not a constant expression}}
367 // expected-note@-5 {{temporary created here}}
368 { int k
= A().ret_a().ret_i(); }
369 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
370 // expected-note@-2 {{heap-allocated object is not a constant expression}}
371 { int k
= by_value_a(A()); }
372 { int k
= const_a_ref(A()); }
373 { int k
= const_a_ref(a
); }
374 { int k
= rvalue_ref(A()); }
375 { int k
= rvalue_ref(std::move(a
)); }
376 { int k
= const_a_ref(A().ret_a()); }
377 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
378 // expected-note@-2 {{is not a constant expression}}
379 { int k
= const_a_ref(to_lvalue_ref(A().ret_a())); }
380 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
381 // expected-note@-2 {{is not a constant expression}}
382 { int k
= const_a_ref(to_lvalue_ref(std::move(a
))); }
383 { int k
= by_value_a(A().ret_a()); }
384 { int k
= by_value_a(to_lvalue_ref(static_cast<const A
&&>(a
))); }
385 { int k
= (A().ret_a(), A().ret_i()); }// expected-error {{is not a constant expression}}
386 // expected-note@-1 {{is not a constant expression}}
387 { int k
= (const_a_ref(A().ret_a()), A().ret_i()); }
388 // expected-error@-1 {{'alloc::A::ret_a' is not a constant expression}}
389 // expected-note@-2 {{is not a constant expression}}
394 namespace self_referencing
{
398 constexpr S(int i
) : ptr(this) {
399 if (this == ptr
&& i
)
405 consteval S
f(int i
) {
412 s
= f(0); // expected-error {{is not a constant expression}}
413 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
418 consteval
S1(int i
) : ptr(this) {
419 if (this == ptr
&& i
)
428 s
= S1(0); // expected-error {{is not a constant expression}}
429 // expected-note@-1 {{is not a constant expression}} expected-note@-1 {{temporary created here}}
435 consteval
int f_eval() { // expected-note+ {{declared here}}
440 struct strong_ordering
{
442 static const strong_ordering less
, equal
, greater
;
444 constexpr strong_ordering
strong_ordering::less
= {-1};
445 constexpr strong_ordering
strong_ordering::equal
= {0};
446 constexpr strong_ordering
strong_ordering::greater
= {1};
447 constexpr bool operator!=(strong_ordering
, int);
452 virtual consteval
void f(); // expected-note {{overridden}}
453 virtual void g(); // expected-note {{overridden}}
460 void f(); // expected-error {{non-consteval function 'f' cannot override a consteval function}}
461 consteval
void g(); // expected-error {{consteval function 'g' cannot override a non-consteval function}}
464 namespace implicit_equals_1
{
467 std::strong_ordering
operator<=>(const X
&) const;
468 constexpr bool operator==(const X
&) const;
469 virtual consteval
bool operator==(const Y
&) const; // expected-note {{here}}
472 std::strong_ordering
operator<=>(const Y
&) const = default;
473 // expected-error@-1 {{non-consteval function 'operator==' cannot override a consteval function}}
477 namespace implicit_equals_2
{
480 constexpr std::strong_ordering
operator<=>(const X
&) const;
481 constexpr bool operator==(const X
&) const;
482 virtual bool operator==(const Y
&) const; // expected-note {{here}}
485 consteval
std::strong_ordering
operator<=>(const Y
&) const = default;
486 // expected-error@-1 {{consteval function 'operator==' cannot override a non-consteval function}}
491 namespace operator_rewrite
{
493 friend consteval
int operator<=>(const A
&, const A
&) { return 0; }
495 const bool k
= A() < A();
499 bool k2
= A() < a
; // OK, does not access 'a'.
502 friend consteval
int operator<=>(const B
&l
, const B
&r
) { return r
.n
- l
.n
; } // expected-note {{read of }}
505 static_assert(B() >= B());
506 B b
; // expected-note {{here}}
507 bool k3
= B() < b
; // expected-error-re {{call to consteval function '{{.*}}::operator<=>' is not a constant expression}} expected-note {{in call}}
512 consteval
A(int(*p
)() = nullptr) : ptr(p
) {}
517 B() : ptr(nullptr) {}
518 consteval
B(int(*p
)(), int) : ptr(p
) {}
523 { A
a(&f_eval
); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
525 { B
b(&f_eval
, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
527 { A a
{&f_eval
}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
529 { B b
{&f_eval
, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
531 { A a
= A(&f_eval
); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
532 { B b
= B(nullptr, 0); }
533 { B b
= B(&f_eval
, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
535 { A a
= A
{&f_eval
}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
536 { B b
= B
{nullptr, 0}; }
537 { B b
= B
{&f_eval
, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
539 { A a
; a
= A(&f_eval
); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
540 { B b
; b
= B(nullptr, 0); }
541 { B b
; b
= B(&f_eval
, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
543 { A a
; a
= A
{&f_eval
}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
544 { B b
; b
= B
{nullptr, 0}; }
545 { B b
; b
= B
{&f_eval
, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
546 { A
* a
; a
= new A(); }
547 { A
* a
; a
= new A(&f_eval
); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
548 { B
* b
; b
= new B(nullptr, 0); }
549 { B
* b
; b
= new B(&f_eval
, 0); } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
550 { A
* a
; a
= new A
{}; }
551 { A
* a
; a
= new A
{&f_eval
}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
552 { B
* b
; b
= new B
{nullptr, 0}; }
553 { B
* b
; b
= new B
{&f_eval
, 0}; } // expected-error {{is not a constant expression}} expected-note {{to a consteval}}
558 namespace copy_ctor
{
560 consteval
int f_eval() { // expected-note+ {{declared here}}
566 constexpr Copy(int(*p
)() = nullptr) : ptr(p
) {}
567 consteval
Copy(const Copy
&) = default;
570 constexpr const Copy
&to_lvalue_ref(const Copy
&&a
) {
575 constexpr const Copy C
;
576 // there is no the copy constructor call when its argument is a prvalue because of garanteed copy elision.
577 // so we need to test with both prvalue and xvalues.
579 { Copy
c((Copy(&f_eval
))); }// expected-error {{cannot take address of consteval}}
580 { Copy
c(std::move(C
)); }
581 { Copy
c(std::move(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
582 { Copy
c(to_lvalue_ref((Copy(&f_eval
)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
583 { Copy
c(to_lvalue_ref(std::move(C
))); }
584 { Copy
c(to_lvalue_ref(std::move(Copy(&f_eval
)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
585 { Copy c
= Copy(C
); }
586 { Copy c
= Copy(Copy(&f_eval
)); }// expected-error {{cannot take address of consteval}}
587 { Copy c
= Copy(std::move(C
)); }
588 { Copy c
= Copy(std::move(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
589 { Copy c
= Copy(to_lvalue_ref(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
590 { Copy c
= Copy(to_lvalue_ref(std::move(C
))); }
591 { Copy c
= Copy(to_lvalue_ref(std::move(Copy(&f_eval
)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
592 { Copy c
; c
= Copy(C
); }
593 { Copy c
; c
= Copy(Copy(&f_eval
)); }// expected-error {{cannot take address of consteval}}
594 { Copy c
; c
= Copy(std::move(C
)); }
595 { Copy c
; c
= Copy(std::move(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
596 { Copy c
; c
= Copy(to_lvalue_ref(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
597 { Copy c
; c
= Copy(to_lvalue_ref(std::move(C
))); }
598 { Copy c
; c
= Copy(to_lvalue_ref(std::move(Copy(&f_eval
)))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
599 { Copy
* c
; c
= new Copy(C
); }
600 { Copy
* c
; c
= new Copy(Copy(&f_eval
)); }// expected-error {{cannot take address of consteval}}
601 { Copy
* c
; c
= new Copy(std::move(C
)); }
602 { Copy
* c
; c
= new Copy(std::move(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
603 { Copy
* c
; c
= new Copy(to_lvalue_ref(Copy(&f_eval
))); }// expected-error {{is not a constant expression}} expected-note {{to a consteval}}
604 { Copy
* c
; c
= new Copy(to_lvalue_ref(std::move(C
))); }
605 { 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}}
608 } // namespace special_ctor
610 namespace unevaluated
{
612 template <typename T
, typename U
> struct is_same
{ static const bool value
= false; };
613 template <typename T
> struct is_same
<T
, T
> { static const bool value
= true; };
615 long f(); // expected-note {{declared here}}
616 auto consteval
g(auto a
) {
620 auto e
= g(f()); // expected-error {{is not a constant expression}}
621 // expected-note@-1 {{non-constexpr function 'f' cannot be used in a constant expression}}
623 using T
= decltype(g(f()));
624 static_assert(is_same
<long, T
>::value
);
626 } // namespace unevaluated
628 namespace value_dependent
{
630 consteval
int foo(int x
) {
634 template <int X
> constexpr int bar() {
635 // Previously this call was rejected as value-dependent constant expressions
636 // can't be immediately evaluated. Now we show that we don't immediately
637 // evaluate them until they are instantiated.
641 template <typename T
> constexpr int baz() {
642 constexpr int t
= sizeof(T
);
643 // Previously this call was rejected as `t` is value-dependent and its value
644 // is unknown until the function is instantiated. Now we show that we don't
645 // reject such calls.
649 static_assert(bar
<15>() == 15);
650 static_assert(baz
<int>() == sizeof(int));
651 } // namespace value_dependent
653 // https://github.com/llvm/llvm-project/issues/55601
654 namespace issue_55601
{
657 consteval
static T
x() { return 5; } // expected-note {{non-constexpr constructor 'derp' cannot be used in a constant expression}}
659 Bar() : a(x()) {} // expected-error {{call to consteval function 'issue_55601::Bar<issue_55601::derp>::x' is not a constant expression}}
660 // expected-error@-1 {{call to consteval function 'issue_55601::derp::operator int' is not a constant expression}}
661 // expected-note@-2 {{in call to 'x()'}}
662 // expected-note@-3 {{non-literal type 'issue_55601::derp' cannot be used in a constant expression}}
670 // Can't be used in a constant expression
671 derp(int); // expected-note {{declared here}}
672 consteval
operator int() const { return 5; }
674 Bar
<derp
> a
; // expected-note {{in instantiation of member function 'issue_55601::Bar<issue_55601::derp>::Bar' requested here}}
676 struct constantDerp
{
677 // Can be used in a constant expression.
678 consteval
constantDerp(int) {}
679 consteval
operator int() const { return 5; }
683 } // namespace issue_55601
685 namespace default_argument
{
687 // Previously calls of consteval functions in default arguments were rejected.
688 // Now we show that we don't reject such calls.
689 consteval
int foo() { return 1; }
690 consteval
int bar(int i
= foo()) { return i
* i
; }
693 Test1(int i
= bar(13)) {}
694 void v(int i
= bar(13) * 2 + bar(15)) {}
699 constexpr Test2(int i
= bar()) {}
700 constexpr void v(int i
= bar(bar(bar(foo())))) {}
704 } // namespace default_argument
714 consteval
int operator[](int i
) const { return {}; }
715 consteval
const derp
* operator->() const { return &d
; }
716 consteval
int f() const { return 12; } // expected-note 2{{declared here}}
721 // We previously rejected both of these overloaded operators as taking the
722 // address of a consteval function outside of an immediate context, but we
723 // accepted direct calls to the overloaded operator. Now we show that we accept
725 constexpr int s
= a
.operator[](1);
726 constexpr int t
= a
[1];
727 constexpr int u
= a
.operator->()->b
;
728 constexpr int v
= a
->b
;
729 // FIXME: I believe this case should work, but we currently reject.
730 constexpr int w
= (a
.*&test::f
)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}}
731 constexpr int x
= a
.f();
733 // Show that we reject when not in an immediate context.
734 int w2
= (a
.*&test::f
)(); // expected-error {{cannot take address of consteval function 'f' outside of an immediate invocation}}
743 consteval
int a() const { return 1; }
746 this->a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \
747 // expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
751 a() + d(); // expected-error {{call to consteval function 'PR48235::A::a' is not a constant expression}} \
752 // expected-note {{use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
757 namespace NamespaceScopeConsteval
{
759 int Val
; // expected-note {{subobject declared here}}
763 S s1
; // expected-error {{call to consteval function 'NamespaceScopeConsteval::S::S' is not a constant expression}} \
764 expected
-note
{{subobject of type
'int' is
not initialized
}}
766 template <typename Ty
>
768 Ty Val
; // expected-note {{subobject declared here}}
772 T
<int> t
; // expected-error {{call to consteval function 'NamespaceScopeConsteval::T<int>::T' is not a constant expression}} \
773 expected
-note
{{subobject of type
'int' is
not initialized
}}
775 } // namespace NamespaceScopeConsteval
777 namespace Issue54578
{
778 // We expect the user-defined literal to be resovled entirely at compile time
779 // despite being instantiated through a template.
780 inline consteval
unsigned char operator""_UC(const unsigned long long n
) {
781 return static_cast<unsigned char>(n
);
784 inline constexpr char f1(const auto octet
) {
788 template <typename Ty
>
789 inline constexpr char f2(const Ty octet
) {
794 static_assert(f1('a') == 4);
795 static_assert(f2('a') == 4);
796 constexpr int c
= f1('a') + f2('a');
797 static_assert(c
== 8);
801 namespace defaulted_special_member_template
{
802 template <typename T
>
803 struct default_ctor
{
805 consteval
default_ctor() = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}}
808 template <typename T
>
812 consteval
copy(const copy
&) = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}}
813 consteval copy
&operator=(const copy
&) = default; // expected-note {{non-constexpr function 'operator=' cannot be used in a constant expression}}
817 template <typename T
>
821 consteval
move(move
&&) = default; // expected-note {{non-constexpr constructor 'foo' cannot be used in a constant expression}}
822 consteval move
&operator=(move
&&) = default; // expected-note {{non-constexpr function 'operator=' cannot be used in a constant expression}}
827 foo() {} // expected-note {{declared here}}
828 foo(const foo
&) {} // expected-note {{declared here}}
829 foo(foo
&&) {} // expected-note {{declared here}}
831 foo
& operator=(const foo
&) { return *this; } // expected-note {{declared here}}
832 foo
& operator=(foo
&&) { return *this; } // expected-note {{declared here}}
836 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}} \
837 expected
-note
{{in call to
'default_ctor()'}}
840 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}} \
841 expected
-note
{{in call to
'copy(good0)'}}
842 fail1
= good0
; // expected-error {{call to consteval function 'defaulted_special_member_template::copy<defaulted_special_member_template::foo>::operator=' is not a constant expression}} \
843 expected
-note
{{in call to
'&fail1->operator=(good0)'}}
846 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}} \
847 expected
-note
{{in call to
'move(good1)'}}
848 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}} \
849 expected
-note
{{in call to
'&fail2->operator=(good1)'}}
851 } // namespace defaulted_special_member_template
853 namespace multiple_default_constructors
{
855 Foo() {} // expected-note {{declared here}}
864 template <typename T
, unsigned N
>
867 S() requires (N
==1) = default;
868 // This cannot be used in constexpr context.
869 S() requires (N
==2) {} // expected-note {{declared here}}
870 consteval
S() requires (N
==3) = default; // expected-note {{non-constexpr constructor 'Foo' cannot be used in a constant expression}}
874 // Explictly defaulted constructor.
877 // User provided constructor.
880 // Consteval explictly defaulted constructor.
881 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}} \
882 expected
-note
{{in call to
'S()'}}
887 consteval
int aConstevalFunction() { // expected-error {{consteval function never produces a constant expression}}
888 // Defaulted default constructors are implicitly consteval.
891 S
<Baz
, 2> s4
; // expected-note {{non-constexpr constructor 'S' cannot be used in a constant expression}}
898 } // namespace multiple_default_constructors
902 consteval
int testDefaultArgForParam(E eParam
= (E
)-1) {
903 // expected-error@-1 {{integer value -1 is outside the valid range of values [0, 1] for this enumeration type}}
908 return testDefaultArgForParam() + testDefaultArgForParam((E
)1);
913 // Nested consteval function.
914 consteval
int f(int v
) {
918 template <typename T
>
919 consteval
int g(T a
) {
920 // An immediate function context.
924 static_assert(g(100) == 100);
925 // --------------------------------------
926 template <typename T
>
927 consteval T
max(const T
& a
, const T
& b
) {
928 return (a
> b
) ? a
: b
;
930 template <typename T
>
931 consteval T
mid(const T
& a
, const T
& b
, const T
& c
) {
932 T m
= max(max(a
, b
), c
);
939 static_assert(max(1,2)==2);
940 static_assert(mid(1,2,3)==2);
941 } // namespace GH51182
943 // https://github.com/llvm/llvm-project/issues/56183
945 consteval
auto Foo(auto c
) { return c
; }
946 consteval
auto Bar(auto f
) { return f(); }
948 constexpr auto x
= Foo(Bar([] { return 'a'; }));
949 static_assert(x
== 'a');
951 } // namespace GH56183
953 // https://github.com/llvm/llvm-project/issues/51695
955 // Original ========================================
956 template <typename T
>
959 template <typename
...>
962 template <typename T
, typename
... Ts
>
963 consteval
auto pop_front(list_t
<T
, Ts
...>) -> auto {
964 return list_t
<Ts
...>{};
967 template <typename
... Ts
, typename F
>
968 consteval
auto apply(list_t
<Ts
...>, F fn
) -> auto {
969 return fn(type_t
<Ts
>{}...);
973 constexpr auto x
= apply(pop_front(list_t
<char, char>{}),
974 []<typename
... Us
>(type_t
<Us
>...) { return 42; });
975 static_assert(x
== 42);
977 // Reduced 1 ========================================
978 consteval
bool zero() { return false; }
980 template <typename F
>
981 consteval
bool foo(bool, F f
) {
986 constexpr auto x
= foo(zero(), []() { return true; });
990 // Reduced 2 ========================================
991 template <typename F
>
992 consteval
auto bar(F f
) { return f
;}
995 constexpr auto t1
= bar(bar(bar(bar([]() { return true; }))))();
998 int a
= 1; // expected-note {{declared here}}
999 auto t2
= bar(bar(bar(bar([=]() { return a
; }))))(); // expected-error-re {{call to consteval function 'GH51695::bar<(lambda at {{.*}})>' is not a constant expression}}
1000 // expected-note@-1 {{read of non-const variable 'a' is not allowed in a constant expression}}
1002 constexpr auto t3
= bar(bar([x
=bar(42)]() { return x
; }))();
1003 static_assert(t3
==42);
1004 constexpr auto t4
= bar(bar([x
=bar(42)]() consteval
{ return x
; }))();
1005 static_assert(t4
==42);
1008 } // namespace GH51695
1010 // https://github.com/llvm/llvm-project/issues/50455
1013 []() consteval
{ int i
{}; }();
1014 []() consteval
{ int i
{}; ++i
; }();
1017 (void)[](int i
) consteval
{ return i
; }(0);
1018 (void)[](int i
) consteval
{ return i
; }(0);
1020 } // namespace GH50455
1025 consteval
operator int() { return 1;}