1 // RUN: %clang_cc1 -std=c++20 -ferror-limit 0 -verify %s
4 template<typename T
> concept True
= true;
5 template<typename
...T
> concept AllTrue1
= True
<T
>; // expected-error {{expression contains unexpanded parameter pack 'T'}}
6 template<typename
...T
> concept AllTrue2
= (True
<T
> && ...);
7 template<typename
...T
> concept AllTrue3
= (bool)(True
<T
> & ...);
8 static_assert(AllTrue2
<int, float, char>);
9 static_assert(AllTrue3
<int, float, char>);
13 template<typename
...T
> concept AllAddable1
= requires(T
...t
) { (void(t
+ 1), ...); };
14 template<typename
...T
> concept AllAddable2
= (requires(T
...t
) { (t
+ 1); } && ...); // expected-error {{requirement contains unexpanded parameter pack 't'}}
15 template<typename
...T
> concept AllAddable3
= (requires(T t
) { (t
+ 1); } && ...);
16 template<typename
...T
> concept AllAddable4
= requires(T t
) { (t
+ 1); }; // expected-error {{expression contains unexpanded parameter pack 'T'}}
17 template<typename
...T
> concept AllAddable5
= requires(T t
) { (void(t
+ 1), ...); }; // expected-error {{does not contain any unexpanded}}
18 template<typename
...T
> concept AllAddable6
= (requires
{ (T() + 1); } && ...);
19 template<typename
...T
> concept AllAddable7
= requires
{ (T() + 1); }; // expected-error {{expression contains unexpanded parameter pack 'T'}}
21 static_assert(AllAddable1
<int, float>);
22 static_assert(AllAddable3
<int, float>);
23 static_assert(AllAddable6
<int, float>);
24 static_assert(!AllAddable1
<int, void>);
25 static_assert(!AllAddable3
<int, void>);
26 static_assert(!AllAddable6
<int, void>);
30 template<class> concept C
= true; // expected-note 2{{here}}
31 template<class ...Ts
> void f1a() requires C
<Ts
>; // expected-error {{requires clause contains unexpanded parameter pack 'Ts'}}
32 template<class ...Ts
> requires C
<Ts
> void f1b(); // expected-error {{requires clause contains unexpanded parameter pack 'Ts'}}
33 template<class ...Ts
> void f2a() requires (C
<Ts
> && ...);
34 template<class ...Ts
> requires (C
<Ts
> && ...) void f2b();
35 template<class ...Ts
> void f3a() requires C
<Ts
...>; // expected-error {{pack expansion used as argument for non-pack parameter of concept}}
36 template<class ...Ts
> requires C
<Ts
...> void f3b(); // expected-error {{pack expansion used as argument for non-pack parameter of concept}}
37 template<class ...Ts
> void f4() {
38 ([] () requires C
<Ts
> {} ()); // expected-error {{expression contains unexpanded parameter pack 'Ts'}}
39 ([]<int = 0> requires C
<Ts
> () {} ()); // expected-error {{expression contains unexpanded parameter pack 'Ts'}}
41 template<class ...Ts
> void f5() {
42 ([] () requires C
<Ts
> {} (), ...);
43 ([]<int = 0> requires C
<Ts
> () {} (), ...);
47 f1b(); // FIXME: Bad error recovery. expected-error {{undeclared identifier}}
51 f3b(); // FIXME: Bad error recovery. expected-error {{undeclared identifier}}
58 template <typename T
> static constexpr bool V
= true;
61 auto x
= []<bool B
> requires B
{}; // expected-note {{constraints not satisfied}} expected-note {{false}}
63 x
.operator()<false>(); // expected-error {{no matching member function}}
65 auto y
= []<typename T
> requires V
<T
> () {};
66 y
.operator()<int>(); // OK
69 template<typename T
> concept C
= true;
70 template<template<typename T
> requires C
<T
> typename U
> struct X
{};
71 template<typename T
> requires C
<T
> struct Y
{};
76 template<typename T
> concept NotInt
= sizeof(T
) != sizeof(int); // expected-note {{because}}
77 template<typename T
> void f() {
78 [](NotInt
auto) {}(T()); // expected-error {{no matching function}} expected-note {{constraints not satisfied}} expected-note {{because}}
80 template void f
<char>(); // OK
81 template void f
<int>(); // expected-note {{in instantiation of}}
84 namespace PackInTypeConstraint
{
85 template<typename T
, typename U
> concept C
= sizeof(T
) == sizeof(int); // expected-note 3{{}}
87 template<typename
...T
, C
<T
> U
> void h1(); // expected-error {{type constraint contains unexpanded parameter pack 'T'}}
88 template<typename
...T
, C
<T
> ...U
> void h2();
89 template<typename
...T
> void h3(C
<T
> auto); // expected-error {{type constraint contains unexpanded parameter pack 'T'}}
90 template<typename
...T
> void h4(C
<T
> auto...);
92 template<typename
...T
> void f1() {
93 []<C
<T
> U
>(U u
){}(T()); // expected-error {{unexpanded parameter pack 'T'}}
95 template<typename
...T
> void f2() {
96 ([]<C
<T
> U
>(U u
){}(T()), ...); // expected-error {{no match}} expected-note 2{{}}
98 template void f2
<int, int, int>(); // OK
99 template void f2
<int, char, double>(); // expected-note {{in instantiation of}}
101 ([]<typename
...T
, C
<T
> U
>(U u
){}(0), // expected-error {{type constraint contains unexpanded parameter pack 'T'}}
102 ...); // expected-error {{does not contain any unexpanded}}
105 template<typename
...T
> void g1() {
106 [](C
<T
> auto){}(T()); // expected-error {{expression contains unexpanded parameter pack 'T'}}
108 template<typename
...T
> void g2() {
109 ([](C
<T
> auto){}(T()), ...); // expected-error {{no matching function}} expected-note {{constraints not satisfied}} expected-note {{because}}
111 template void g2
<int, int, int>(); // OK
112 template void g2
<int, char, double>(); // expected-note {{in instantiation of}}
114 ([]<typename
...T
>(C
<T
> auto){}(1), // expected-error {{type constraint contains unexpanded parameter pack 'T'}}
115 ...); // expected-error {{does not contain any unexpanded}}
118 template<typename
...T
> void g4() {
119 []() -> C
<T
> auto{ return T(); }(); // expected-error {{expression contains unexpanded parameter pack 'T'}}
121 template<typename
...T
> void g5() {
122 ([]() -> C
<T
> auto{ // expected-error-re {{deduced type {{.*}} does not satisfy}} expected-note {{while substituting into a lambda}}
126 template void g5
<int, int, int>(); // OK
127 template void g5
<int, char, double>(); // expected-note {{in instantiation of}}
129 ([]<typename
...T
>() -> C
<T
> auto{ // expected-error {{declaration type contains unexpanded parameter pack 'T'}}
130 return T(); // expected-error {{expression contains unexpanded parameter pack 'T'}}
132 ...); // expected-error {{does not contain any unexpanded}}
136 namespace BuiltinIsConstantEvaluated
{
137 // Check that we do all satisfaction and diagnostic checks in a constant context.
138 template<typename T
> concept C
= __builtin_is_constant_evaluated(); // expected-warning {{always}}
139 static_assert(C
<int>);
141 template<typename T
> concept D
= __builtin_is_constant_evaluated() == true; // expected-warning {{always}}
142 static_assert(D
<int>);
144 template<typename T
> concept E
= __builtin_is_constant_evaluated() == true && // expected-warning {{always}}
145 false; // expected-note {{'false' evaluated to false}}
146 static_assert(E
<int>); // expected-error {{failed}} expected-note {{because 'int' does not satisfy 'E'}}
148 template<typename T
> concept F
= __builtin_is_constant_evaluated() == false; // expected-warning {{always}}
149 // expected-note@-1 {{'__builtin_is_constant_evaluated() == false' (1 == 0)}}
150 static_assert(F
<int>); // expected-error {{failed}} expected-note {{because 'int' does not satisfy 'F'}}
152 template<typename T
> concept G
= __builtin_is_constant_evaluated() && // expected-warning {{always}}
153 false; // expected-note {{'false' evaluated to false}}
154 static_assert(G
<int>); // expected-error {{failed}} expected-note {{because 'int' does not satisfy 'G'}}
157 namespace NoConstantFolding
{
158 // Ensure we use strict constant evaluation rules when checking satisfaction.
160 template <class T
> concept C
= &n
+ 3 - 3 == &n
; // expected-error {{non-constant expression}} expected-note {{cannot refer to element 3 of non-array object}}
161 static_assert(C
<void>); // expected-note {{while checking}}
165 template <typename T
> concept foo
= true;
166 template <typename T
> concept foo2
= foo
<T
> && true;
167 void f(foo
auto, auto);
168 void f(foo2
auto, auto);
169 void g() { f(1, 2); }
173 template<typename
> concept C
= false;
174 template<typename T
, typename U
> void f(T
, U
);
175 template<C T
, typename U
> void f(T
, U
) = delete;
176 void g() { f(0, 0); }
180 template<class T
> concept C
= false; // expected-note 7 {{because 'false' evaluated to false}}
182 C
auto f1() { // expected-error {{deduced type 'void' does not satisfy 'C'}}
185 C
auto f2() { // expected-error {{deduced type 'void' does not satisfy 'C'}}
188 C
auto f3() { // expected-error {{deduced type 'void' does not satisfy 'C'}}
190 C
decltype(auto) f4() { // expected-error {{deduced type 'void' does not satisfy 'C'}}
193 C
decltype(auto) f5() { // expected-error {{deduced type 'void' does not satisfy 'C'}}
196 C
decltype(auto) f6() { // expected-error {{deduced type 'void' does not satisfy 'C'}}
198 C
auto& f7() { // expected-error {{deduced type 'void' does not satisfy 'C'}}
202 return; // expected-error {{cannot deduce return type 'C auto &' from omitted return expression}}
204 C
auto& f9() { // expected-error {{cannot deduce return type 'C auto &' for function with no return statements}}
208 template<class T
> concept C
= false; // expected-note 3 {{because 'false' evaluated to false}}
210 C
auto *f1() { // expected-error {{deduced type 'void' does not satisfy 'C'}}
211 return (void*)nullptr;
213 C
auto *f2() { // expected-error {{deduced type 'int' does not satisfy 'C'}}
214 return (int*)nullptr;
216 C
auto *****f3() { // expected-error {{deduced type 'int' does not satisfy 'C'}}
217 return (int*****)nullptr;
224 static void f() requires (N
== 0) { return; } // expected-note {{candidate template ignored: constraints not satisfied}} expected-note {{evaluated to false}}
225 static void f() requires (N
== 1) { return; } // expected-note {{candidate template ignored: constraints not satisfied}} expected-note {{evaluated to false}}
227 void (*f1
)() = A
<2>::f
; // expected-error {{address of overloaded function 'f' does not match required type}}
230 template <int N2
= 1> static void f() requires (N2
== 0) { return; } // expected-note {{candidate template ignored: constraints not satisfied [with N2 = 1]}} expected-note {{evaluated to false}}
232 void (*f2
)() = B::f
; // expected-error {{address of overloaded function 'f' does not match required type}}
237 template <class T
, class U
>
238 struct is_same
{ static constexpr bool value
= false; };
241 struct is_same
<T
, T
> { static constexpr bool value
= true; };
243 template <class T
, class U
>
244 concept same_as
= is_same
<T
, U
>::value
; // expected-note-re 4 {{because {{.*}} evaluated to false}}
248 same_as
<int const> auto i1
= f(); // expected-error {{deduced type 'int' does not satisfy 'same_as<const int>'}}
249 same_as
<int const> auto &i2
= f();
250 same_as
<int const> auto &&i3
= f(); // expected-error {{deduced type 'const int &' does not satisfy 'same_as<const int>'}}
252 same_as
<int const &> auto i4
= f(); // expected-error {{deduced type 'int' does not satisfy 'same_as<const int &>'}}
253 same_as
<int const &> auto &i5
= f(); // expected-error {{deduced type 'const int' does not satisfy 'same_as<const int &>'}}
254 same_as
<int const &> auto &&i6
= f();
257 concept C
= false; // expected-note 3 {{because 'false' evaluated to false}}
261 C
auto **j1
= g(); // expected-error {{deduced type 'int' does not satisfy 'C'}}
262 C
auto **&j2
= g(); // expected-error {{deduced type 'int' does not satisfy 'C'}}
263 C
auto **&&j3
= g(); // expected-error {{deduced type 'int' does not satisfy 'C'}}
267 template<class, template <class> class> concept C
= true;
268 template <class> struct S
{};
269 void f(C
<GH55567::S
> auto);
270 } // namespace GH55567
272 namespace SubConstraintChecks
{
273 template <typename T
>
274 concept TrueConstraint
= true;
275 template <typename T
>
276 concept FalseConstraint
= false;
278 template <typename T
, typename
... Us
>
279 class ContainsConstrainedFuncTrue
{
281 template <typename V
, TrueConstraint Constrained
>
282 static void func(V
&&, Constrained
&&C
);
284 template <typename T
, typename
... Us
>
285 class ContainsConstrainedFuncFalse
{
287 template <typename V
, FalseConstraint Constrained
>
288 static void func(V
&&, Constrained
&&C
);
291 template <typename
... Us
>
292 concept TrueConstraint2
=
293 requires(float &&t
) {
294 ContainsConstrainedFuncTrue
<float, Us
...>::func(5, 0.0);
296 template <typename
... Us
>
297 concept FalseConstraint2
=
298 requires(float &&t
) {
299 ContainsConstrainedFuncFalse
<float, Us
...>::func(5, 0.0); // #FC2_CONSTR
302 template <typename T
>
304 requires TrueConstraint2
<int>
307 template <typename T
>
308 void useFalse(int F
) // #USE_FALSE
309 requires FalseConstraint2
<int> // #USE_FALSE_CONSTR
312 // Should only diagnose 'false' once instantiated.
316 // expected-error@-1{{no matching function for call to 'useFalse'}}
317 // expected-note@#USE_FALSE{{constraints not satisfied}}
318 // expected-note@#USE_FALSE_CONSTR{{because 'int' does not satisfy 'FalseConstraint2'}}
319 // expected-note@#FC2_CONSTR {{would be invalid: no matching function for call to 'func'}}
321 } // namespace SubConstraintChecks
323 namespace DeducedTemplateArgs
{
324 template <typename Itr
> struct ItrTraits
{
325 template <typename PtrItr
> struct Ptr
{
327 template <typename PtrItr
>
328 requires requires
{ typename
PtrItr::pointer
; }
330 using type
= typename
Itr::pointer
;
332 using pointer
= typename Ptr
<Itr
>::type
; // #TRAITS_PTR
335 struct complete_itr
{
339 template <typename T
> class Complete
{
340 using ItrType
= ItrTraits
<complete_itr
>;
341 ItrType
begin() noexcept
{ return ItrType(); }
344 // This version doesn't have 'pointer', so error confirms we are in the first
346 struct not_complete_itr
{
349 template <typename T
> class NotComplete
{
350 using ItrType
= ItrTraits
<not_complete_itr
>;
351 ItrType
begin() noexcept
{ return ItrType(); }
352 // expected-error@#TRAITS_PTR{{no type named 'type' in }}
353 // expected-note@-2{{in instantiation of template class }}
355 } // namespace DeducedTemplateArgs
357 namespace DeferredInstantiationInstScope
{
358 template <typename T
>
362 template <typename T
>
363 struct remove_ref
<T
&> {
366 template <typename T
>
367 struct remove_ref
<T
&&> {
371 template <typename T
>
372 constexpr bool IsInt
= PR54443::is_same
<typename remove_ref
<T
>::type
,
375 template <typename U
>
376 void SingleDepthReferencesTop(U
&&u
) {
378 void operator()() // #SDRT_OP
379 requires IsInt
<decltype(u
)> // #SDRT_REQ
386 template <typename U
>
387 void SingleDepthReferencesTopNotCalled(U
&&u
) {
390 requires IsInt
<typename
decltype(u
)::FOO
>
396 template <typename U
>
397 void SingleDepthReferencesTopCalled(U
&&u
) {
399 void operator()() // #CALLOP
400 requires IsInt
<typename
decltype(u
)::FOO
> // #CONSTR
405 // expected-error@-1{{no matching function for call to object of type 'lc'}}
406 // expected-note@#SDRTC{{in instantiation of function template}}
407 // expected-note@#CALLOP{{constraints not satisfied}}
408 // expected-note@#CONSTR{{substituted constraint expression is ill-formed}}
411 template <typename U
>
412 void SingleDepthReferencesTopLambda(U
&&u
) {
414 requires IsInt
<decltype(u
)> // #SDRTL_REQ
418 template <typename U
>
419 void DoubleDepthReferencesTop(U
&&u
) {
420 struct lc
{ // #DDRT_STRCT
423 void operator()() // #DDRT_OP
424 requires IsInt
<decltype(u
)> // #DDRT_REQ
435 template <typename U
>
436 void DoubleDepthReferencesTopLambda(U
&&u
) {
437 []() { []() // #DDRTL_OP
438 requires IsInt
<decltype(u
)> // #DDRTL_REQ
442 template <typename U
>
443 void DoubleDepthReferencesAll(U
&&u
) {
444 struct lc
{ // #DDRA_STRCT
445 void operator()(U
&&u2
) {
447 void operator()(U
&&u3
) // #DDRA_OP
448 requires IsInt
<decltype(u
)> && // #DDRA_REQ
449 IsInt
<decltype(u2
)> && IsInt
<decltype(u3
)>
453 lv2(u2
); // #DDRA_CALL
460 template <typename U
>
461 void DoubleDepthReferencesAllLambda(U
&&u
) {
462 [](U
&&u2
) { // #DDRAL_OP1
463 [](U
&& u3
) // #DDRAL_OP2
464 requires IsInt
<decltype(u
)> // #DDRAL_REQ
465 && IsInt
<decltype(u2
)>
466 && IsInt
<decltype(u3
)>
471 template <typename U
>
472 struct CausesFriendConstraint
{
473 template <typename V
>
474 friend void FriendFunc(CausesFriendConstraint
, V
) // #FF_DECL
479 // FIXME: Re-enable this test when constraints are allowed to refer to captures.
480 // template<typename T>
481 // void ChecksCapture(T x) {
482 // [y = x]() requires(IsInt<decltype(y)>){}();
485 template <typename T
>
486 void ChecksLocalVar(T x
) {
489 requires(IsInt
<decltype(Local
)>) // #CLV_REQ
493 template <typename T
>
494 void LocalStructMemberVar(T x
) {
498 requires(IsInt
<decltype(local
)>) // #LSMV_REQ
501 s
.foo(); // #LSMV_CALL
504 template <typename T
>
505 struct ChecksMemberVar
{
508 requires(IsInt
<decltype(t
)>) // #CMV_FOO
510 template <typename U
>
511 void foo2() // #CMV_FOO2
512 requires(IsInt
<decltype(t
)>) // #CMV_FOO2_REQ
516 void test_dependent() {
519 SingleDepthReferencesTop(v
);
520 SingleDepthReferencesTop(will_fail
);
521 // expected-error@#SDRT_CALL{{no matching function for call to object of type 'lc'}}
522 // expected-note@-2{{in instantiation of function template specialization}}
523 // expected-note@#SDRT_OP{{candidate function not viable}}
524 // expected-note@#SDRT_REQ{{'IsInt<decltype(u)>' evaluated to false}}
526 SingleDepthReferencesTopNotCalled(v
);
527 // Won't error unless we try to call it.
528 SingleDepthReferencesTopNotCalled(will_fail
);
529 SingleDepthReferencesTopCalled(v
); // #SDRTC
530 SingleDepthReferencesTopLambda(v
);
531 SingleDepthReferencesTopLambda(will_fail
);
532 // expected-note@-1{{in instantiation of function template specialization}}
533 // expected-error@#SDRTL_OP{{no matching function for call to object of type}}
534 // expected-note@#SDRTL_OP{{candidate function not viable: constraints not satisfied}}
535 // expected-note@#SDRTL_REQ{{because 'IsInt<decltype(u)>' evaluated to false}}
537 DoubleDepthReferencesTop(v
);
538 DoubleDepthReferencesTop(will_fail
);
539 // expected-error@#DDRT_CALL{{no matching function for call to object of type 'lc2'}}
540 // expected-note@-2{{in instantiation of function template specialization}}
541 // expected-note@#DDRT_STRCT{{in instantiation of member function}}
542 // expected-note@#DDRT_OP{{candidate function not viable}}
543 // expected-note@#DDRT_REQ{{'IsInt<decltype(u)>' evaluated to false}}
545 DoubleDepthReferencesTopLambda(v
);
546 DoubleDepthReferencesTopLambda(will_fail
);
547 // expected-note@-1{{in instantiation of function template specialization}}
548 // expected-error@#DDRTL_OP{{no matching function for call to object of type}}
549 // expected-note@#DDRTL_OP{{candidate function not viable: constraints not satisfied}}
550 // expected-note@#DDRTL_OP{{while substituting into a lambda expression here}}
551 // expected-note@#DDRTL_REQ{{because 'IsInt<decltype(u)>' evaluated to false}}
552 DoubleDepthReferencesAll(v
);
553 DoubleDepthReferencesAll(will_fail
);
554 // expected-error@#DDRA_CALL{{no matching function for call to object of type 'lc2'}}
555 // expected-note@-2{{in instantiation of function template specialization}}
556 // expected-note@#DDRA_STRCT{{in instantiation of member function}}
557 // expected-note@#DDRA_OP{{candidate function not viable}}
558 // expected-note@#DDRA_REQ{{'IsInt<decltype(u)>' evaluated to false}}
560 DoubleDepthReferencesAllLambda(v
);
561 DoubleDepthReferencesAllLambda(will_fail
);
562 // expected-note@-1{{in instantiation of function template specialization}}
563 // expected-note@#DDRAL_OP1{{while substituting into a lambda expression here}}
564 // expected-error@#DDRAL_OP2{{no matching function for call to object of type}}
565 // expected-note@#DDRAL_OP2{{candidate function not viable: constraints not satisfied}}
566 // expected-note@#DDRAL_REQ{{because 'IsInt<decltype(u)>' evaluated to false}}
568 CausesFriendConstraint
<int> CFC
;
570 FriendFunc(CFC
, 1.0);
571 // expected-error@-1{{no matching function for call to 'FriendFunc'}}
572 // expected-note@#FF_DECL{{constraints not satisfied}}
573 // expected-note@#FF_REQ{{because 'IsInt<double>' evaluated to false}}
575 // FIXME: Re-enable this test when constraints are allowed to refer to captures.
579 ChecksLocalVar(will_fail
);
580 // expected-note@-1{{in instantiation of function template specialization}}
581 // expected-error@#CLV_OP{{no matching function for call to object of type}}
582 // expected-note@#CLV_OP{{candidate function not viable: constraints not satisfied}}
583 // expected-note@#CLV_REQ{{because 'IsInt<decltype(Local)>' evaluated to false}}
587 LocalStructMemberVar(v
);
588 LocalStructMemberVar(will_fail
);
589 // expected-error@#LSMV_CALL{{invalid reference to function 'foo'}}
590 // expected-note@-2{{in instantiation of function template specialization}}
591 // expected-note@#LSMV_REQ{{because 'IsInt<decltype(this->local)>' evaluated to false}}
593 ChecksMemberVar
<int> CMV
;
597 ChecksMemberVar
<float> CMV2
;
599 // expected-error@-1{{invalid reference to function 'foo'}}
600 // expected-note@#CMV_FOO{{because 'IsInt<decltype(this->t)>' evaluated to false}}
602 // expected-error@-1{{no matching member function for call to 'foo2'}}
603 // expected-note@#CMV_FOO2{{constraints not satisfied}}
604 // expected-note@#CMV_FOO2_REQ{{because 'IsInt<decltype(this->t)>' evaluated to false}}
606 } // namespace DeferredInstantiationInstScope
608 // Ane example of evaluating a concept at two different depths in the same
609 // evaluation. No diagnostic is expected.
610 namespace SameConceptDifferentDepth
{
612 concept sentinel_for
=
618 concept bidirectional_iterator
=
621 template <class _Iter
>
622 class move_iterator
{
625 requires sentinel_for
<_Iter
>{}
628 static_assert(bidirectional_iterator
<move_iterator
<int>>);
629 } // namespace SameConceptDifferentDepth
633 concept __can_reference
= true;
635 template <class _Iter
>
636 class common_iterator
{
639 constexpr auto x
= requires(_Iter
& __i
) { { __i
} -> __can_reference
; };
644 auto commonIter1
= common_iterator
<int>();
646 } // namespace VarInit
649 namespace InlineFriendOperator
{
650 template <typename T
>
652 template <class _Iter
>
653 class counted_iterator
{
656 constexpr counted_iterator() = default;
657 friend constexpr auto operator+( // expected-note {{candidate function not viable}}
658 int __n
, const counted_iterator
&__x
)
659 requires C
<decltype(I
)>
661 return __x
+ __n
; // expected-error{{invalid operands to binary expression}}
665 constexpr bool test() {
666 counted_iterator
<int> iter
;
667 auto x
= 2 + iter
; // expected-note{{in instantiation of member function 'InlineFriendOperator::operator+'}}
671 } // namespace InlineFriendOperator
673 namespace ClassTemplateInstantiation
{
675 template < typename A
, typename B
, typename C
>
676 concept ConstraintF
= false; // #ConstraintF
677 template < typename A
, typename B
, typename C
>
678 concept ConstraintT
= true;
680 template < typename T
> struct Parent
{
681 template < typename U
, ConstraintT
<T
, U
> > struct ChildT
{};
682 ChildT
<Type
, Type
> CauseInstT
;
683 template < typename U
, ConstraintF
<T
, U
> > struct ChildF
{};// #ChildF
684 ChildF
<Type
, Type
> CauseInstF
; //#CauseInstF
687 // expected-error@#CauseInstF{{constraints not satisfied for class template}}
688 // expected-note@+3{{in instantiation of template class}}
689 // expected-note@#ChildF{{evaluated to false}}
690 // expected-note@#ConstraintF{{because 'false' evaluated to false}}
692 } // namespace ClassTemplateInstantiation
694 namespace SelfFriend
{
696 concept Constraint
= requires (T i
) { (*i
); };
698 concept Constraint2
= requires (T i
) { (*i
); };
700 template<Constraint T
>
702 template <Constraint
>
703 friend class Iterator
;
707 template<Constraint T
> // #ITER_BAD
709 template <Constraint2
>//#ITER_BAD_FRIEND
710 friend class IteratorBad
;
716 IteratorBad
<int*> I3
; // expected-error@#ITER_BAD_FRIEND{{constraint differs}}
717 // expected-note@-1{{in instantiation of template class}}
718 // expected-note@#ITER_BAD{{previous template declaration}}
719 } // namespace SelfFriend
722 namespace Surrogates
{
727 operator F
*() requires N
{ return f1
; } // expected-note{{conversion candidate 'operator int (*)(int)' not viable: constraints not satisfied}}
729 int i
= A
<true>{}(0);
730 int j
= A
<false>{}(0); // expected-error{{no matching function for call to object of type 'A<false>'}}
734 namespace ConstrainedMemberVarTemplate
{
735 template <long Size
> struct Container
{
736 static constexpr long arity
= Size
;
737 template <typename U
>
738 requires(sizeof(U
) == arity
) // #CMVT_REQ
739 using var_templ
= int;
741 Container
<4>::var_templ
<int> inst
;
742 Container
<5>::var_templ
<int> inst_fail
;
743 // expected-error@-1{{constraints not satisfied for alias template 'var_templ'}}
744 // expected-note@#CMVT_REQ{{because 'sizeof(int) == arity' (4 == 5) evaluated to false}}
745 } // namespace ConstrainedMemberVarTemplate
747 // These should not diagnose, where we were unintentionally doing so before by
748 // checking trailing requires clause twice, yet not having the ability to the
749 // 2nd time, since it was no longer a dependent variant.
750 namespace InheritedFromPartialSpec
{
752 constexpr bool Check
= true;
757 Foo(U
&&) requires (Check
<U
>){}
759 void MemFunc(U
&&) requires (Check
<U
>){}
761 static void StaticMemFunc(U
&&) requires (Check
<U
>){}
762 ~Foo() requires (Check
<T
>){}
766 struct Foo
<void> : Foo
<int> {
768 using Foo
<int>::MemFunc
;
769 using Foo
<int>::StaticMemFunc
;
775 Foo
<void>::StaticMemFunc(1.1);
779 struct counted_iterator
{
780 constexpr auto operator->() const noexcept requires
false {
786 concept __has_member_pointer
= requires
{ typename
_Ip::pointer
; };
789 struct __iterator_traits_member_pointer_or_arrow_or_void
{ using type
= void; };
790 template<__has_member_pointer _Ip
>
791 struct __iterator_traits_member_pointer_or_arrow_or_void
<_Ip
> { using type
= typename
_Ip::pointer
; };
794 requires
requires(_Ip
& __i
) { __i
.operator->(); } && (!__has_member_pointer
<_Ip
>)
795 struct __iterator_traits_member_pointer_or_arrow_or_void
<_Ip
> {
796 using type
= decltype(declval
<_Ip
&>().operator->());
801 __iterator_traits_member_pointer_or_arrow_or_void
<counted_iterator
<int>> f
;
803 }// namespace InheritedFromPartialSpec
806 template<typename
, typename
..., typename
= int> // expected-error{{template parameter pack must be the last template parameter}}
807 concept invalid
= true;
809 template<typename
> requires invalid
<int> // expected-error{{use of undeclared identifier 'invalid'}}
810 no errors are printed
813 static_assert(invalid
<int> also here
; // expected-error{{use of undeclared identifier 'invalid'}}
817 b
= invalid
<int> not just in declarations
; // expected-error{{expected ';' after expression}}
818 // expected-error@-1{{use of undeclared identifier 'invalid'}}
819 // expected-error@-2{{expected ';' after expression}}
820 // expected-error@-3{{use of undeclared identifier 'just'}}
821 // expected-error@-4{{unknown type name 'in'}}
824 } // namespace GH48182
827 template<class T
> concept C
= sizeof(T
) == 4; // #61777_C
828 template<class T
, class U
> concept C2
= sizeof(T
) == sizeof(U
); //#61777_C2
832 template<class, C
auto> struct TakesUnary
{ static const int i
= 0 ; }; // #UNARY
833 template<class, C2
<T
> auto> struct TakesBinary
{ static const int i
= 0 ; }; //#BINARY
836 static_assert(Parent
<void>::TakesUnary
<int, 0>::i
== 0);
837 // expected-error@+3{{constraints not satisfied for class template 'TakesUnary'}}
838 // expected-note@#UNARY{{because 'decltype(0ULL)' (aka 'unsigned long long') does not satisfy 'C'}}
839 // expected-note@#61777_C{{because 'sizeof(unsigned long long) == 4' (8 == 4) evaluated to false}}
840 static_assert(Parent
<void>::TakesUnary
<int, 0uLL>::i
== 0);
842 static_assert(Parent
<int>::TakesBinary
<int, 0>::i
== 0);
843 // expected-error@+3{{constraints not satisfied for class template 'TakesBinary'}}
844 // expected-note@#BINARY{{because 'C2<decltype(0ULL), int>' evaluated to false}}
845 // expected-note@#61777_C2{{because 'sizeof(unsigned long long) == sizeof(int)' (8 == 4) evaluated to false}}
846 static_assert(Parent
<int>::TakesBinary
<int, 0ULL>::i
== 0);
849 namespace TemplateInsideNonTemplateClass
{
850 template<typename T
, typename U
> concept C
= true;
852 template<typename T
> auto L
= []<C
<T
> U
>() {};
855 template<C
<int> U
> friend constexpr auto decltype(L
<int>)::operator()() const;
865 template <typename T
>
870 Foo::Bar
<int> BarInstance
;
871 } // namespace TemplateInsideNonTemplateClass
874 template <typename T0
>
875 concept C
= (sizeof(T0
) >= 4);
877 template<typename
...>
882 template<typename
> requires C
<T
>
885 template<typename
> requires
true
889 template <typename
...> struct Mod
{};
891 template <typename T1
, typename T2
>
893 template <typename
> requires C
<T1
>
894 constexpr static int f() { return 1; }
896 template <typename
> requires C
<T2
>
897 constexpr static int f() { return 2; }
900 static_assert(Mod
<int, char>::f
<double>() == 1);
901 static_assert(Mod
<char, int>::f
<double>() == 2);
905 template<typename
...>
911 void foo() requires C
<U
> && C
<T
> && C
<V
>{}
913 void foo() requires
true{}
918 Outer
<int>::Inner
<float> I
;
921 } // namespace GH61959
924 namespace TemplateInsideTemplateInsideTemplate
{
928 template <unsigned I0
>
930 template <unsigned I1
>
932 template <typename T
>
937 template <typename T
>
945 static_assert(W0
<0>::W1
<1>::F
<int>::value
== 1);
946 } // TemplateInsideTemplateInsideTemplate
951 template<auto N
, class T
> void f() {
952 auto l
= []() requires N
{ }; // expected-note 2{{candidate function not viable: constraints not satisfied}} \
953 // expected-note 2{{because 'false' evaluated to false}}
956 // expected-error@-1 {{no matching function for call to object of type}}
958 // expected-error-re@-1 {{no viable conversion from '(lambda {{.*}})' to 'void (*)()'}}
961 template void f
<false, int>(); // expected-note {{in instantiation of function template specialization 'GH63181::f<false, int>' requested here}}
962 template void f
<true, int>();
964 template<class T
> concept C
= __is_same(T
, int); // expected-note{{because '__is_same(char, int)' evaluated to false}}
966 template<class... Ts
> void f() {
967 ([]() requires C
<Ts
> { return Ts(); }(), ...);
968 // expected-error@-1 {{no matching function for call to object of type}} \
969 // expected-note@-1 {{candidate function not viable: constraints not satisfied}} \
970 // expected-note@-1 {{because 'char' does not satisfy 'C'}}
973 template void f
<int, int, int>();
974 template void f
<int, int, char>();
975 //expected-note@-1{{in instantiation of function template specialization 'GH63181::f<int, int, char>' requested here}}
978 template <typename T
, bool IsTrue
>
979 concept Test
= IsTrue
; // expected-note 2{{because 'false' evaluated to false}}
981 template <typename T
, bool IsTrue
>
983 auto l
= [](T t
) // expected-note 2{{candidate function not viable: constraints not satisfied}}
984 requires Test
<decltype(t
), IsTrue
> // expected-note 2{{because 'Test<decltype(t), false>' evaluated to false}}
987 F
* f
= l
; // expected-error {{no viable conversion from}}
988 l(0); // expected-error {{no matching function for call to object}}
993 params
<int, false>(); // expected-note {{in instantiation of function template specialization 'GH63181::params<int, false>' requested here}}
1000 concept True
= true;
1003 concept False
= false; // expected-note 9 {{'false' evaluated to false}}
1006 concept Irrelevant
= false;
1008 template <typename T
>
1009 concept ErrorRequires
= requires(ErrorRequires
auto x
) { x
; };
1010 // expected-error@-1 {{a concept definition cannot refer to itself}} \
1011 // expected-error@-1 {{'auto' not allowed in requires expression parameter}} \
1012 // expected-note@-1 {{declared here}}
1014 template<typename T
> concept C1
= C1
<T
> && []<C1
>(C1
auto) -> C1
auto {};
1015 //expected-error@-1 4{{a concept definition cannot refer to itself}} \
1016 //expected-note@-1 4{{declared here}}
1018 template<class T
> void aaa(T t
) // expected-note {{candidate template ignored: constraints not satisfied}}
1019 requires (False
<T
> || False
<T
>) || False
<T
> {} // expected-note 3 {{'int' does not satisfy 'False'}}
1020 template<class T
> void bbb(T t
) // expected-note {{candidate template ignored: constraints not satisfied}}
1021 requires (False
<T
> || False
<T
>) && True
<T
> {} // expected-note 2 {{'long' does not satisfy 'False'}}
1022 template<class T
> void ccc(T t
) // expected-note {{candidate template ignored: constraints not satisfied}}
1023 requires (True
<T
> || Irrelevant
<T
>) && False
<T
> {} // expected-note {{'unsigned long' does not satisfy 'False'}}
1024 template<class T
> void ddd(T t
) // expected-note {{candidate template ignored: constraints not satisfied}}
1025 requires (Irrelevant
<T
> || True
<T
>) && False
<T
> {} // expected-note {{'int' does not satisfy 'False'}}
1026 template<class T
> void eee(T t
) // expected-note {{candidate template ignored: constraints not satisfied}}
1027 requires (Irrelevant
<T
> || Irrelevant
<T
> || True
<T
>) && False
<T
> {} // expected-note {{'long' does not satisfy 'False'}}
1029 template<class T
> void fff(T t
) // expected-note {{candidate template ignored: constraints not satisfied}}
1030 requires((ErrorRequires
<T
> || False
<T
> || True
<T
>) && False
<T
>) {} // expected-note {{'unsigned long' does not satisfy 'False'}}
1033 aaa(42); // expected-error {{no matching function}}
1034 bbb(42L); // expected-error{{no matching function}}
1035 ccc(42UL); // expected-error {{no matching function}}
1036 ddd(42); // expected-error {{no matching function}}
1037 eee(42L); // expected-error {{no matching function}}
1038 fff(42UL); // expected-error {{no matching function}}
1043 template<typename C
>
1044 auto end(C c
) ->int;
1046 template <typename T
>
1047 concept Iterator
= true;
1049 template <typename CT
>
1050 concept Container
= requires(CT b
) {
1051 { end
} -> Iterator
; // #66612GH_END
1054 static_assert(Container
<int>);// expected-error{{static assertion failed}}
1055 // expected-note@-1{{because 'int' does not satisfy 'Container'}}
1056 // expected-note@#66612GH_END{{because 'end' would be invalid: reference to overloaded function could not be resolved; did you mean to call it?}}
1061 concept True
= true;
1064 concept False
= false;
1068 requires False
<T
> || False
<T
> || False
<T
> || False
<T
> || False
<T
> ||
1069 False
<T
> || False
<T
> || False
<T
> || False
<T
> || True
<T
>
1072 void test() { cand(42); }
1077 template<class> concept IsFoo
= true;
1079 template<class> struct Struct
{
1080 template<IsFoo
auto... xs
>
1083 template<auto... xs
> requires (... && IsFoo
<decltype(xs
)>)
1086 template<IsFoo
auto... xs
>
1087 static inline int field
= 0;
1090 template void Struct
<void>::foo
<>();
1091 template void Struct
<void>::bar
<>();
1092 template int Struct
<void>::field
<1, 2>;
1098 template <class T
> struct basic_sender
{
1100 basic_sender(T
) : func(T()) {}
1103 auto a
= basic_sender
{[](auto... __captures
) {
1104 return []() // #note-a-1
1105 requires((__captures
, ...), false) // #note-a-2
1109 auto b
= basic_sender
{[](auto... __captures
) {
1111 requires([](int, double) { return true; }(decltype(__captures
)()...))
1117 // expected-error@-1{{no matching function for call}}
1118 // expected-note@#note-a-1{{constraints not satisfied}}
1119 // expected-note@#note-a-2{{evaluated to false}}
1123 } // namespace GH64808
1125 namespace GH86757_1
{
1126 template <typename
...> concept b
= false;
1127 template <typename
> concept c
= b
<>;
1128 template <typename d
> concept f
= c
< d
>;
1129 template <f
> struct e
; // expected-note {{}}
1130 template <f d
> struct e
<d
>; // expected-error {{class template partial specialization is not more specialized than the primary template}}
1134 namespace constrained_variadic
{
1135 template <typename T
= int>
1137 void f(); // expected-note {{candidate}}
1138 void f(...) requires
true; // expected-note {{candidate}}
1140 void g(...); // expected-note {{candidate}}
1141 void g() requires
true; // expected-note {{candidate}}
1143 consteval
void h(...);
1144 consteval
void h(...) requires
true {};
1148 S
{}.f(); // expected-error{{call to member function 'f' is ambiguous}}
1149 S
{}.g(); // expected-error{{call to member function 'g' is ambiguous}}
1155 namespace GH109780
{
1157 template <typename T
>
1158 concept Concept
; // expected-error {{expected '='}}
1160 bool val
= Concept
<int>;
1162 template <typename T
>
1163 concept C
= invalid
; // expected-error {{use of undeclared identifier 'invalid'}}
1167 } // namespace GH109780