xref: /llvm-project/clang/test/SemaTemplate/instantiate-requires-clause.cpp (revision babdef27c503c0bbbcc017e9f88affddda90ea4e)
1 // RUN: %clang_cc1 -std=c++2a -x c++ %s -Wno-unused-value -verify
2 
3 template <typename... Args> requires ((sizeof(Args) == 1), ...)
4 // expected-note@-1 {{because '(sizeof(int) == 1) , (sizeof(char) == 1) , (sizeof(int) == 1)' evaluated to false}}
f1(Args &&...args)5 void f1(Args&&... args) { }
6 // expected-note@-1 {{candidate template ignored: constraints not satisfied [with Args = <int, char, int>]}}
7 
8 using f11 = decltype(f1('a'));
9 using f12 = decltype(f1(1, 'b'));
10 using f13 = decltype(f1(1, 'b', 2));
11 // expected-error@-1 {{no matching function for call to 'f1'}}
12 
13 template <typename... Args>
f2(Args &&...args)14 void f2(Args&&... args) requires ((sizeof(args) == 1), ...) { }
15 // expected-note@-1 {{candidate template ignored: constraints not satisfied [with Args = <int, char, int>]}}
16 // expected-note@-2 {{because '(sizeof (args) == 1) , (sizeof (args) == 1) , (sizeof (args) == 1)' evaluated to false}}
17 
18 using f21 = decltype(f2('a'));
19 using f22 = decltype(f2(1, 'b'));
20 using f23 = decltype(f2(1, 'b', 2));
21 // expected-error@-1 {{no matching function for call to 'f2'}}
22 
23 template <typename... Args> requires ((sizeof(Args) == 1), ...)
24 // expected-note@-1 {{because '(sizeof(int) == 1) , (sizeof(char) == 1) , (sizeof(int) == 1)' evaluated to false}}
f3(Args &&...args)25 void f3(Args&&... args) requires ((sizeof(args) == 1), ...) { }
26 // expected-note@-1 {{candidate template ignored: constraints not satisfied [with Args = <int, char, int>]}}
27 
28 using f31 = decltype(f3('a'));
29 using f32 = decltype(f3(1, 'b'));
30 using f33 = decltype(f3(1, 'b', 2));
31 // expected-error@-1 {{no matching function for call to 'f3'}}
32 
33 template<typename T>
34 struct S {
35 	template<typename U>
fS36 	static constexpr auto f(U const index) requires(index, true) {
37 		return true;
38 	}
39 };
40 
41 static_assert(S<void>::f(1));
42 
43 // Similar to the 'S' test, but tries to use 'U' in the requires clause.
44 template <typename T2>
45 struct S1 {
46   // expected-note@+3 {{candidate template ignored: constraints not satisfied [with U = int]}}
47   // expected-note@+3 {{because substituted constraint expression is ill-formed: type 'int' cannot be used prior to '::' because it has no members}}
48   template <typename U>
fS149   static constexpr auto f(U const index)
50     requires(U::foo)
51   { return true; }
52 };
53 
54 // expected-error@+1 {{no matching function for call to 'f'}}
55 static_assert(S1<void>::f(1));
56 
57 constexpr auto value = 0;
58 
59 template<typename T>
60 struct S2 {
61   template<typename = void> requires(value, true)
fS262   static constexpr auto f() requires(value, true) {
63   }
64 };
65 
66 static_assert((S2<int>::f(), true));
67 
68 template<typename T>
69 struct S3 {
70 	template<typename... Args> requires true
fS371 	static constexpr void f(Args...) { }
72 };
73 
74 static_assert((S3<int>::f(), true));
75 
76 template<typename T>
77 struct S4 {
78     template<typename>
fooS479     constexpr void foo() requires (decltype(this)(), true) { }
gooS480     constexpr void goo() requires (decltype(this)(), true) { }
81 };
82 
83 static_assert((S4<int>{}.foo<int>(), S4<int>{}.goo(), true));
84