Clang Project

clang_source_code/test/SemaTemplate/pack-deduction.cpp
1// RUN: %clang_cc1 -std=c++11 -verify %s
2
3template<typename ...T> struct X {};
4
5template<typename T, typename U> struct P {};
6
7namespace Nested {
8  template<typename ...T> int f1(X<T, T...>... a); // expected-note +{{conflicting types for parameter 'T'}}
9  template<typename ...T> int f2(P<X<T...>, T> ...a); // expected-note +{{conflicting types 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
22namespace 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
34namespace 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
41namespace 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
69namespace 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
132namespace 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
158namespace 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
170namespace Nested_Explicit_Specialization {
171template <typename>
172struct 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
184void Run() {
185  Outer<void>::Inner<0>().Test(1,1);
186}
187}
188