1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
2 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++1z
3 
4 // Template argument deduction with template template parameters.
5 template<typename T, template<T> class A>
6 struct X0 {
7   static const unsigned value = 0;
8 };
9 
10 template<template<int> class A>
11 struct X0<int, A> {
12   static const unsigned value = 1;
13 };
14 
15 template<int> struct X0i;
16 template<long> struct X0l;
17 int array_x0a[X0<long, X0l>::value == 0? 1 : -1];
18 int array_x0b[X0<int, X0i>::value == 1? 1 : -1];
19 
20 template<typename T, typename U>
21 struct is_same {
22   static const bool value = false;
23 };
24 
25 template<typename T>
26 struct is_same<T, T> {
27   static const bool value = true;
28 };
29 
30 template<typename T> struct allocator { };
31 template<typename T, typename Alloc = allocator<T> > struct vector {};
32 
33 // Fun with meta-lambdas!
34 struct _1 {};
35 struct _2 {};
36 
37 // Replaces all occurrences of _1 with Arg1 and _2 with Arg2 in T.
38 template<typename T, typename Arg1, typename Arg2>
39 struct Replace {
40   typedef T type;
41 };
42 
43 // Replacement of the whole type.
44 template<typename Arg1, typename Arg2>
45 struct Replace<_1, Arg1, Arg2> {
46   typedef Arg1 type;
47 };
48 
49 template<typename Arg1, typename Arg2>
50 struct Replace<_2, Arg1, Arg2> {
51   typedef Arg2 type;
52 };
53 
54 // Replacement through cv-qualifiers
55 template<typename T, typename Arg1, typename Arg2>
56 struct Replace<const T, Arg1, Arg2> {
57   typedef typename Replace<T, Arg1, Arg2>::type const type;
58 };
59 
60 // Replacement of templates
61 template<template<typename> class TT, typename T1, typename Arg1, typename Arg2>
62 struct Replace<TT<T1>, Arg1, Arg2> {
63   typedef TT<typename Replace<T1, Arg1, Arg2>::type> type;
64 };
65 
66 template<template<typename, typename> class TT, typename T1, typename T2,
67          typename Arg1, typename Arg2>
68 struct Replace<TT<T1, T2>, Arg1, Arg2> {
69   typedef TT<typename Replace<T1, Arg1, Arg2>::type,
70              typename Replace<T2, Arg1, Arg2>::type> type;
71 };
72 
73 // Just for kicks...
74 template<template<typename, typename> class TT, typename T1,
75          typename Arg1, typename Arg2>
76 struct Replace<TT<T1, _2>, Arg1, Arg2> {
77   typedef TT<typename Replace<T1, Arg1, Arg2>::type, Arg2> type;
78 };
79 
80 int array0[is_same<Replace<_1, int, float>::type, int>::value? 1 : -1];
81 int array1[is_same<Replace<const _1, int, float>::type, const int>::value? 1 : -1];
82 int array2[is_same<Replace<vector<_1>, int, float>::type, vector<int> >::value? 1 : -1];
83 int array3[is_same<Replace<vector<const _1>, int, float>::type, vector<const int> >::value? 1 : -1];
84 int array4[is_same<Replace<vector<int, _2>, double, float>::type, vector<int, float> >::value? 1 : -1];
85 
86 // PR5911
87 template <typename T, int N> void f(const T (&a)[N]);
88 int iarr[] = { 1 };
89 void test_PR5911() { f(iarr); }
90 
91 // Must not examine base classes of incomplete type during template argument
92 // deduction.
93 namespace PR6257 {
94   template <typename T> struct X {
95     template <typename U> X(const X<U>& u);
96   };
97   struct A;
98   void f(A& a);
99   void f(const X<A>& a);
100   void test(A& a) { (void)f(a); }
101 }
102 
103 // PR7463
104 namespace PR7463 {
105   const int f ();
106   template <typename T_> void g (T_&); // expected-note{{T_ = int}}
107   void h (void) { g(f()); } // expected-error{{no matching function for call}}
108 }
109 
110 namespace test0 {
111   template <class T> void make(const T *(*fn)()); // expected-note {{candidate template ignored: cannot deduce a type for 'T' that would make 'const T' equal 'char'}}
112   char *char_maker();
113   void test() {
114     make(char_maker); // expected-error {{no matching function for call to 'make'}}
115   }
116 }
117 
118 namespace test1 {
119   template<typename T> void foo(const T a[3][3]);
120   void test() {
121     int a[3][3];
122     foo(a);
123   }
124 }
125 
126 // PR7708
127 namespace test2 {
128   template<typename T> struct Const { typedef void const type; };
129 
130   template<typename T> void f(T, typename Const<T>::type*);
131   template<typename T> void f(T, void const *);
132 
133   void test() {
134     void *p = 0;
135     f(0, p);
136   }
137 }
138 
139 // rdar://problem/8537391
140 namespace test3 {
141   struct Foo {
142     template <void F(char)> static inline void foo();
143   };
144 
145   class Bar {
146     template<typename T> static inline void wobble(T ch);
147 
148   public:
149     static void madness() {
150       Foo::foo<wobble<char> >();
151     }
152   };
153 }
154 
155 // Verify that we can deduce enum-typed arguments correctly.
156 namespace test14 {
157   enum E { E0, E1 };
158   template <E> struct A {};
159   template <E e> void foo(const A<e> &a) {}
160 
161   void test() {
162     A<E0> a;
163     foo(a);
164   }
165 }
166 
167 namespace PR21536 {
168   template<typename ...T> struct X;
169   template<typename A, typename ...B> struct S {
170     static_assert(sizeof...(B) == 1, "");
171     void f() {
172       using T = A;
173       using T = int;
174 
175       using U = X<B...>;
176       using U = X<int>;
177     }
178   };
179   template<typename ...T> void f(S<T...>);
180   void g() { f(S<int, int>()); }
181 }
182 
183 namespace PR19372 {
184   template <template<typename...> class C, typename ...Us> struct BindBack {
185     template <typename ...Ts> using apply = C<Ts..., Us...>;
186   };
187   template <typename, typename...> struct Y;
188   template <typename ...Ts> using Z = Y<Ts...>;
189 
190   using T = BindBack<Z, int>::apply<>;
191   using T = Z<int>;
192 
193   using U = BindBack<Z, int, int>::apply<char>;
194   using U = Z<char, int, int>;
195 
196   namespace BetterReduction {
197     template<typename ...> struct S;
198     template<typename ...A> using X = S<A...>; // expected-note {{parameter}}
199     template<typename ...A> using Y = X<A..., A...>;
200     template<typename ...A> using Z = X<A..., 1, 2, 3>; // expected-error {{must be a type}}
201 
202     using T = Y<int>;
203     using T = S<int, int>;
204   }
205 }
206 
207 namespace PR18645 {
208   template<typename F> F Quux(F &&f);
209   auto Baz = Quux(Quux<float>);
210 }
211 
212 namespace NonDeducedNestedNameSpecifier {
213   template<typename T> struct A {
214     template<typename U> struct B {
215       B(int) {}
216     };
217   };
218 
219   template<typename T> int f(A<T>, typename A<T>::template B<T>);
220   int k = f(A<int>(), 0);
221 }
222 
223 namespace PR27601_RecursivelyInheritedBaseSpecializationsDeductionAmbiguity {
224 namespace ns1 {
225 
226 template<class...> struct B { };
227 template<class H, class ... Ts> struct B<H, Ts...> : B<> { };
228 template<class ... Ts> struct D : B<Ts...> { };
229 
230 template<class T, class ... Ts> void f(B<T, Ts...> &) { }
231 
232 int main() {
233   D<int, char> d;
234   f<int>(d);
235 }
236 } //end ns1
237 
238 namespace ns2 {
239 
240 template <int i, typename... Es> struct tup_impl;
241 
242 template <int i> struct tup_impl<i> {}; // empty tail
243 
244 template <int i, typename Head, typename... Tail>
245 struct tup_impl<i, Head, Tail...> : tup_impl<i + 1, Tail...> {
246   using value_type = Head;
247   Head head;
248 };
249 
250 template <typename... Es> struct tup : tup_impl<0, Es...> {};
251 
252 template <typename Head, int i, typename... Tail>
253 Head &get_helper(tup_impl<i, Head, Tail...> &t) {
254   return t.head;
255 }
256 
257 template <typename Head, int i, typename... Tail>
258 Head const &get_helper(tup_impl<i, Head, Tail...> const &t) {
259   return t.head;
260 }
261 
262 int main() {
263   tup<int, double, char> t;
264   get_helper<double>(t);
265   return 0;
266 }
267 } // end ns2
268 }
269 
270 namespace multiple_deduction_different_type {
271   template<typename T, T v> struct X {};
272   template<template<typename T, T> class X, typename T, typename U, int N>
273     void f(X<T, N>, X<U, N>) {} // expected-note 2{{values of conflicting types}}
274   template<template<typename T, T> class X, typename T, typename U, const int *N>
275     void g(X<T, N>, X<U, N>) {} // expected-note 0-2{{values of conflicting types}}
276   int n;
277   void h() {
278     f(X<int, 1+1>(), X<unsigned int, 3-1>()); // expected-error {{no matching function}}
279     f(X<unsigned int, 1+1>(), X<int, 3-1>()); // expected-error {{no matching function}}
280 #if __cplusplus > 201402L
281     g(X<const int*, &n>(), X<int*, &n + 1 - 1>()); // expected-error {{no matching function}}
282     g(X<int*, &n>(), X<const int*, &n + 1 - 1>()); // expected-error {{no matching function}}
283 #endif
284   }
285 
286   template<template<typename T, T> class X, typename T, typename U, T N>
287     void x(X<T, N>, int(*)[N], X<U, N>) {} // expected-note 1+{{candidate}}
288   template<template<typename T, T> class X, typename T, typename U, T N>
289     void x(int(*)[N], X<T, N>, X<U, N>) {} // expected-note 1+{{candidate}}
290   int arr[3];
291   void y() {
292     x(X<int, 3>(), &arr, X<int, 3>());
293     x(&arr, X<int, 3>(), X<int, 3>());
294 
295     x(X<int, 3>(), &arr, X<char, 3>()); // expected-error {{no matching function}}
296     x(&arr, X<int, 3>(), X<char, 3>()); // expected-error {{no matching function}}
297 
298     x(X<char, 3>(), &arr, X<char, 3>());
299     x(&arr, X<char, 3>(), X<char, 3>());
300   }
301 }
302 
303 namespace nullptr_deduction {
304   using nullptr_t = decltype(nullptr);
305 
306   template<typename T, T v> struct X {};
307   template<typename T, T v> void f(X<T, v>) {
308     static_assert(!v, ""); // expected-warning 2{{implicit conversion of nullptr constant to 'bool'}}
309   }
310   void g() {
311     f(X<int*, nullptr>()); // expected-note {{instantiation of}}
312     f(X<nullptr_t, nullptr>()); // expected-note {{instantiation of}}
313   }
314 
315   template<template<typename T, T> class X, typename T, int *P>
316     void f0(X<T, P>) {} // expected-note {{deduced non-type template argument does not have the same type as the corresponding template parameter ('nullptr_t' vs 'int *')}}
317   void h0() {
318     f0(X<int*, nullptr>());
319     f0(X<nullptr_t, nullptr>()); // expected-error {{no matching function}}
320   }
321 
322   template<template<typename T, T> class X, typename T, typename U, int *P>
323     void f1(X<T, P>, X<U, P>) {} // expected-note 2{{values of conflicting types}}
324   void h() {
325     f1(X<int*, nullptr>(), X<nullptr_t, nullptr>()); // expected-error {{no matching function}}
326     f1(X<nullptr_t, nullptr>(), X<int*, nullptr>()); // expected-error {{no matching function}}
327   }
328 
329   template<template<typename T, T> class X, typename T, typename U, nullptr_t P>
330     void f2(X<T, P>, X<U, P>) {} // expected-note 2{{values of conflicting types}}
331   void i() {
332     f2(X<int*, nullptr>(), X<nullptr_t, nullptr>()); // expected-error {{no matching function}}
333     f2(X<nullptr_t, nullptr>(), X<int*, nullptr>()); // expected-error {{no matching function}}
334   }
335 }
336 
337 namespace member_pointer {
338   struct A { void f(int); };
339   template<typename T, void (A::*F)(T)> struct B;
340   template<typename T> struct C;
341   template<typename T, void (A::*F)(T)> struct C<B<T, F>> {
342     C() { A a; T t; (a.*F)(t); }
343   };
344   C<B<int, &A::f>> c;
345 }
346 
347 namespace deduction_substitution_failure {
348   template<typename T> struct Fail { typedef typename T::error error; }; // expected-error 2{{prior to '::'}}
349 
350   template<typename T, typename U> struct A {};
351   template<typename T> struct A<T, typename Fail<T>::error> {}; // expected-note {{instantiation of}}
352   A<int, int> ai; // expected-note {{during template argument deduction for class template partial specialization 'A<T, typename Fail<T>::error>' [with T = int]}} expected-note {{in instantiation of template class 'deduction_substitution_failure::A<int, int>'}}
353 
354   template<typename T, typename U> int B; // expected-warning 0-1 {{extension}}
355   template<typename T> int B<T, typename Fail<T>::error> {}; // expected-note {{instantiation of}}
356   int bi = B<char, char>; // expected-note {{during template argument deduction for variable template partial specialization 'B<T, typename Fail<T>::error>' [with T = char]}}
357 }
358 
359 namespace deduction_after_explicit_pack {
360   template<typename ...T, typename U> int *f(T ...t, int &r, U *u) {
361     return u;
362   }
363   template<typename U, typename ...T> int *g(T ...t, int &r, U *u) {
364     return u;
365   }
366   void h(float a, double b, int c) {
367     f<float&, double&>(a, b, c, &c); // ok
368     g<int, float&, double&>(a, b, c, &c); // ok
369   }
370 
371   template<class... ExtraArgs>
372   int test(ExtraArgs..., unsigned vla_size, const char *input);
373   int n = test(0, "");
374 
375   template <typename... T> void i(T..., int, T..., ...); // expected-note 5{{deduced packs of different lengths}}
376   void j() {
377     i(0);
378     i(0, 1); // expected-error {{no match}}
379     i(0, 1, 2); // expected-error {{no match}}
380     i<>(0);
381     i<>(0, 1); // expected-error {{no match}}
382     i<>(0, 1, 2); // expected-error {{no match}}
383     i<int, int>(0, 1, 2, 3, 4);
384     i<int, int>(0, 1, 2, 3, 4, 5); // expected-error {{no match}}
385   }
386 
387   // GCC alarmingly accepts this by deducing T={int} by matching the second
388   // parameter against the first argument, then passing the first argument
389   // through the first parameter.
390   template<typename... T> struct X { X(int); operator int(); };
391   template<typename... T> void p(T..., X<T...>, ...); // expected-note {{deduced packs of different lengths for parameter 'T' (<> vs. <int>)}}
392   void q() { p(X<int>(0), 0); } // expected-error {{no match}}
393 
394   struct A {
395     template <typename T> void f(T, void *, int = 0); // expected-note 2{{no known conversion from 'double' to 'void *' for 2nd argument}}
396     void f(); // expected-note 2{{requires 0}}
397 
398     template <typename T> static void g(T, void *, int = 0); // expected-note 2{{no known conversion from 'double' to 'void *' for 2nd argument}}
399     void g(); // expected-note 2{{requires 0}}
400 
401     void h() {
402       f(1.0, 2.0); // expected-error {{no match}}
403       g(1.0, 2.0); // expected-error {{no match}}
404     }
405   };
406   void f(A a) {
407     a.f(1.0, 2.0); // expected-error {{no match}}
408     a.g(1.0, 2.0); // expected-error {{no match}}
409   }
410 }
411 
412 namespace overload_vs_pack {
413   void f(int);
414   void f(float);
415   void g(double);
416 
417   template<typename ...T> struct X {};
418   template<typename ...T> void x(T...);
419 
420   template<typename ...T> struct Y { typedef int type(typename T::error...); };
421   template<> struct Y<int, float, double> { typedef int type; };
422 
423   template<typename ...T> typename Y<T...>::type g1(X<T...>, void (*...fns)(T)); // expected-note {{deduced conflicting types for parameter 'T' (<int, float> vs. <(no value), double>)}}
424   template<typename ...T> typename Y<T...>::type g2(void(*)(T...), void (*...fns)(T)); // expected-note {{deduced conflicting types for parameter 'T' (<int, float> vs. <(no value), double>)}}
425 
426   template<typename T> int &h1(decltype(g1(X<int, float, T>(), f, f, g)) *p);
427   template<typename T> float &h1(...);
428 
429   template<typename T> int &h2(decltype(g2(x<int, float, T>, f, f, g)) *p);
430   template<typename T> float &h2(...);
431 
432   int n1 = g1(X<int, float>(), f, g); // expected-error {{no matching function}}
433   int n2 = g2(x<int, float>, f, g); // expected-error {{no matching function}}
434 
435   int &a1 = h1<double>(0); // ok, skip deduction for 'f's, deduce matching value from 'g'
436   int &a2 = h2<double>(0);
437 
438   float &b1 = h1<float>(0); // deduce mismatching value from 'g', so we do not trigger instantiation of Y
439   float &b2 = h2<float>(0);
440 
441   template<typename ...T> int partial_deduction(void (*...f)(T)); // expected-note {{deduced incomplete pack <(no value), double> for template parameter 'T'}}
442   int pd1 = partial_deduction(f, g); // expected-error {{no matching function}}
443 
444   template<typename ...T> int partial_deduction_2(void (*...f)(T), ...); // expected-note {{deduced incomplete pack <(no value), double> for template parameter 'T'}}
445   int pd2 = partial_deduction_2(f, g); // expected-error {{no matching function}}
446 
447   namespace cwg_example {
448     void f(char, char);
449     void f(int, int);
450     void x(int, char);
451 
452     template<typename T, typename ...U> void j(void(*)(U...), void (*...fns)(T, U));
453     void test() { j(x, f, x); }
454   }
455 }
456 
457 namespace b29946541 {
458   template<typename> class A {};
459   template<typename T, typename U, template<typename, typename> class C>
460   void f(C<T, U>); // expected-note {{failed template argument deduction}}
461   void g(A<int> a) { f(a); } // expected-error {{no match}}
462 }
463 
464 namespace deduction_from_empty_list {
465   template<int M, int N = 5> void f(int (&&)[N], int (&&)[N]) { // expected-note {{1 vs. 2}}
466     static_assert(M == N, "");
467   }
468 
469   void test() {
470     f<5>({}, {});
471     f<1>({}, {0});
472     f<1>({0}, {});
473     f<1>({0}, {0});
474     f<1>({0}, {0, 1}); // expected-error {{no matching}}
475   }
476 }
477 
478 namespace check_extended_pack {
479   template<typename T> struct X { typedef int type; };
480   template<typename ...T> void f(typename X<T>::type...);
481   template<typename T> void f(T, int, int);
482   void g() {
483     f<int>(0, 0, 0);
484   }
485 
486   template<int, int*> struct Y {};
487   template<int ...N> void g(Y<N...>); // expected-note {{deduced non-type template argument does not have the same type as the corresponding template parameter ('int *' vs 'int')}}
488   int n;
489   void h() { g<0>(Y<0, &n>()); } // expected-error {{no matching function}}
490 }
491 
492 namespace dependent_template_template_param_non_type_param_type {
493   template<int N> struct A {
494     template<typename V = int, V M = 12, V (*Y)[M], template<V (*v)[M]> class W>
495     A(W<Y>);
496   };
497 
498   int n[12];
499   template<int (*)[12]> struct Q {};
500   Q<&n> qn;
501   A<0> a(qn);
502 }
503 
504 namespace dependent_list_deduction {
505   template<typename T, T V> void a(const int (&)[V]) {
506     static_assert(is_same<T, decltype(sizeof(0))>::value, "");
507     static_assert(V == 3, "");
508   }
509   template<typename T, T V> void b(const T (&)[V]) {
510     static_assert(is_same<T, int>::value, "");
511     static_assert(V == 3, "");
512   }
513   template<typename T, T V> void c(const T (&)[V]) {
514     static_assert(is_same<T, decltype(sizeof(0))>::value, "");
515     static_assert(V == 3, "");
516   }
517   void d() {
518     a({1, 2, 3});
519 #if __cplusplus <= 201402L
520     // expected-error@-2 {{no match}} expected-note@-15 {{couldn't infer template argument 'T'}}
521 #endif
522     b({1, 2, 3});
523     c({{}, {}, {}});
524 #if __cplusplus <= 201402L
525     // expected-error@-2 {{no match}} expected-note@-12 {{couldn't infer template argument 'T'}}
526 #endif
527   }
528 
529   template<typename ...T> struct X;
530   template<int ...T> struct Y;
531   template<typename ...T, T ...V> void f(const T (&...p)[V]) {
532     static_assert(is_same<X<T...>, X<int, char, char>>::value, "");
533     static_assert(is_same<Y<V...>, Y<3, 2, 4>>::value, "");
534   }
535   template<typename ...T, T ...V> void g(const T (&...p)[V]) {
536     static_assert(is_same<X<T...>, X<int, decltype(sizeof(0))>>::value, "");
537     static_assert(is_same<Y<V...>, Y<2, 3>>::value, "");
538   }
539   void h() {
540     f({1, 2, 3}, {'a', 'b'}, "foo");
541     g({1, 2}, {{}, {}, {}});
542 #if __cplusplus <= 201402
543     // expected-error@-2 {{no match}}
544     // expected-note@-9 {{deduced incomplete pack}}
545     // We deduce V$1 = (size_t)3, which in C++1z also deduces T$1 = size_t.
546 #endif
547   }
548 }
549 
550 namespace designators {
551   template<typename T, int N> constexpr int f(T (&&)[N]) { return N; } // expected-note 2{{couldn't infer template argument 'T'}}
552   static_assert(f({1, 2, [20] = 3}) == 3, ""); // expected-error {{no matching function}} expected-warning 2{{C99}} expected-note {{}}
553 
554   static_assert(f({.a = 1, .b = 2}) == 3, ""); // expected-error {{no matching function}}
555 }
556 
557 namespace nested_packs {
558   template<typename ...T, typename ...U> void f(T (*...f)(U...)); // expected-note {{deduced packs of different lengths for parameter 'U' (<> vs. <int>)}}
559   void g() { f(g); f(g, g); f(g, g, g); }
560   void h(int) { f(h); f(h, h); f(h, h, h); }
561   void i() { f(g, h); } // expected-error {{no matching function}}
562 
563 #if __cplusplus >= 201703L
564   template<auto ...A> struct Q {};
565   template<typename ...T, T ...A, T ...B> void q(Q<A...>, Q<B...>); // #q
566   void qt(Q<> q0, Q<1, 2> qii, Q<1, 2, 3> qiii) {
567     q(q0, q0);
568     q(qii, qii);
569     q(qii, qiii); // expected-error {{no match}} expected-note@#q {{deduced packs of different lengths for parameter 'T' (<int, int> vs. <int, int, int>)}}
570     q(q0, qiii); // expected-error {{no match}} expected-note@#q {{deduced packs of different lengths for parameter 'T' (<> vs. <int, int, int>)}}
571   }
572 #endif
573 }
574 
575 namespace PR44890 {
576   template<typename ...Ts>
577     struct tuple {};
578 
579   template<int I, typename ...Ts>
580     int get0(const tuple<Ts...> &t) { return 0; }
581 
582   template<typename ...Ts> struct tuple_wrapper : tuple<Ts...> {
583     template<int I> int get() { return get0<0, Ts...>(*this); }
584   };
585 
586   int f() {
587     tuple_wrapper<int> w;
588     return w.get<0>();
589   }
590 }
591 
592 namespace merge_size_only_deductions {
593 #if __cplusplus >= 201703L
594   // Based on a testcase by Hubert Tong.
595   template<typename ...> struct X {};
596   template<auto ...> struct Y {};
597   template<typename T> struct id { using Type = T; };
598 
599   template<typename ...T, typename T::Type ...V>
600     int f(X<char [V] ...>, Y<V ...>, X<T ...>);
601 
602   using size_t = __SIZE_TYPE__;
603   int a = f(X<char [1], char [2]>(), Y<(size_t)1, (size_t)2>(), X<id<size_t>, id<size_t>>());
604   int b = f(X<char [1], char [2]>(), Y<1, 2>(), X<id<int>, id<int>>());
605 #endif
606 }
607