[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaTemplate / concepts.cpp
blob312469313fc53589a316c143fa9285c292c8d298
1 // RUN: %clang_cc1 -std=c++20 -ferror-limit 0 -verify %s
3 namespace PR47043 {
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>);
12 namespace PR47025 {
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>);
29 namespace PR45699 {
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> () {} (), ...);
45 void g() {
46 f1a();
47 f1b(); // FIXME: Bad error recovery. expected-error {{undeclared identifier}}
48 f2a();
49 f2b();
50 f3a();
51 f3b(); // FIXME: Bad error recovery. expected-error {{undeclared identifier}}
52 f4();
53 f5();
57 namespace P0857R0 {
58 template <typename T> static constexpr bool V = true;
60 void f() {
61 auto x = []<bool B> requires B {}; // expected-note {{constraints not satisfied}} expected-note {{false}}
62 x.operator()<true>();
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 {};
72 X<Y> xy;
75 namespace PR50306 {
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}}
100 void f3() {
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}}
113 void g3() {
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}}
123 return T();
124 }(), ...);
126 template void g5<int, int, int>(); // OK
127 template void g5<int, char, double>(); // expected-note {{in instantiation of}}
128 void g6() {
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'}}
131 }(),
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.
159 int n;
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}}
164 namespace PR50337 {
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); }
172 namespace PR50561 {
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); }
179 namespace PR49188 {
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'}}
183 return void();
185 C auto f2() { // expected-error {{deduced type 'void' does not satisfy 'C'}}
186 return;
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'}}
191 return void();
193 C decltype(auto) f5() { // expected-error {{deduced type 'void' does not satisfy 'C'}}
194 return;
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'}}
199 return void();
201 C auto& f8() {
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}}
207 namespace PR53911 {
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;
221 namespace PR54379 {
222 template <int N>
223 struct A {
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}}
229 struct B {
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}}
235 namespace PR54443 {
237 template <class T, class U>
238 struct is_same { static constexpr bool value = false; };
240 template <class T>
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}}
246 int const &f();
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();
256 template <class T>
257 concept C = false; // expected-note 3 {{because 'false' evaluated to false}}
259 int **const &g();
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'}}
266 namespace GH55567 {
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 {
280 public:
281 template <typename V, TrueConstraint Constrained>
282 static void func(V &&, Constrained &&C);
284 template <typename T, typename... Us>
285 class ContainsConstrainedFuncFalse {
286 public:
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>
303 void useTrue(int F)
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.
313 void UseUse() {
314 useTrue<int>(5);
315 useFalse<int>(5);
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; }
329 struct Ptr<PtrItr> {
330 using type = typename Itr::pointer;
332 using pointer = typename Ptr<Itr>::type; // #TRAITS_PTR
335 struct complete_itr {
336 using pointer = int;
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
345 // verison of 'Ptr'.
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>
359 struct remove_ref {
360 using type = T;
362 template <typename T>
363 struct remove_ref<T &> {
364 using type = T;
366 template <typename T>
367 struct remove_ref<T &&> {
368 using type = T;
371 template <typename T>
372 constexpr bool IsInt = PR54443::is_same<typename remove_ref<T>::type,
373 int>::value;
375 template <typename U>
376 void SingleDepthReferencesTop(U &&u) {
377 struct lc {
378 void operator()() // #SDRT_OP
379 requires IsInt<decltype(u)> // #SDRT_REQ
382 lc lv;
383 lv(); // #SDRT_CALL
386 template <typename U>
387 void SingleDepthReferencesTopNotCalled(U &&u) {
388 struct lc {
389 void operator()()
390 requires IsInt<typename decltype(u)::FOO>
393 lc lv;
396 template <typename U>
397 void SingleDepthReferencesTopCalled(U &&u) {
398 struct lc {
399 void operator()() // #CALLOP
400 requires IsInt<typename decltype(u)::FOO> // #CONSTR
403 lc lv;
404 lv();
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) {
413 []() // #SDRTL_OP
414 requires IsInt<decltype(u)> // #SDRTL_REQ
415 {}();
418 template <typename U>
419 void DoubleDepthReferencesTop(U &&u) {
420 struct lc { // #DDRT_STRCT
421 void operator()() {
422 struct lc2 {
423 void operator()() // #DDRT_OP
424 requires IsInt<decltype(u)> // #DDRT_REQ
427 lc2 lv2;
428 lv2(); // #DDRT_CALL
431 lc lv;
432 lv();
435 template <typename U>
436 void DoubleDepthReferencesTopLambda(U &&u) {
437 []() { []() // #DDRTL_OP
438 requires IsInt<decltype(u)> // #DDRTL_REQ
439 {}(); }();
442 template <typename U>
443 void DoubleDepthReferencesAll(U &&u) {
444 struct lc { // #DDRA_STRCT
445 void operator()(U &&u2) {
446 struct lc2 {
447 void operator()(U &&u3) // #DDRA_OP
448 requires IsInt<decltype(u)> && // #DDRA_REQ
449 IsInt<decltype(u2)> && IsInt<decltype(u3)>
452 lc2 lv2;
453 lv2(u2); // #DDRA_CALL
456 lc lv;
457 lv(u);
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)>
467 {}(u2);
468 }(u);
471 template <typename U>
472 struct CausesFriendConstraint {
473 template <typename V>
474 friend void FriendFunc(CausesFriendConstraint, V) // #FF_DECL
475 requires IsInt<U> &&
476 IsInt<V> // #FF_REQ
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)>){}();
483 // }
485 template <typename T>
486 void ChecksLocalVar(T x) {
487 T Local;
488 []() // #CLV_OP
489 requires(IsInt<decltype(Local)>) // #CLV_REQ
490 {}();
493 template <typename T>
494 void LocalStructMemberVar(T x) {
495 struct S {
496 T local;
497 void foo()
498 requires(IsInt<decltype(local)>) // #LSMV_REQ
500 } s;
501 s.foo(); // #LSMV_CALL
504 template <typename T>
505 struct ChecksMemberVar {
506 T t;
507 void foo()
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() {
517 int v = 0;
518 float will_fail;
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;
569 FriendFunc(CFC, 1);
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.
576 // ChecksCapture(v);
578 ChecksLocalVar(v);
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;
594 CMV.foo();
595 CMV.foo2<int>();
597 ChecksMemberVar<float> CMV2;
598 CMV2.foo();
599 // expected-error@-1{{invalid reference to function 'foo'}}
600 // expected-note@#CMV_FOO{{because 'IsInt<decltype(this->t)>' evaluated to false}}
601 CMV2.foo2<float>();
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 {
611 template <class _Ip>
612 concept sentinel_for =
613 requires(_Ip __i) {
614 __i++;
617 template <class _Ip>
618 concept bidirectional_iterator =
619 sentinel_for<_Ip>;
621 template <class _Iter>
622 class move_iterator {
623 public:
624 auto operator++(int)
625 requires sentinel_for<_Iter>{}
628 static_assert(bidirectional_iterator<move_iterator<int>>);
629 } // namespace SameConceptDifferentDepth
631 namespace VarInit {
632 template <class _Tp>
633 concept __can_reference = true;
635 template <class _Iter>
636 class common_iterator {
637 public:
638 common_iterator() {
639 constexpr auto x = requires(_Iter & __i) { { __i } -> __can_reference; };
643 void test() {
644 auto commonIter1 = common_iterator<int>();
646 } // namespace VarInit
649 namespace InlineFriendOperator {
650 template <typename T>
651 concept C = true;
652 template <class _Iter>
653 class counted_iterator {
654 _Iter I;
655 public:
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+'}}
669 return true;
671 } // namespace InlineFriendOperator
673 namespace ClassTemplateInstantiation {
674 struct Type;
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}}
691 Parent<int> Inst;
692 } // namespace ClassTemplateInstantiation
694 namespace SelfFriend {
695 template<class T>
696 concept Constraint = requires (T i) { (*i); };
697 template<class T>
698 concept Constraint2 = requires (T i) { (*i); };
700 template<Constraint T>
701 struct Iterator {
702 template <Constraint>
703 friend class Iterator;
704 void operator*();
707 template<Constraint T> // #ITER_BAD
708 struct IteratorBad {
709 template <Constraint2>//#ITER_BAD_FRIEND
710 friend class IteratorBad;
711 void operator*();
714 Iterator<int*> I;
715 Iterator<char*> I2;
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 {
723 int f1(int);
724 template <auto N>
725 struct A {
726 using F = int(int);
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 {
751 template<class C>
752 constexpr bool Check = true;
754 template<typename T>
755 struct Foo {
756 template<typename U>
757 Foo(U&&) requires (Check<U>){}
758 template<typename U>
759 void MemFunc(U&&) requires (Check<U>){}
760 template<typename U>
761 static void StaticMemFunc(U&&) requires (Check<U>){}
762 ~Foo() requires (Check<T>){}
765 template<>
766 struct Foo<void> : Foo<int> {
767 using Foo<int>::Foo;
768 using Foo<int>::MemFunc;
769 using Foo<int>::StaticMemFunc;
772 void use() {
773 Foo<void> F {1.1};
774 F.MemFunc(1.1);
775 Foo<void>::StaticMemFunc(1.1);
778 template<typename T>
779 struct counted_iterator {
780 constexpr auto operator->() const noexcept requires false {
781 return T::Invalid;
785 template<class _Ip>
786 concept __has_member_pointer = requires { typename _Ip::pointer; };
788 template<class>
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; };
793 template<class _Ip>
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->());
800 void use2() {
801 __iterator_traits_member_pointer_or_arrow_or_void<counted_iterator<int>> f;
803 }// namespace InheritedFromPartialSpec
805 namespace GH48182 {
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'}}
815 int foo() {
816 bool b;
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'}}
822 return b;
824 } // namespace GH48182
826 namespace GH61777 {
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
830 template<class T>
831 struct Parent {
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>() {};
854 struct Q {
855 template<C<int> U> friend constexpr auto decltype(L<int>)::operator()() const;
858 template <class T>
859 concept C1 = false;
861 struct Foo {
862 template <typename>
863 struct Bar {};
865 template <typename T>
866 requires(C1<T>)
867 struct Bar<T>;
870 Foo::Bar<int> BarInstance;
871 } // namespace TemplateInsideNonTemplateClass
873 namespace GH61959 {
874 template <typename T0>
875 concept C = (sizeof(T0) >= 4);
877 template<typename...>
878 struct Orig { };
880 template<typename T>
881 struct Orig<T> {
882 template<typename> requires C<T>
883 void f() { }
885 template<typename> requires true
886 void f() { }
889 template <typename...> struct Mod {};
891 template <typename T1, typename T2>
892 struct Mod<T1, 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);
903 template<typename T>
904 struct Outer {
905 template<typename ...>
906 struct Inner {};
908 template<typename U>
909 struct Inner<U> {
910 template<typename V>
911 void foo() requires C<U> && C<T> && C<V>{}
912 template<typename V>
913 void foo() requires true{}
917 void bar() {
918 Outer<int>::Inner<float> I;
919 I.foo<char>();
921 } // namespace GH61959
924 namespace TemplateInsideTemplateInsideTemplate {
925 template<typename T>
926 concept C1 = false;
928 template <unsigned I0>
929 struct W0 {
930 template <unsigned I1>
931 struct W1 {
932 template <typename T>
933 struct F {
934 enum { value = 1 };
937 template <typename T>
938 requires C1<T>
939 struct F<T> {
940 enum { value = 2 };
945 static_assert(W0<0>::W1<1>::F<int>::value == 1);
946 } // TemplateInsideTemplateInsideTemplate
949 namespace GH63181 {
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}}
955 l();
956 // expected-error@-1 {{no matching function for call to object of type}}
957 void(*ptr)() = l;
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>
982 void params() {
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}}
986 using F = void(T);
987 F* f = l; // expected-error {{no viable conversion from}}
988 l(0); // expected-error {{no matching function for call to object}}
991 void test_params() {
992 params<int, true>();
993 params<int, false>(); // expected-note {{in instantiation of function template specialization 'GH63181::params<int, false>' requested here}}
998 namespace GH54678 {
999 template<class>
1000 concept True = true;
1002 template<class>
1003 concept False = false; // expected-note 9 {{'false' evaluated to false}}
1005 template<class>
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'}}
1032 void test() {
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}}
1042 namespace GH66612 {
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?}}
1059 namespace GH66938 {
1060 template <class>
1061 concept True = true;
1063 template <class>
1064 concept False = false;
1066 template <class T>
1067 void cand(T t)
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); }
1075 namespace GH63837 {
1077 template<class> concept IsFoo = true;
1079 template<class> struct Struct {
1080 template<IsFoo auto... xs>
1081 void foo() {}
1083 template<auto... xs> requires (... && IsFoo<decltype(xs)>)
1084 void bar() {}
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>;
1096 namespace GH64808 {
1098 template <class T> struct basic_sender {
1099 T func;
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
1107 }()};
1109 auto b = basic_sender{[](auto... __captures) {
1110 return []()
1111 requires([](int, double) { return true; }(decltype(__captures)()...))
1113 }(1, 2.33)};
1115 void foo() {
1116 a.func();
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}}
1120 b.func();
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>
1136 struct S {
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 {};
1147 int test() {
1148 S{}.f(); // expected-error{{call to member function 'f' is ambiguous}}
1149 S{}.g(); // expected-error{{call to member function 'g' is ambiguous}}
1150 S{}.h();
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'}}
1165 bool val2 = C<int>;
1167 } // namespace GH109780