1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
2 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++1z
4 // Template argument deduction with template template parameters.
5 template<typename T, template<T> class A>
7 static const unsigned value = 0;
10 template<template<int> class A>
12 static const unsigned value = 1;
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];
20 template<typename T, typename U>
22 static const bool value = false;
26 struct is_same<T, T> {
27 static const bool value = true;
30 template<typename T> struct allocator { };
31 template<typename T, typename Alloc = allocator<T> > struct vector {};
33 // Fun with meta-lambdas!
37 // Replaces all occurrences of _1 with Arg1 and _2 with Arg2 in T.
38 template<typename T, typename Arg1, typename Arg2>
43 // Replacement of the whole type.
44 template<typename Arg1, typename Arg2>
45 struct Replace<_1, Arg1, Arg2> {
49 template<typename Arg1, typename Arg2>
50 struct Replace<_2, Arg1, Arg2> {
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;
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;
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;
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;
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];
87 template <typename T, int N> void f(const T (&a)[N]);
89 void test_PR5911() { f(iarr); }
91 // Must not examine base classes of incomplete type during template argument
94 template <typename T> struct X {
95 template <typename U> X(const X<U>& u);
99 void f(const X<A>& a);
100 void test(A& a) { (void)f(a); }
106 template <typename T_> void g (T_&); // expected-note{{T_ = int}}
107 void h (void) { g(f()); } // expected-error{{no matching function for call}}
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'}}
114 make(char_maker); // expected-error {{no matching function for call to 'make'}}
119 template<typename T> void foo(const T a[3][3]);
128 template<typename T> struct Const { typedef void const type; };
130 template<typename T> void f(T, typename Const<T>::type*);
131 template<typename T> void f(T, void const *);
139 // rdar://problem/8537391
142 template <void F(char)> static inline void foo();
146 template<typename T> static inline void wobble(T ch);
149 static void madness() {
150 Foo::foo<wobble<char> >();
155 // Verify that we can deduce enum-typed arguments correctly.
158 template <E> struct A {};
159 template <E e> void foo(const A<e> &a) {}
168 template<typename ...T> struct X;
169 template<typename A, typename ...B> struct S {
170 static_assert(sizeof...(B) == 1, "");
179 template<typename ...T> void f(S<T...>);
180 void g() { f(S<int, int>()); }
184 template <template<typename...> class C, typename ...Us> struct BindBack {
185 template <typename ...Ts> using apply = C<Ts..., Us...>;
187 template <typename, typename...> struct Y;
188 template <typename ...Ts> using Z = Y<Ts...>;
190 using T = BindBack<Z, int>::apply<>;
193 using U = BindBack<Z, int, int>::apply<char>;
194 using U = Z<char, int, int>;
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}}
203 using T = S<int, int>;
208 template<typename F> F Quux(F &&f);
209 auto Baz = Quux(Quux<float>);
212 namespace NonDeducedNestedNameSpecifier {
213 template<typename T> struct A {
214 template<typename U> struct B {
219 template<typename T> int f(A<T>, typename A<T>::template B<T>);
220 int k = f(A<int>(), 0);
223 namespace PR27601_RecursivelyInheritedBaseSpecializationsDeductionAmbiguity {
226 template<class...> struct B { };
227 template<class H, class ... Ts> struct B<H, Ts...> : B<> { };
228 template<class ... Ts> struct D : B<Ts...> { };
230 template<class T, class ... Ts> void f(B<T, Ts...> &) { }
240 template <int i, typename... Es> struct tup_impl;
242 template <int i> struct tup_impl<i> {}; // empty tail
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;
250 template <typename... Es> struct tup : tup_impl<0, Es...> {};
252 template <typename Head, int i, typename... Tail>
253 Head &get_helper(tup_impl<i, Head, Tail...> &t) {
257 template <typename Head, int i, typename... Tail>
258 Head const &get_helper(tup_impl<i, Head, Tail...> const &t) {
263 tup<int, double, char> t;
264 get_helper<double>(t);
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}}
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}}
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}}
292 x(X<int, 3>(), &arr, X<int, 3>());
293 x(&arr, X<int, 3>(), X<int, 3>());
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}}
298 x(X<char, 3>(), &arr, X<char, 3>());
299 x(&arr, X<char, 3>(), X<char, 3>());
303 namespace nullptr_deduction {
304 using nullptr_t = decltype(nullptr);
306 template<typename T, T v> struct X {};
307 template<typename T, T v> void f(X<T, v>) {
308 static_assert(!v, "");
311 f(X<int*, nullptr>());
312 f(X<nullptr_t, nullptr>());
315 template<template<typename T, T> class X, typename T, typename U, int *P>
316 void f1(X<T, P>, X<U, P>) {} // expected-note 2{{values of conflicting types}}
318 f1(X<int*, nullptr>(), X<nullptr_t, nullptr>()); // expected-error {{no matching function}}
319 f1(X<nullptr_t, nullptr>(), X<int*, nullptr>()); // expected-error {{no matching function}}
322 template<template<typename T, T> class X, typename T, typename U, nullptr_t P>
323 void f2(X<T, P>, X<U, P>) {} // expected-note 2{{values of conflicting types}}
325 f2(X<int*, nullptr>(), X<nullptr_t, nullptr>()); // expected-error {{no matching function}}
326 f2(X<nullptr_t, nullptr>(), X<int*, nullptr>()); // expected-error {{no matching function}}
330 namespace member_pointer {
331 struct A { void f(int); };
332 template<typename T, void (A::*F)(T)> struct B;
333 template<typename T> struct C;
334 template<typename T, void (A::*F)(T)> struct C<B<T, F>> {
335 C() { A a; T t; (a.*F)(t); }
340 namespace deduction_substitution_failure {
341 template<typename T> struct Fail { typedef typename T::error error; }; // expected-error 2{{prior to '::'}}
343 template<typename T, typename U> struct A {};
344 template<typename T> struct A<T, typename Fail<T>::error> {}; // expected-note {{instantiation of}}
345 A<int, int> ai; // expected-note {{during template argument deduction for class template partial specialization 'A<T, typename Fail<T>::error>' [with T = int]}}
347 template<typename T, typename U> int B; // expected-warning 0-1 {{extension}}
348 template<typename T> int B<T, typename Fail<T>::error> {}; // expected-note {{instantiation of}}
349 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]}}