xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/instantiate-member-template.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc template<typename T>
4*f4a2713aSLionel Sambuc struct X0 {
5*f4a2713aSLionel Sambuc   template<typename U> T f0(U);
6*f4a2713aSLionel Sambuc   template<typename U> U& f1(T*, U); // expected-error{{pointer to a reference}} \
7*f4a2713aSLionel Sambuc                                      // expected-note{{candidate}}
8*f4a2713aSLionel Sambuc };
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc X0<int> x0i;
11*f4a2713aSLionel Sambuc X0<void> x0v;
12*f4a2713aSLionel Sambuc X0<int&> x0ir; // expected-note{{instantiation}}
13*f4a2713aSLionel Sambuc 
test_X0(int * ip,double * dp)14*f4a2713aSLionel Sambuc void test_X0(int *ip, double *dp) {
15*f4a2713aSLionel Sambuc   X0<int> xi;
16*f4a2713aSLionel Sambuc   int i1 = xi.f0(ip);
17*f4a2713aSLionel Sambuc   double *&dpr = xi.f1(ip, dp);
18*f4a2713aSLionel Sambuc   xi.f1(dp, dp); // expected-error{{no matching}}
19*f4a2713aSLionel Sambuc 
20*f4a2713aSLionel Sambuc   X0<void> xv;
21*f4a2713aSLionel Sambuc   double *&dpr2 = xv.f1(ip, dp);
22*f4a2713aSLionel Sambuc }
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc template<typename T>
25*f4a2713aSLionel Sambuc struct X1 {
26*f4a2713aSLionel Sambuc   template<typename U>
27*f4a2713aSLionel Sambuc   struct Inner0 {
28*f4a2713aSLionel Sambuc     U x;
29*f4a2713aSLionel Sambuc     T y; // expected-error{{void}}
30*f4a2713aSLionel Sambuc   };
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc   template<typename U>
33*f4a2713aSLionel Sambuc   struct Inner1 {
34*f4a2713aSLionel Sambuc     U x; // expected-error{{void}}
35*f4a2713aSLionel Sambuc     T y;
36*f4a2713aSLionel Sambuc   };
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc   template<typename U>
39*f4a2713aSLionel Sambuc   struct Inner2 {
40*f4a2713aSLionel Sambuc     struct SuperInner {
41*f4a2713aSLionel Sambuc       U z; // expected-error{{void}}
42*f4a2713aSLionel Sambuc     };
43*f4a2713aSLionel Sambuc   };
44*f4a2713aSLionel Sambuc 
45*f4a2713aSLionel Sambuc   template<typename U>
46*f4a2713aSLionel Sambuc   struct Inner3 {
f0X1::Inner347*f4a2713aSLionel Sambuc     void f0(T t, U u) { // expected-note{{passing argument to parameter 't' here}}
48*f4a2713aSLionel Sambuc       (void)(t + u); // expected-error{{invalid operands}}
49*f4a2713aSLionel Sambuc     }
50*f4a2713aSLionel Sambuc 
51*f4a2713aSLionel Sambuc     template<typename V>
f1X1::Inner352*f4a2713aSLionel Sambuc     V f1(T t, U u, V) {
53*f4a2713aSLionel Sambuc       return t + u; // expected-error{{cannot initialize return object}}
54*f4a2713aSLionel Sambuc     }
55*f4a2713aSLionel Sambuc   };
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc   template<typename U>
58*f4a2713aSLionel Sambuc   struct Inner4;
59*f4a2713aSLionel Sambuc };
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc template<typename T>
62*f4a2713aSLionel Sambuc template<typename U>
63*f4a2713aSLionel Sambuc struct X1<T>::Inner4 {
64*f4a2713aSLionel Sambuc   template<typename V>
65*f4a2713aSLionel Sambuc   V f2(T t, U u, V);
66*f4a2713aSLionel Sambuc 
67*f4a2713aSLionel Sambuc   static U value;
68*f4a2713aSLionel Sambuc };
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc template<typename T>
71*f4a2713aSLionel Sambuc template<typename U>
72*f4a2713aSLionel Sambuc U X1<T>::Inner4<U>::value; // expected-error{{reference variable}}
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc template<typename T>
75*f4a2713aSLionel Sambuc template<typename U>
76*f4a2713aSLionel Sambuc template<typename V>
f2(T t,U u,V)77*f4a2713aSLionel Sambuc V X1<T>::Inner4<U>::f2(T t, U u, V) {
78*f4a2713aSLionel Sambuc   return t + u; // expected-error{{cannot initialize return object}}
79*f4a2713aSLionel Sambuc }
80*f4a2713aSLionel Sambuc 
test_X1(int * ip,int i,double * dp)81*f4a2713aSLionel Sambuc void test_X1(int *ip, int i, double *dp) {
82*f4a2713aSLionel Sambuc   X1<void>::Inner0<int> *xvip; // okay
83*f4a2713aSLionel Sambuc   X1<void>::Inner0<int> xvi; // expected-note{{instantiation}}
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc   X1<int>::Inner1<void> *xivp; // okay
86*f4a2713aSLionel Sambuc   X1<int>::Inner1<void> xiv; // expected-note{{instantiation}}
87*f4a2713aSLionel Sambuc 
88*f4a2713aSLionel Sambuc   X1<int>::Inner2<void>::SuperInner *xisivp; // okay
89*f4a2713aSLionel Sambuc   X1<int>::Inner2<void>::SuperInner xisiv; // expected-note{{instantiation}}
90*f4a2713aSLionel Sambuc 
91*f4a2713aSLionel Sambuc   X1<int*>::Inner3<int> id3;
92*f4a2713aSLionel Sambuc   id3.f0(ip, i);
93*f4a2713aSLionel Sambuc   id3.f0(dp, i); // expected-error{{cannot initialize a parameter of type 'int *' with an lvalue of type 'double *'}}
94*f4a2713aSLionel Sambuc   id3.f1(ip, i, ip);
95*f4a2713aSLionel Sambuc   id3.f1(ip, i, dp); // expected-note{{instantiation}}
96*f4a2713aSLionel Sambuc 
97*f4a2713aSLionel Sambuc   X1<int*>::Inner3<double*> id3b;
98*f4a2713aSLionel Sambuc   id3b.f0(ip, dp); // expected-note{{instantiation}}
99*f4a2713aSLionel Sambuc 
100*f4a2713aSLionel Sambuc   X1<int*>::Inner4<int> id4;
101*f4a2713aSLionel Sambuc   id4.f2(ip, i, dp); // expected-note{{instantiation}}
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc   X1<int*>::Inner4<int>::value = 17;
104*f4a2713aSLionel Sambuc   i = X1<int*>::Inner4<int&>::value; // expected-note{{instantiation}}
105*f4a2713aSLionel Sambuc }
106*f4a2713aSLionel Sambuc 
107*f4a2713aSLionel Sambuc 
108*f4a2713aSLionel Sambuc template<typename T>
109*f4a2713aSLionel Sambuc struct X2 {
110*f4a2713aSLionel Sambuc   template<T *Ptr> // expected-error{{pointer to a reference}}
111*f4a2713aSLionel Sambuc   struct Inner;
112*f4a2713aSLionel Sambuc 
113*f4a2713aSLionel Sambuc   template<T Value> // expected-error{{cannot have type 'float'}}
114*f4a2713aSLionel Sambuc   struct Inner2;
115*f4a2713aSLionel Sambuc };
116*f4a2713aSLionel Sambuc 
117*f4a2713aSLionel Sambuc X2<int&> x2a; // expected-note{{instantiation}}
118*f4a2713aSLionel Sambuc X2<float> x2b; // expected-note{{instantiation}}
119*f4a2713aSLionel Sambuc 
120*f4a2713aSLionel Sambuc namespace N0 {
121*f4a2713aSLionel Sambuc   template<typename T>
122*f4a2713aSLionel Sambuc   struct X0 { };
123*f4a2713aSLionel Sambuc 
124*f4a2713aSLionel Sambuc   struct X1 {
fN0::X1125*f4a2713aSLionel Sambuc     template<typename T> void f(X0<T>& vals) { g(vals); }
gN0::X1126*f4a2713aSLionel Sambuc     template<typename T> void g(X0<T>& vals) { }
127*f4a2713aSLionel Sambuc   };
128*f4a2713aSLionel Sambuc 
test(X1 x1,X0<int> x0i,X0<long> x0l)129*f4a2713aSLionel Sambuc   void test(X1 x1, X0<int> x0i, X0<long> x0l) {
130*f4a2713aSLionel Sambuc     x1.f(x0i);
131*f4a2713aSLionel Sambuc     x1.f(x0l);
132*f4a2713aSLionel Sambuc   }
133*f4a2713aSLionel Sambuc }
134*f4a2713aSLionel Sambuc 
135*f4a2713aSLionel Sambuc namespace PR6239 {
136*f4a2713aSLionel Sambuc   template <typename T>
137*f4a2713aSLionel Sambuc   struct X0 {
138*f4a2713aSLionel Sambuc     class type {
139*f4a2713aSLionel Sambuc       typedef T E;
140*f4a2713aSLionel Sambuc       template <E e>  // subsitute T for E and bug goes away
141*f4a2713aSLionel Sambuc       struct sfinae {  };
142*f4a2713aSLionel Sambuc 
143*f4a2713aSLionel Sambuc       template <class U>
144*f4a2713aSLionel Sambuc       typename sfinae<&U::operator=>::type test(int);
145*f4a2713aSLionel Sambuc     };
146*f4a2713aSLionel Sambuc   };
147*f4a2713aSLionel Sambuc 
148*f4a2713aSLionel Sambuc   template <typename T>
149*f4a2713aSLionel Sambuc   struct X1 {
150*f4a2713aSLionel Sambuc     typedef T E;
151*f4a2713aSLionel Sambuc     template <E e>  // subsitute T for E and bug goes away
152*f4a2713aSLionel Sambuc     struct sfinae {  };
153*f4a2713aSLionel Sambuc 
154*f4a2713aSLionel Sambuc     template <class U>
155*f4a2713aSLionel Sambuc     typename sfinae<&U::operator=>::type test(int);
156*f4a2713aSLionel Sambuc   };
157*f4a2713aSLionel Sambuc 
158*f4a2713aSLionel Sambuc }
159*f4a2713aSLionel Sambuc 
160*f4a2713aSLionel Sambuc namespace PR7587 {
161*f4a2713aSLionel Sambuc   template<typename> class X0;
162*f4a2713aSLionel Sambuc   template<typename> struct X1;
163*f4a2713aSLionel Sambuc   template<typename> class X2;
164*f4a2713aSLionel Sambuc 
165*f4a2713aSLionel Sambuc   template<typename T> class X3
166*f4a2713aSLionel Sambuc   {
167*f4a2713aSLionel Sambuc     template<
168*f4a2713aSLionel Sambuc       template<typename> class TT,
169*f4a2713aSLionel Sambuc       typename U = typename X1<T>::type
170*f4a2713aSLionel Sambuc     >
171*f4a2713aSLionel Sambuc     struct Inner {
172*f4a2713aSLionel Sambuc       typedef X2<TT<typename X1<T>::type> > Type;
173*f4a2713aSLionel Sambuc     };
174*f4a2713aSLionel Sambuc 
175*f4a2713aSLionel Sambuc     const typename Inner<X0>::Type minCoeff() const;
176*f4a2713aSLionel Sambuc   };
177*f4a2713aSLionel Sambuc 
178*f4a2713aSLionel Sambuc   template<typename T> class X3<T*>
179*f4a2713aSLionel Sambuc   {
180*f4a2713aSLionel Sambuc     template<
181*f4a2713aSLionel Sambuc       template<typename> class TT,
182*f4a2713aSLionel Sambuc       typename U = typename X1<T>::type
183*f4a2713aSLionel Sambuc     >
184*f4a2713aSLionel Sambuc     struct Inner {
185*f4a2713aSLionel Sambuc       typedef X2<TT<typename X1<T>::type> > Type;
186*f4a2713aSLionel Sambuc     };
187*f4a2713aSLionel Sambuc 
188*f4a2713aSLionel Sambuc     const typename Inner<X0>::Type minCoeff() const;
189*f4a2713aSLionel Sambuc   };
190*f4a2713aSLionel Sambuc 
191*f4a2713aSLionel Sambuc }
192*f4a2713aSLionel Sambuc 
193*f4a2713aSLionel Sambuc namespace PR7669 {
194*f4a2713aSLionel Sambuc   template<class> struct X {
195*f4a2713aSLionel Sambuc     template<class> struct Y {
196*f4a2713aSLionel Sambuc       template<int,class> struct Z;
197*f4a2713aSLionel Sambuc       template<int Dummy> struct Z<Dummy,int> {};
198*f4a2713aSLionel Sambuc     };
199*f4a2713aSLionel Sambuc   };
200*f4a2713aSLionel Sambuc 
a()201*f4a2713aSLionel Sambuc   void a()
202*f4a2713aSLionel Sambuc   {
203*f4a2713aSLionel Sambuc     X<int>::Y<int>::Z<0,int>();
204*f4a2713aSLionel Sambuc   }
205*f4a2713aSLionel Sambuc }
206*f4a2713aSLionel Sambuc 
207*f4a2713aSLionel Sambuc namespace PR8489 {
208*f4a2713aSLionel Sambuc   template <typename CT>
209*f4a2713aSLionel Sambuc   class C {
210*f4a2713aSLionel Sambuc     template<typename FT>
F()211*f4a2713aSLionel Sambuc     void F() {} // expected-note{{FT}}
212*f4a2713aSLionel Sambuc   };
f()213*f4a2713aSLionel Sambuc   void f() {
214*f4a2713aSLionel Sambuc     C<int> c;
215*f4a2713aSLionel Sambuc     c.F(); // expected-error{{no matching member function}}
216*f4a2713aSLionel Sambuc   }
217*f4a2713aSLionel Sambuc }
218*f4a2713aSLionel Sambuc 
219*f4a2713aSLionel Sambuc namespace rdar8986308 {
220*f4a2713aSLionel Sambuc   template <bool> struct __static_assert_test;
221*f4a2713aSLionel Sambuc   template <> struct __static_assert_test<true> {};
222*f4a2713aSLionel Sambuc   template <unsigned> struct __static_assert_check {};
223*f4a2713aSLionel Sambuc 
224*f4a2713aSLionel Sambuc   namespace std {
225*f4a2713aSLionel Sambuc 
226*f4a2713aSLionel Sambuc     template <class _Tp, class _Up>
227*f4a2713aSLionel Sambuc     struct __has_rebind
228*f4a2713aSLionel Sambuc     {
229*f4a2713aSLionel Sambuc     private:
230*f4a2713aSLionel Sambuc       struct __two {char _; char __;};
231*f4a2713aSLionel Sambuc       template <class _Xp> static __two __test(...);
232*f4a2713aSLionel Sambuc       template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
233*f4a2713aSLionel Sambuc     public:
234*f4a2713aSLionel Sambuc       static const bool value = sizeof(__test<_Tp>(0)) == 1;
235*f4a2713aSLionel Sambuc     };
236*f4a2713aSLionel Sambuc 
237*f4a2713aSLionel Sambuc   }
238*f4a2713aSLionel Sambuc 
239*f4a2713aSLionel Sambuc   template <class T> struct B1 {};
240*f4a2713aSLionel Sambuc 
241*f4a2713aSLionel Sambuc   template <class T>
242*f4a2713aSLionel Sambuc   struct B
243*f4a2713aSLionel Sambuc   {
244*f4a2713aSLionel Sambuc     template <class U> struct rebind {typedef B1<U> other;};
245*f4a2713aSLionel Sambuc   };
246*f4a2713aSLionel Sambuc 
247*f4a2713aSLionel Sambuc   template <class T, class U> struct D1 {};
248*f4a2713aSLionel Sambuc 
249*f4a2713aSLionel Sambuc   template <class T, class U>
250*f4a2713aSLionel Sambuc   struct D
251*f4a2713aSLionel Sambuc   {
252*f4a2713aSLionel Sambuc     template <class V> struct rebind {typedef D1<V, U> other;};
253*f4a2713aSLionel Sambuc   };
254*f4a2713aSLionel Sambuc 
main()255*f4a2713aSLionel Sambuc   int main()
256*f4a2713aSLionel Sambuc   {
257*f4a2713aSLionel Sambuc     typedef __static_assert_check<sizeof(__static_assert_test<((std::__has_rebind<B<int>, double>::value))>)> __t64;
258*f4a2713aSLionel Sambuc     typedef __static_assert_check<sizeof(__static_assert_test<((std::__has_rebind<D<char, int>, double>::value))>)> __t64;
259*f4a2713aSLionel Sambuc   }
260*f4a2713aSLionel Sambuc 
261*f4a2713aSLionel Sambuc }
262