1 // RUN: %clang_cc1 -std=c++20 -ferror-limit 0 -verify %s 2 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>); 10 } 11 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'}} 20 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>); 27 } 28 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'}} 40 } 41 template<class ...Ts> void f5() { 42 ([] () requires C<Ts> {} (), ...); 43 ([]<int = 0> requires C<Ts> () {} (), ...); 44 } 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(); 54 } 55 } 56 57 namespace P0857R0 { 58 template <typename T> static constexpr bool V = true; 59 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}} 64 65 auto y = []<typename T> requires V<T> () {}; 66 y.operator()<int>(); // OK 67 } 68 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; 73 } 74 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}} 79 } 80 template void f<char>(); // OK 81 template void f<int>(); // expected-note {{in instantiation of}} 82 } 83 84 namespace PackInTypeConstraint { 85 template<typename T, typename U> concept C = sizeof(T) == sizeof(int); // expected-note 3{{}} 86 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...); 91 92 template<typename ...T> void f1() { 93 []<C<T> U>(U u){}(T()); // expected-error {{unexpanded parameter pack 'T'}} 94 } 95 template<typename ...T> void f2() { 96 ([]<C<T> U>(U u){}(T()), ...); // expected-error {{no match}} expected-note 2{{}} 97 } 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}} 103 } 104 105 template<typename ...T> void g1() { 106 [](C<T> auto){}(T()); // expected-error {{expression contains unexpanded parameter pack 'T'}} 107 } 108 template<typename ...T> void g2() { 109 ([](C<T> auto){}(T()), ...); // expected-error {{no matching function}} expected-note {{constraints not satisfied}} expected-note {{because}} 110 } 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}} 116 } 117 118 template<typename ...T> void g4() { 119 []() -> C<T> auto{ return T(); }(); // expected-error {{expression contains unexpanded parameter pack 'T'}} 120 } 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 }(), ...); 125 } 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}} 133 } 134 } 135 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>); 140 141 template<typename T> concept D = __builtin_is_constant_evaluated() == true; // expected-warning {{always}} 142 static_assert(D<int>); 143 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'}} 147 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'}} 151 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'}} 155 } 156 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}} 162 } 163 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); } 170 } 171 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); } 177 } 178 179 namespace PR49188 { 180 template<class T> concept C = false; // expected-note 7 {{because 'false' evaluated to false}} 181 182 C auto f1() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 183 return void(); 184 } 185 C auto f2() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 186 return; 187 } 188 C auto f3() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 189 } 190 C decltype(auto) f4() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 191 return void(); 192 } 193 C decltype(auto) f5() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 194 return; 195 } 196 C decltype(auto) f6() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 197 } 198 C auto& f7() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 199 return void(); 200 } 201 C auto& f8() { 202 return; // expected-error {{cannot deduce return type 'C auto &' from omitted return expression}} 203 } 204 C auto& f9() { // expected-error {{cannot deduce return type 'C auto &' for function with no return statements}} 205 } 206 } 207 namespace PR53911 { 208 template<class T> concept C = false; // expected-note 3 {{because 'false' evaluated to false}} 209 210 C auto *f1() { // expected-error {{deduced type 'void' does not satisfy 'C'}} 211 return (void*)nullptr; 212 } 213 C auto *f2() { // expected-error {{deduced type 'int' does not satisfy 'C'}} 214 return (int*)nullptr; 215 } 216 C auto *****f3() { // expected-error {{deduced type 'int' does not satisfy 'C'}} 217 return (int*****)nullptr; 218 } 219 } 220 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}} 226 }; 227 void (*f1)() = A<2>::f; // expected-error {{address of overloaded function 'f' does not match required type}} 228 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}} 231 }; 232 void (*f2)() = B::f; // expected-error {{address of overloaded function 'f' does not match required type}} 233 } 234 235 namespace PR54443 { 236 237 template <class T, class U> 238 struct is_same { static constexpr bool value = false; }; 239 240 template <class T> 241 struct is_same<T, T> { static constexpr bool value = true; }; 242 243 template <class T, class U> 244 concept same_as = is_same<T, U>::value; // expected-note-re 4 {{because {{.*}} evaluated to false}} 245 246 int const &f(); 247 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>'}} 251 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(); 255 256 template <class T> 257 concept C = false; // expected-note 3 {{because 'false' evaluated to false}} 258 259 int **const &g(); 260 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'}} 264 } 265 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 271 272 namespace SubConstraintChecks { 273 template <typename T> 274 concept TrueConstraint = true; 275 template <typename T> 276 concept FalseConstraint = false; 277 278 template <typename T, typename... Us> 279 class ContainsConstrainedFuncTrue { 280 public: 281 template <typename V, TrueConstraint Constrained> 282 static void func(V &&, Constrained &&C); 283 }; 284 template <typename T, typename... Us> 285 class ContainsConstrainedFuncFalse { 286 public: 287 template <typename V, FalseConstraint Constrained> 288 static void func(V &&, Constrained &&C); 289 }; 290 291 template <typename... Us> 292 concept TrueConstraint2 = 293 requires(float &&t) { 294 ContainsConstrainedFuncTrue<float, Us...>::func(5, 0.0); 295 }; 296 template <typename... Us> 297 concept FalseConstraint2 = 298 requires(float &&t) { 299 ContainsConstrainedFuncFalse<float, Us...>::func(5, 0.0); // #FC2_CONSTR 300 }; 301 302 template <typename T> 303 void useTrue(int F) 304 requires TrueConstraint2<int> 305 {} 306 307 template <typename T> 308 void useFalse(int F) // #USE_FALSE 309 requires FalseConstraint2<int> // #USE_FALSE_CONSTR 310 {} 311 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'}} 320 } 321 } // namespace SubConstraintChecks 322 323 namespace DeducedTemplateArgs { 324 template <typename Itr> struct ItrTraits { 325 template <typename PtrItr> struct Ptr { 326 }; 327 template <typename PtrItr> 328 requires requires { typename PtrItr::pointer; } 329 struct Ptr<PtrItr> { 330 using type = typename Itr::pointer; 331 }; 332 using pointer = typename Ptr<Itr>::type; // #TRAITS_PTR 333 }; 334 335 struct complete_itr { 336 using pointer = int; 337 }; 338 339 template <typename T> class Complete { 340 using ItrType = ItrTraits<complete_itr>; 341 ItrType begin() noexcept { return ItrType(); } 342 }; 343 344 // This version doesn't have 'pointer', so error confirms we are in the first 345 // verison of 'Ptr'. 346 struct not_complete_itr { 347 }; 348 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 }} 354 }; 355 } // namespace DeducedTemplateArgs 356 357 namespace DeferredInstantiationInstScope { 358 template <typename T> 359 struct remove_ref { 360 using type = T; 361 }; 362 template <typename T> 363 struct remove_ref<T &> { 364 using type = T; 365 }; 366 template <typename T> 367 struct remove_ref<T &&> { 368 using type = T; 369 }; 370 371 template <typename T> 372 constexpr bool IsInt = PR54443::is_same<typename remove_ref<T>::type, 373 int>::value; 374 375 template <typename U> 376 void SingleDepthReferencesTop(U &&u) { 377 struct lc { 378 void operator()() // #SDRT_OP 379 requires IsInt<decltype(u)> // #SDRT_REQ 380 {} 381 }; 382 lc lv; 383 lv(); // #SDRT_CALL 384 } 385 386 template <typename U> 387 void SingleDepthReferencesTopNotCalled(U &&u) { 388 struct lc { 389 void operator()() 390 requires IsInt<typename decltype(u)::FOO> 391 {} 392 }; 393 lc lv; 394 } 395 396 template <typename U> 397 void SingleDepthReferencesTopCalled(U &&u) { 398 struct lc { 399 void operator()() // #CALLOP 400 requires IsInt<typename decltype(u)::FOO> // #CONSTR 401 {} 402 }; 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}} 409 } 410 411 template <typename U> 412 void SingleDepthReferencesTopLambda(U &&u) { 413 []() // #SDRTL_OP 414 requires IsInt<decltype(u)> // #SDRTL_REQ 415 {}(); 416 } 417 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 425 {} 426 }; 427 lc2 lv2; 428 lv2(); // #DDRT_CALL 429 } 430 }; 431 lc lv; 432 lv(); 433 } 434 435 template <typename U> 436 void DoubleDepthReferencesTopLambda(U &&u) { 437 []() { []() // #DDRTL_OP 438 requires IsInt<decltype(u)> // #DDRTL_REQ 439 {}(); }(); 440 } 441 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)> 450 {} 451 }; 452 lc2 lv2; 453 lv2(u2); // #DDRA_CALL 454 } 455 }; 456 lc lv; 457 lv(u); 458 } 459 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); 469 } 470 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 477 {} 478 }; 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 // } 484 485 template <typename T> 486 void ChecksLocalVar(T x) { 487 T Local; 488 []() // #CLV_OP 489 requires(IsInt<decltype(Local)>) // #CLV_REQ 490 {}(); 491 } 492 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 499 {} 500 } s; 501 s.foo(); // #LSMV_CALL 502 }; 503 504 template <typename T> 505 struct ChecksMemberVar { 506 T t; 507 void foo() 508 requires(IsInt<decltype(t)>) // #CMV_FOO 509 {} 510 template <typename U> 511 void foo2() // #CMV_FOO2 512 requires(IsInt<decltype(t)>) // #CMV_FOO2_REQ 513 {} 514 }; 515 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}} 525 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}} 536 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}} 544 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}} 559 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}} 567 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}} 574 575 // FIXME: Re-enable this test when constraints are allowed to refer to captures. 576 // ChecksCapture(v); 577 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}} 584 585 586 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}} 592 593 ChecksMemberVar<int> CMV; 594 CMV.foo(); 595 CMV.foo2<int>(); 596 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}} 605 } 606 } // namespace DeferredInstantiationInstScope 607 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++; 615 }; 616 617 template <class _Ip> 618 concept bidirectional_iterator = 619 sentinel_for<_Ip>; 620 621 template <class _Iter> 622 class move_iterator { 623 public: 624 auto operator++(int) 625 requires sentinel_for<_Iter>{} 626 }; 627 628 static_assert(bidirectional_iterator<move_iterator<int>>); 629 } // namespace SameConceptDifferentDepth 630 631 namespace VarInit { 632 template <class _Tp> 633 concept __can_reference = true; 634 635 template <class _Iter> 636 class common_iterator { 637 public: 638 common_iterator() { 639 constexpr auto x = requires(_Iter & __i) { { __i } -> __can_reference; }; 640 } 641 }; 642 643 void test() { 644 auto commonIter1 = common_iterator<int>(); 645 } 646 } // namespace VarInit 647 648 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)> 660 { 661 return __x + __n; // expected-error{{invalid operands to binary expression}} 662 } 663 }; 664 665 constexpr bool test() { 666 counted_iterator<int> iter; 667 auto x = 2 + iter; // expected-note{{in instantiation of member function 'InlineFriendOperator::operator+'}} 668 669 return true; 670 } 671 } // namespace InlineFriendOperator 672 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; 679 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 685 }; 686 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 693 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); }; 699 700 template<Constraint T> 701 struct Iterator { 702 template <Constraint> 703 friend class Iterator; 704 void operator*(); 705 }; 706 707 template<Constraint T> // #ITER_BAD 708 struct IteratorBad { 709 template <Constraint2>//#ITER_BAD_FRIEND 710 friend class IteratorBad; 711 void operator*(); 712 }; 713 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 720 721 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}} 728 }; 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>'}} 731 } 732 733 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; 740 }; 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 746 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; 753 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>){} 763 }; 764 765 template<> 766 struct Foo<void> : Foo<int> { 767 using Foo<int>::Foo; 768 using Foo<int>::MemFunc; 769 using Foo<int>::StaticMemFunc; 770 }; 771 772 void use() { 773 Foo<void> F {1.1}; 774 F.MemFunc(1.1); 775 Foo<void>::StaticMemFunc(1.1); 776 } 777 778 template<typename T> 779 struct counted_iterator { 780 constexpr auto operator->() const noexcept requires false { 781 return T::Invalid; 782 }; 783 }; 784 785 template<class _Ip> 786 concept __has_member_pointer = requires { typename _Ip::pointer; }; 787 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; }; 792 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->()); 797 }; 798 799 800 void use2() { 801 __iterator_traits_member_pointer_or_arrow_or_void<counted_iterator<int>> f; 802 } 803 }// namespace InheritedFromPartialSpec 804 805 namespace GH48182 { 806 template<typename, typename..., typename = int> // expected-error{{template parameter pack must be the last template parameter}} 807 concept invalid = true; 808 809 template<typename> requires invalid<int> // expected-error{{use of undeclared identifier 'invalid'}} 810 no errors are printed 811 ; 812 813 static_assert(invalid<int> also here ; // expected-error{{use of undeclared identifier 'invalid'}} 814 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; 823 } 824 } // namespace GH48182 825 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 829 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 834 }; 835 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); 841 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); 847 } 848 849 namespace TemplateInsideNonTemplateClass { 850 template<typename T, typename U> concept C = true; 851 852 template<typename T> auto L = []<C<T> U>() {}; 853 854 struct Q { 855 template<C<int> U> friend constexpr auto decltype(L<int>)::operator()() const; 856 }; 857 858 template <class T> 859 concept C1 = false; 860 861 struct Foo { 862 template <typename> 863 struct Bar {}; 864 865 template <typename T> 866 requires(C1<T>) 867 struct Bar<T>; 868 }; 869 870 Foo::Bar<int> BarInstance; 871 } // namespace TemplateInsideNonTemplateClass 872 873 namespace GH61959 { 874 template <typename T0> 875 concept C = (sizeof(T0) >= 4); 876 877 template<typename...> 878 struct Orig { }; 879 880 template<typename T> 881 struct Orig<T> { 882 template<typename> requires C<T> 883 void f() { } 884 885 template<typename> requires true 886 void f() { } 887 }; 888 889 template <typename...> struct Mod {}; 890 891 template <typename T1, typename T2> 892 struct Mod<T1, T2> { 893 template <typename> requires C<T1> 894 constexpr static int f() { return 1; } 895 896 template <typename> requires C<T2> 897 constexpr static int f() { return 2; } 898 }; 899 900 static_assert(Mod<int, char>::f<double>() == 1); 901 static_assert(Mod<char, int>::f<double>() == 2); 902 903 template<typename T> 904 struct Outer { 905 template<typename ...> 906 struct Inner {}; 907 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{} 914 }; 915 }; 916 917 void bar() { 918 Outer<int>::Inner<float> I; 919 I.foo<char>(); 920 } 921 } // namespace GH61959 922 923 924 namespace TemplateInsideTemplateInsideTemplate { 925 template<typename T> 926 concept C1 = false; 927 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 }; 935 }; 936 937 template <typename T> 938 requires C1<T> 939 struct F<T> { 940 enum { value = 2 }; 941 }; 942 }; 943 }; 944 945 static_assert(W0<0>::W1<1>::F<int>::value == 1); 946 } // TemplateInsideTemplateInsideTemplate 947 948 949 namespace GH63181 { 950 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}} 954 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 (*)()'}} 959 } 960 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>(); 963 964 template<class T> concept C = __is_same(T, int); // expected-note{{because '__is_same(char, int)' evaluated to false}} 965 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'}} 971 } 972 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}} 976 977 978 template <typename T, bool IsTrue> 979 concept Test = IsTrue; // expected-note 2{{because 'false' evaluated to false}} 980 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}} 985 {}; 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}} 989 } 990 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}} 994 } 995 996 } 997 998 namespace GH54678 { 999 template<class> 1000 concept True = true; 1001 1002 template<class> 1003 concept False = false; // expected-note 9 {{'false' evaluated to false}} 1004 1005 template<class> 1006 concept Irrelevant = false; 1007 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}} 1013 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}} 1017 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'}} 1028 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'}} 1031 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}} 1039 } 1040 } 1041 1042 namespace GH66612 { 1043 template<typename C> 1044 auto end(C c) ->int; 1045 1046 template <typename T> 1047 concept Iterator = true; 1048 1049 template <typename CT> 1050 concept Container = requires(CT b) { 1051 { end } -> Iterator; // #66612GH_END 1052 }; 1053 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?}} 1057 } 1058 1059 namespace GH66938 { 1060 template <class> 1061 concept True = true; 1062 1063 template <class> 1064 concept False = false; 1065 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> 1070 {} 1071 1072 void test() { cand(42); } 1073 } 1074 1075 namespace GH63837 { 1076 1077 template<class> concept IsFoo = true; 1078 1079 template<class> struct Struct { 1080 template<IsFoo auto... xs> 1081 void foo() {} 1082 1083 template<auto... xs> requires (... && IsFoo<decltype(xs)>) 1084 void bar() {} 1085 1086 template<IsFoo auto... xs> 1087 static inline int field = 0; 1088 }; 1089 1090 template void Struct<void>::foo<>(); 1091 template void Struct<void>::bar<>(); 1092 template int Struct<void>::field<1, 2>; 1093 1094 } 1095 1096 namespace GH64808 { 1097 1098 template <class T> struct basic_sender { 1099 T func; 1100 basic_sender(T) : func(T()) {} 1101 }; 1102 1103 auto a = basic_sender{[](auto... __captures) { 1104 return []() // #note-a-1 1105 requires((__captures, ...), false) // #note-a-2 1106 {}; 1107 }()}; 1108 1109 auto b = basic_sender{[](auto... __captures) { 1110 return []() 1111 requires([](int, double) { return true; }(decltype(__captures)()...)) 1112 {}; 1113 }(1, 2.33)}; 1114 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(); 1121 } 1122 1123 } // namespace GH64808 1124 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}} 1131 } 1132 1133 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}} 1139 1140 void g(...); // expected-note {{candidate}} 1141 void g() requires true; // expected-note {{candidate}} 1142 1143 consteval void h(...); 1144 consteval void h(...) requires true {}; 1145 }; 1146 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(); 1151 } 1152 1153 } 1154 1155 namespace GH109780 { 1156 1157 template <typename T> 1158 concept Concept; // expected-error {{expected '='}} 1159 1160 bool val = Concept<int>; 1161 1162 template <typename T> 1163 concept C = invalid; // expected-error {{use of undeclared identifier 'invalid'}} 1164 1165 bool val2 = C<int>; 1166 1167 } // namespace GH109780 1168 1169 namespace GH121980 { 1170 1171 template <class> 1172 concept has_member_difference_type; // expected-error {{expected '='}} 1173 1174 template <has_member_difference_type> struct incrementable_traits; // expected-note {{declared here}} 1175 1176 template <has_member_difference_type Tp> 1177 struct incrementable_traits<Tp>; // expected-error {{not more specialized than the primary}} 1178 1179 } 1180