annotate clang/test/SemaTemplate/pack-deduction.cpp @ 236:c4bab56944e8 llvm-original

LLVM 16
author kono
date Wed, 09 Nov 2022 17:45:10 +0900
parents 1d019706d866
children 1f2b6ac9f198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -std=c++11 -verify %s
anatofuz
parents:
diff changeset
2
anatofuz
parents:
diff changeset
3 template<typename ...T> struct X {};
anatofuz
parents:
diff changeset
4
anatofuz
parents:
diff changeset
5 template<typename T, typename U> struct P {};
anatofuz
parents:
diff changeset
6
anatofuz
parents:
diff changeset
7 namespace Nested {
anatofuz
parents:
diff changeset
8 template<typename ...T> int f1(X<T, T...>... a); // expected-note +{{packs of different lengths for parameter 'T'}}
anatofuz
parents:
diff changeset
9 template<typename ...T> int f2(P<X<T...>, T> ...a); // expected-note +{{packs of different lengths for parameter 'T'}}
anatofuz
parents:
diff changeset
10
anatofuz
parents:
diff changeset
11 int a1 = f1(X<int, int, double>(), X<double, int, double>());
anatofuz
parents:
diff changeset
12 int a2 = f1(X<int, int>());
anatofuz
parents:
diff changeset
13 int a3 = f1(X<int>(), X<double>()); // expected-error {{no matching}}
anatofuz
parents:
diff changeset
14 int a4 = f1(X<int, int>(), X<int>()); // expected-error {{no matching}}
anatofuz
parents:
diff changeset
15 int a5 = f1(X<int>(), X<int, int>()); // expected-error {{no matching}}
anatofuz
parents:
diff changeset
16 int a6 = f1(X<int, int, int>(), X<int, int, int>(), X<int, int, int, int>()); // expected-error {{no matching}}
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 int b1 = f2(P<X<int, double>, int>(), P<X<int, double>, double>());
anatofuz
parents:
diff changeset
19 int b2 = f2(P<X<int, double>, int>(), P<X<int, double>, double>(), P<X<int, double>, char>()); // expected-error {{no matching}}
anatofuz
parents:
diff changeset
20 }
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 namespace PR14841 {
anatofuz
parents:
diff changeset
23 template<typename T, typename U> struct A {};
anatofuz
parents:
diff changeset
24 template<typename ...Ts> void f(A<Ts...>); // expected-note {{substitution failure [with Ts = <char, short, int>]: too many template arg}}
anatofuz
parents:
diff changeset
25
anatofuz
parents:
diff changeset
26 void g(A<char, short> a) {
anatofuz
parents:
diff changeset
27 f(a);
anatofuz
parents:
diff changeset
28 f<char>(a);
anatofuz
parents:
diff changeset
29 f<char, short>(a);
anatofuz
parents:
diff changeset
30 f<char, short, int>(a); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
31 }
anatofuz
parents:
diff changeset
32 }
anatofuz
parents:
diff changeset
33
anatofuz
parents:
diff changeset
34 namespace RetainExprPacks {
anatofuz
parents:
diff changeset
35 int f(int a, int b, int c);
anatofuz
parents:
diff changeset
36 template<typename ...Ts> struct X {};
anatofuz
parents:
diff changeset
37 template<typename ...Ts> int g(X<Ts...>, decltype(f(Ts()...)));
anatofuz
parents:
diff changeset
38 int n = g<int, int>(X<int, int, int>(), 0);
anatofuz
parents:
diff changeset
39 }
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 namespace PR14615 {
anatofuz
parents:
diff changeset
42 namespace comment0 {
anatofuz
parents:
diff changeset
43 template <class A, class...> struct X {};
anatofuz
parents:
diff changeset
44 template <class... B> struct X<int, B...> {
anatofuz
parents:
diff changeset
45 typedef int type;
anatofuz
parents:
diff changeset
46 struct valid {};
anatofuz
parents:
diff changeset
47 };
anatofuz
parents:
diff changeset
48 template <typename A, typename... B, typename T = X<A, B...>,
anatofuz
parents:
diff changeset
49 typename = typename T::valid>
anatofuz
parents:
diff changeset
50 typename T::type check(int);
anatofuz
parents:
diff changeset
51 int i = check<int, char>(1);
anatofuz
parents:
diff changeset
52 }
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 namespace comment2 {
anatofuz
parents:
diff changeset
55 template <class...> struct X;
anatofuz
parents:
diff changeset
56 template <typename... B, typename X<B...>::type I = 0>
anatofuz
parents:
diff changeset
57 char check(B...); // expected-note {{undefined template 'PR14615::comment2::X<char, int>'}}
anatofuz
parents:
diff changeset
58 void f() { check<char>(1, 2); } // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
59 }
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 namespace comment3 {
anatofuz
parents:
diff changeset
62 template <class...> struct X;
anatofuz
parents:
diff changeset
63 template <typename... B, typename X<B...>::type I = (typename X<B...>::type)0>
anatofuz
parents:
diff changeset
64 char check(B...); // expected-note {{undefined template 'PR14615::comment3::X<char, int>'}}
anatofuz
parents:
diff changeset
65 void f() { check<char>(1, 2); } // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
66 }
anatofuz
parents:
diff changeset
67 }
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 namespace fully_expanded_packs {
anatofuz
parents:
diff changeset
70 template<typename ...T> struct A {
anatofuz
parents:
diff changeset
71 template<T ...X> static constexpr int f() {
anatofuz
parents:
diff changeset
72 // expected-note@-1 1+{{deduced too few arguments for expanded pack 'X'}}
anatofuz
parents:
diff changeset
73 // expected-note@-2 1+{{too many template arguments}}
anatofuz
parents:
diff changeset
74 return (X + ... + 0); // expected-warning {{extension}}
anatofuz
parents:
diff changeset
75 }
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 template<T ...X, int Y> static constexpr int g() {
anatofuz
parents:
diff changeset
78 // expected-note@-1 1+{{deduced too few arguments for expanded pack 'X'}}
anatofuz
parents:
diff changeset
79 // expected-note@-2 1+{{couldn't infer template argument 'Y'}}
anatofuz
parents:
diff changeset
80 // expected-note@-3 1+{{too many template arguments}}
anatofuz
parents:
diff changeset
81 return (X + ... + (1000 * Y)); // expected-warning {{extension}}
anatofuz
parents:
diff changeset
82 }
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 template<T ...X, int Y, T ...Z> static constexpr int h() {
anatofuz
parents:
diff changeset
85 // expected-note@-1 1+{{deduced too few arguments for expanded pack 'X'}}
anatofuz
parents:
diff changeset
86 // expected-note@-2 1+{{couldn't infer template argument 'Y'}}
anatofuz
parents:
diff changeset
87 // expected-note@-3 1+{{deduced too few arguments for expanded pack 'Z'}}
anatofuz
parents:
diff changeset
88 // expected-note@-4 1+{{too many template arguments}}
anatofuz
parents:
diff changeset
89 return (X + ... + (1000 * Y)) + 1000000 * (Z + ... + 0); // expected-warning 2{{extension}}
anatofuz
parents:
diff changeset
90 }
anatofuz
parents:
diff changeset
91
anatofuz
parents:
diff changeset
92 template<T ...X, int ...Z> static constexpr int i() {
anatofuz
parents:
diff changeset
93 return (X + ... + 0) + 1000 * (Z + ... + 0); // expected-warning 2{{extension}}
anatofuz
parents:
diff changeset
94 }
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 template<T ...X, int Y, int ...Z> static constexpr int j() {
anatofuz
parents:
diff changeset
97 return (X + ... + (1000 * Y)) + 1000000 * (Z + ... + 0); // expected-warning 2{{extension}}
anatofuz
parents:
diff changeset
98 }
anatofuz
parents:
diff changeset
99 };
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 void check_invalid_calls() {
anatofuz
parents:
diff changeset
102 A<int, int>::f(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
103 A<int, int>::f<>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
104 A<int, int>::f<0>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
105 A<int, int>::g(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
106 A<int, int>::g<>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
107 A<int, int>::g<0>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
108 A<int, int>::g<0, 0>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
109 A<>::f<0>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
110 A<>::g(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
111 A<>::g<>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
112 A<>::g<0, 0>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
113 A<>::h<>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
114 A<int>::h<>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
115 A<int>::h<0, 0>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
116 A<>::h<0, 0>(); // expected-error {{no matching function}}
anatofuz
parents:
diff changeset
117 }
anatofuz
parents:
diff changeset
118
anatofuz
parents:
diff changeset
119 static_assert(A<>::f() == 0, "");
anatofuz
parents:
diff changeset
120 static_assert(A<int>::f<1>() == 1, "");
anatofuz
parents:
diff changeset
121 static_assert(A<>::g<1>() == 1000, "");
anatofuz
parents:
diff changeset
122 static_assert(A<int>::g<1, 2>() == 2001, "");
anatofuz
parents:
diff changeset
123 static_assert(A<>::h<1>() == 1000, "");
anatofuz
parents:
diff changeset
124 static_assert(A<int>::h<1, 2, 3>() == 3002001, "");
anatofuz
parents:
diff changeset
125 static_assert(A<int, int>::h<1, 20, 3, 4, 50>() == 54003021, "");
anatofuz
parents:
diff changeset
126 static_assert(A<>::i<1>() == 1000, "");
anatofuz
parents:
diff changeset
127 static_assert(A<int>::i<1>() == 1, "");
anatofuz
parents:
diff changeset
128 static_assert(A<>::j<1, 2, 30>() == 32001000, "");
anatofuz
parents:
diff changeset
129 static_assert(A<int>::j<1, 2, 3, 40>() == 43002001, "");
anatofuz
parents:
diff changeset
130 }
anatofuz
parents:
diff changeset
131
anatofuz
parents:
diff changeset
132 namespace partial_full_mix {
anatofuz
parents:
diff changeset
133 template<typename T, typename U> struct pair {};
anatofuz
parents:
diff changeset
134 template<typename ...T> struct tuple {};
anatofuz
parents:
diff changeset
135 template<typename ...T> struct A {
anatofuz
parents:
diff changeset
136 template<typename ...U> static pair<tuple<T...>, tuple<U...>> f(pair<T, U> ...p);
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
137 // expected-note@-1 {{[with U = <char, double, long>]: pack expansion contains parameter packs 'T' and 'U' that have different lengths (2 vs. 3)}}
150
anatofuz
parents:
diff changeset
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}}
anatofuz
parents:
diff changeset
139
anatofuz
parents:
diff changeset
140 template<typename ...U> static pair<tuple<T...>, tuple<U...>> g(pair<T, U> ...p, ...);
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
141 // expected-note@-1 {{[with U = <char, double, long>]: pack expansion contains parameter packs 'T' and 'U' that have different lengths (2 vs. 3)}}
150
anatofuz
parents:
diff changeset
142
anatofuz
parents:
diff changeset
143 template<typename ...U> static tuple<U...> h(tuple<pair<T, U>..., pair<int, int>>);
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
144 // expected-note@-1 {{[with U = <int[2]>]: pack expansion contains parameter packs 'T' and 'U' that have different lengths (2 vs. 1)}}
150
anatofuz
parents:
diff changeset
145 };
anatofuz
parents:
diff changeset
146
anatofuz
parents:
diff changeset
147 pair<tuple<int, float>, tuple<char, double>> k1 = A<int, float>().f<char>(pair<int, char>(), pair<float, double>());
anatofuz
parents:
diff changeset
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}}
anatofuz
parents:
diff changeset
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}}
anatofuz
parents:
diff changeset
150
anatofuz
parents:
diff changeset
151 // FIXME: We should accept this by treating the pack 'p' as having a fixed length of 2 here.
anatofuz
parents:
diff changeset
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}}
anatofuz
parents:
diff changeset
153
anatofuz
parents:
diff changeset
154 // FIXME: We should accept this by treating the pack of pairs as having a fixed length of 2 here.
anatofuz
parents:
diff changeset
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}}
anatofuz
parents:
diff changeset
156 }
anatofuz
parents:
diff changeset
157
anatofuz
parents:
diff changeset
158 namespace substitution_vs_function_deduction {
anatofuz
parents:
diff changeset
159 template <typename... T> struct A {
anatofuz
parents:
diff changeset
160 template <typename... U> void f(void(*...)(T, U)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}
anatofuz
parents:
diff changeset
161 template <typename... U> void g(void...(T, U)); // expected-note {{could not match 'void (T, U)' against 'void (*)(int, int)'}}
anatofuz
parents:
diff changeset
162 };
anatofuz
parents:
diff changeset
163 void f(int, int) {
anatofuz
parents:
diff changeset
164 A<int>().f(f);
anatofuz
parents:
diff changeset
165 // FIXME: We fail to decay the parameter to a pointer type.
anatofuz
parents:
diff changeset
166 A<int>().g(f); // expected-error {{no match}}
anatofuz
parents:
diff changeset
167 }
anatofuz
parents:
diff changeset
168 }
anatofuz
parents:
diff changeset
169
anatofuz
parents:
diff changeset
170 namespace Nested_Explicit_Specialization {
anatofuz
parents:
diff changeset
171 template <typename>
anatofuz
parents:
diff changeset
172 struct Outer {
anatofuz
parents:
diff changeset
173
anatofuz
parents:
diff changeset
174 template <int>
anatofuz
parents:
diff changeset
175 struct Inner;
anatofuz
parents:
diff changeset
176
anatofuz
parents:
diff changeset
177 template <>
anatofuz
parents:
diff changeset
178 struct Inner<0> {
anatofuz
parents:
diff changeset
179 template <typename... Args>
anatofuz
parents:
diff changeset
180 void Test(Args...) {}
anatofuz
parents:
diff changeset
181 };
anatofuz
parents:
diff changeset
182 };
anatofuz
parents:
diff changeset
183
anatofuz
parents:
diff changeset
184 void Run() {
anatofuz
parents:
diff changeset
185 Outer<void>::Inner<0>().Test(1,1);
anatofuz
parents:
diff changeset
186 }
anatofuz
parents:
diff changeset
187 }