xref: /llvm-project/clang/test/SemaTemplate/pack-deduction.cpp (revision 5c55f9664f7e2f9fe29589a97bc5818d6b8d3c9c)
1 // RUN: %clang_cc1 -std=c++11 -verify %s
2 
3 template<typename ...T> struct X {};
4 
5 template<typename T, typename U> struct P {};
6 
7 namespace Nested {
8   template<typename ...T> int f1(X<T, T...>... a); // expected-note +{{packs of different lengths for parameter 'T'}}
9   template<typename ...T> int f2(P<X<T...>, T> ...a); // expected-note +{{packs of different lengths for parameter 'T'}}
10 
11   int a1 = f1(X<int, int, double>(), X<double, int, double>());
12   int a2 = f1(X<int, int>());
13   int a3 = f1(X<int>(), X<double>()); // expected-error {{no matching}}
14   int a4 = f1(X<int, int>(), X<int>()); // expected-error {{no matching}}
15   int a5 = f1(X<int>(), X<int, int>()); // expected-error {{no matching}}
16   int a6 = f1(X<int, int, int>(), X<int, int, int>(), X<int, int, int, int>()); // expected-error {{no matching}}
17 
18   int b1 = f2(P<X<int, double>, int>(), P<X<int, double>, double>());
19   int b2 = f2(P<X<int, double>, int>(), P<X<int, double>, double>(), P<X<int, double>, char>()); // expected-error {{no matching}}
20 }
21 
22 namespace PR14841 {
23   template<typename T, typename U> struct A {};
24   template<typename ...Ts> void f(A<Ts...>); // expected-note {{substitution failure [with Ts = <char, short, int>]: too many template arg}}
25 
26   void g(A<char, short> a) {
27     f(a);
28     f<char>(a);
29     f<char, short>(a);
30     f<char, short, int>(a); // expected-error {{no matching function}}
31   }
32 }
33 
34 namespace RetainExprPacks {
35   int f(int a, int b, int c);
36   template<typename ...Ts> struct X {};
37   template<typename ...Ts> int g(X<Ts...>, decltype(f(Ts()...)));
38   int n = g<int, int>(X<int, int, int>(), 0);
39 }
40 
41 namespace PR14615 {
42   namespace comment0 {
43     template <class A, class...> struct X {};
44     template <class... B> struct X<int, B...> {
45       typedef int type;
46       struct valid {};
47     };
48     template <typename A, typename... B, typename T = X<A, B...>,
49               typename = typename T::valid>
50     typename T::type check(int);
51     int i = check<int, char>(1);
52   }
53 
54   namespace comment2 {
55     template <class...> struct X;
56     template <typename... B, typename X<B...>::type I = 0>
57     char check(B...); // expected-note {{undefined template 'PR14615::comment2::X<char, int>'}}
58     void f() { check<char>(1, 2); } // expected-error {{no matching function}}
59   }
60 
61   namespace comment3 {
62     template <class...> struct X;
63     template <typename... B, typename X<B...>::type I = (typename X<B...>::type)0>
64     char check(B...); // expected-note {{undefined template 'PR14615::comment3::X<char, int>'}}
65     void f() { check<char>(1, 2); } // expected-error {{no matching function}}
66   }
67 }
68 
69 namespace fully_expanded_packs {
70   template<typename ...T> struct A {
71     template<T ...X> static constexpr int f() {
72       // expected-note@-1 1+{{deduced too few arguments for expanded pack 'X'}}
73       // expected-note@-2 1+{{too many template arguments}}
74       return (X + ... + 0); // expected-warning {{extension}}
75     }
76 
77     template<T ...X, int Y> static constexpr int g() {
78       // expected-note@-1 1+{{deduced too few arguments for expanded pack 'X'}}
79       // expected-note@-2 1+{{couldn't infer template argument 'Y'}}
80       // expected-note@-3 1+{{too many template arguments}}
81       return (X + ... + (1000 * Y)); // expected-warning {{extension}}
82     }
83 
84     template<T ...X, int Y, T ...Z> static constexpr int h() {
85       // expected-note@-1 1+{{deduced too few arguments for expanded pack 'X'}}
86       // expected-note@-2 1+{{couldn't infer template argument 'Y'}}
87       // expected-note@-3 1+{{deduced too few arguments for expanded pack 'Z'}}
88       // expected-note@-4 1+{{too many template arguments}}
89       return (X + ... + (1000 * Y)) + 1000000 * (Z + ... + 0); // expected-warning 2{{extension}}
90     }
91 
92     template<T ...X, int ...Z> static constexpr int i() {
93       return (X + ... + 0) + 1000 * (Z + ... + 0); // expected-warning 2{{extension}}
94     }
95 
96     template<T ...X, int Y, int ...Z> static constexpr int j() {
97       return (X + ... + (1000 * Y)) + 1000000 * (Z + ... + 0); // expected-warning 2{{extension}}
98     }
99   };
100 
101   void check_invalid_calls() {
102     A<int, int>::f(); // expected-error {{no matching function}}
103     A<int, int>::f<>(); // expected-error {{no matching function}}
104     A<int, int>::f<0>(); // expected-error {{no matching function}}
105     A<int, int>::g(); // expected-error {{no matching function}}
106     A<int, int>::g<>(); // expected-error {{no matching function}}
107     A<int, int>::g<0>(); // expected-error {{no matching function}}
108     A<int, int>::g<0, 0>(); // expected-error {{no matching function}}
109     A<>::f<0>(); // expected-error {{no matching function}}
110     A<>::g(); // expected-error {{no matching function}}
111     A<>::g<>(); // expected-error {{no matching function}}
112     A<>::g<0, 0>(); // expected-error {{no matching function}}
113     A<>::h<>(); // expected-error {{no matching function}}
114     A<int>::h<>(); // expected-error {{no matching function}}
115     A<int>::h<0, 0>(); // expected-error {{no matching function}}
116     A<>::h<0, 0>(); // expected-error {{no matching function}}
117   }
118 
119   static_assert(A<>::f() == 0, "");
120   static_assert(A<int>::f<1>() == 1, "");
121   static_assert(A<>::g<1>() == 1000, "");
122   static_assert(A<int>::g<1, 2>() == 2001, "");
123   static_assert(A<>::h<1>() == 1000, "");
124   static_assert(A<int>::h<1, 2, 3>() == 3002001, "");
125   static_assert(A<int, int>::h<1, 20, 3, 4, 50>() == 54003021, "");
126   static_assert(A<>::i<1>() == 1000, "");
127   static_assert(A<int>::i<1>() == 1, "");
128   static_assert(A<>::j<1, 2, 30>() == 32001000, "");
129   static_assert(A<int>::j<1, 2, 3, 40>() == 43002001, "");
130 }
131 
132 namespace partial_full_mix {
133   template<typename T, typename U> struct pair {};
134   template<typename ...T> struct tuple {};
135   template<typename ...T> struct A {
136     template<typename ...U> static pair<tuple<T...>, tuple<U...>> f(pair<T, U> ...p);
137     // expected-note@-1 {{[with U = <char, double, long>]: pack expansion contains parameter pack 'U' that has a different length (2 vs. 3) from outer parameter packs}}
138     // expected-note@-2 {{[with U = <char, double, void>]: pack expansion contains parameter pack 'U' that has a different length (at least 3 vs. 2) from outer parameter packs}}
139 
140     template<typename ...U> static pair<tuple<T...>, tuple<U...>> g(pair<T, U> ...p, ...);
141     // expected-note@-1 {{[with U = <char, double, long>]: pack expansion contains parameter pack 'U' that has a different length (2 vs. 3) from outer parameter packs}}
142 
143     template<typename ...U> static tuple<U...> h(tuple<pair<T, U>..., pair<int, int>>);
144     // expected-note@-1 {{[with U = <int[2]>]: pack expansion contains parameter pack 'U' that has a different length (2 vs. 1) from outer parameter packs}}
145   };
146 
147   pair<tuple<int, float>, tuple<char, double>> k1 = A<int, float>().f<char>(pair<int, char>(), pair<float, double>());
148   pair<tuple<int, float>, tuple<char, double>> k2 = A<int, float>().f<char>(pair<int, char>(), pair<float, double>(), pair<void, long>()); // expected-error {{no match}}
149   pair<tuple<int, float>, tuple<char, double>> k3 = A<int, float>().f<char, double, void>(pair<int, char>(), pair<float, double>()); // expected-error {{no match}}
150 
151   // FIXME: We should accept this by treating the pack 'p' as having a fixed length of 2 here.
152   pair<tuple<int, float>, tuple<char, double>> k4 = A<int, float>().g<char>(pair<int, char>(), pair<float, double>(), pair<void, long>()); // expected-error {{no match}}
153 
154   // FIXME: We should accept this by treating the pack of pairs as having a fixed length of 2 here.
155   tuple<int[2], int[4]> k5 = A<int[1], int[3]>::h<int[2]>(tuple<pair<int[1], int[2]>, pair<int[3], int[4]>, pair<int, int>>()); // expected-error {{no match}}
156 }
157 
158 namespace substitution_vs_function_deduction {
159   template <typename... T> struct A {
160     template <typename... U> void f(void(*...)(T, U)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}
161     template <typename... U> void g(void...(T, U)); // expected-note {{could not match 'void (T, U)' against 'void (*)(int, int)'}}
162   };
163   void f(int, int) {
164     A<int>().f(f);
165     // FIXME: We fail to decay the parameter to a pointer type.
166     A<int>().g(f); // expected-error {{no match}}
167   }
168 }
169 
170 namespace Nested_Explicit_Specialization {
171 template <typename>
172 struct Outer {
173 
174   template <int>
175   struct Inner;
176 
177   template <>
178   struct Inner<0> {
179     template <typename... Args>
180     void Test(Args...) {}
181   };
182 };
183 
184 void Run() {
185   Outer<void>::Inner<0>().Test(1,1);
186 }
187 }
188 
189 namespace GH107560 {
190 int bar(...);
191 
192 template <int> struct Int {};
193 
194 template <class ...T>
195 constexpr auto foo(T... x) -> decltype(bar(T(x)...)) { return 10; }
196 
197 template <class ...T>
198 constexpr auto baz(Int<foo<T>(T())>... x) -> int { return 1; }
199 
200 static_assert(baz<Int<1>, Int<2>, Int<3>>(Int<10>(), Int<10>(), Int<10>()) == 1, "");
201 }
202 
203 namespace GH17042 {
204 
205 template <class... Ts> struct X {
206   template <class... Us> using Y = X<void(Ts, Us)...>; // #GH17042_Y
207 };
208 
209 template <class... T>
210 using any_pairs_list = X<int, int>::Y<T...>; // #any_pairs_list
211 
212 template <class... T>
213 using any_pairs_list_2 = X<int, int>::Y<>;
214 // expected-error@#GH17042_Y {{different length (2 vs. 0)}} \
215 // expected-note@-1 {{requested here}}
216 
217 template <class A, class B, class... P>
218 using any_pairs_list_3 = X<int, int>::Y<A, B, P...>; // #any_pairs_list_3
219 
220 template <class A, class B, class C, class... P>
221 using any_pairs_list_4 = X<int, int>::Y<A, B, C, P...>;
222 // expected-error@#GH17042_Y {{different length (2 vs. at least 3)}} \
223 // expected-note@-1 {{requested here}}
224 
225 static_assert(__is_same(any_pairs_list<char, char>, X<void(int, char), void(int, char)>), "");
226 
227 static_assert(!__is_same(any_pairs_list<char, char, char>, X<void(int, char), void(int, char)>), "");
228 // expected-error@#GH17042_Y {{different length (2 vs. 3)}} \
229 // expected-note@#any_pairs_list {{requested here}} \
230 // expected-note@-1 {{requested here}}
231 
232 static_assert(__is_same(any_pairs_list_3<char, char>, X<void(int, char), void(int, char)>), "");
233 
234 static_assert(!__is_same(any_pairs_list_3<char, char, float>, X<void(int, char), void(int, char)>), "");
235 // expected-error@#GH17042_Y {{different length (2 vs. 3)}} \
236 // expected-note@#any_pairs_list_3 {{requested here}} \
237 // expected-note@-1 {{requested here}}
238 
239 namespace TemplateTemplateParameters {
240 template <class... T> struct C {};
241 
242 template <class T, template <class> class... Args1> struct Ttp {
243   template <template <class> class... Args2>
244   using B = C<void(Args1<T>, Args2<T>)...>;
245 };
246 template <class> struct D {};
247 
248 template <template <class> class... Args>
249 using Alias = Ttp<int, D, D>::B<Args...>;
250 }
251 
252 namespace NTTP {
253 template <int... Args1> struct Nttp {
254   template <int... Args2> using B = Nttp<(Args1 + Args2)...>;
255 };
256 
257 template <int... Args> using Alias = Nttp<1, 2, 3>::B<Args...>;
258 }
259 
260 }
261