xref: /llvm-project/clang/test/SemaTemplate/instantiate-member-template.cpp (revision 43f788f16cec157904e752dd4cf85475622741e7)
18fbe78f6SDaniel Dunbar // RUN: %clang_cc1 -fsyntax-only -verify %s
2e704c9dfSDouglas Gregor 
3e704c9dfSDouglas Gregor template<typename T>
4e704c9dfSDouglas Gregor struct X0 {
5e704c9dfSDouglas Gregor   template<typename U> T f0(U);
6e704c9dfSDouglas Gregor   template<typename U> U& f1(T*, U); // expected-error{{pointer to a reference}} \
7e704c9dfSDouglas Gregor                                      // expected-note{{candidate}}
8e704c9dfSDouglas Gregor };
9e704c9dfSDouglas Gregor 
10e704c9dfSDouglas Gregor X0<int> x0i;
11e704c9dfSDouglas Gregor X0<void> x0v;
12e704c9dfSDouglas Gregor X0<int&> x0ir; // expected-note{{instantiation}}
13e704c9dfSDouglas Gregor 
test_X0(int * ip,double * dp)14e704c9dfSDouglas Gregor void test_X0(int *ip, double *dp) {
15e704c9dfSDouglas Gregor   X0<int> xi;
16e704c9dfSDouglas Gregor   int i1 = xi.f0(ip);
17e704c9dfSDouglas Gregor   double *&dpr = xi.f1(ip, dp);
18e704c9dfSDouglas Gregor   xi.f1(dp, dp); // expected-error{{no matching}}
19e704c9dfSDouglas Gregor 
20e704c9dfSDouglas Gregor   X0<void> xv;
21e704c9dfSDouglas Gregor   double *&dpr2 = xv.f1(ip, dp);
22e704c9dfSDouglas Gregor }
23a654dd8aSDouglas Gregor 
2401afeeffSDouglas Gregor template<typename T>
2501afeeffSDouglas Gregor struct X1 {
2601afeeffSDouglas Gregor   template<typename U>
2701afeeffSDouglas Gregor   struct Inner0 {
2801afeeffSDouglas Gregor     U x;
2901afeeffSDouglas Gregor     T y; // expected-error{{void}}
3001afeeffSDouglas Gregor   };
3101afeeffSDouglas Gregor 
3201afeeffSDouglas Gregor   template<typename U>
3301afeeffSDouglas Gregor   struct Inner1 {
3401afeeffSDouglas Gregor     U x; // expected-error{{void}}
3501afeeffSDouglas Gregor     T y;
3601afeeffSDouglas Gregor   };
3739cacdb0SDouglas Gregor 
3839cacdb0SDouglas Gregor   template<typename U>
3939cacdb0SDouglas Gregor   struct Inner2 {
4039cacdb0SDouglas Gregor     struct SuperInner {
4139cacdb0SDouglas Gregor       U z; // expected-error{{void}}
4239cacdb0SDouglas Gregor     };
4339cacdb0SDouglas Gregor   };
44d99bb430SDouglas Gregor 
45d99bb430SDouglas Gregor   template<typename U>
46d99bb430SDouglas Gregor   struct Inner3 {
f0X1::Inner3474f4946aaSDouglas Gregor     void f0(T t, U u) { // expected-note{{passing argument to parameter 't' here}}
48d99bb430SDouglas Gregor       (void)(t + u); // expected-error{{invalid operands}}
49d99bb430SDouglas Gregor     }
50d99bb430SDouglas Gregor 
51d99bb430SDouglas Gregor     template<typename V>
f1X1::Inner352d99bb430SDouglas Gregor     V f1(T t, U u, V) {
53e1314a64SDouglas Gregor       return t + u; // expected-error{{cannot initialize return object}}
54d99bb430SDouglas Gregor     }
5501afeeffSDouglas Gregor   };
5601afeeffSDouglas Gregor 
575c580931SDouglas Gregor   template<typename U>
585c580931SDouglas Gregor   struct Inner4;
59d99bb430SDouglas Gregor };
60d99bb430SDouglas Gregor 
615c580931SDouglas Gregor template<typename T>
625c580931SDouglas Gregor template<typename U>
635c580931SDouglas Gregor struct X1<T>::Inner4 {
645c580931SDouglas Gregor   template<typename V>
655c580931SDouglas Gregor   V f2(T t, U u, V);
6611395b66SDouglas Gregor 
6711395b66SDouglas Gregor   static U value;
685c580931SDouglas Gregor };
695c580931SDouglas Gregor 
705c580931SDouglas Gregor template<typename T>
715c580931SDouglas Gregor template<typename U>
7211395b66SDouglas Gregor U X1<T>::Inner4<U>::value; // expected-error{{reference variable}}
7311395b66SDouglas Gregor 
7411395b66SDouglas Gregor template<typename T>
7511395b66SDouglas Gregor template<typename U>
765c580931SDouglas Gregor template<typename V>
f2(T t,U u,V)775c580931SDouglas Gregor V X1<T>::Inner4<U>::f2(T t, U u, V) {
78e1314a64SDouglas Gregor   return t + u; // expected-error{{cannot initialize return object}}
795c580931SDouglas Gregor }
805c580931SDouglas Gregor 
test_X1(int * ip,int i,double * dp)81d99bb430SDouglas Gregor void test_X1(int *ip, int i, double *dp) {
8201afeeffSDouglas Gregor   X1<void>::Inner0<int> *xvip; // okay
8301afeeffSDouglas Gregor   X1<void>::Inner0<int> xvi; // expected-note{{instantiation}}
8401afeeffSDouglas Gregor 
8501afeeffSDouglas Gregor   X1<int>::Inner1<void> *xivp; // okay
8601afeeffSDouglas Gregor   X1<int>::Inner1<void> xiv; // expected-note{{instantiation}}
8739cacdb0SDouglas Gregor 
8839cacdb0SDouglas Gregor   X1<int>::Inner2<void>::SuperInner *xisivp; // okay
8939cacdb0SDouglas Gregor   X1<int>::Inner2<void>::SuperInner xisiv; // expected-note{{instantiation}}
90d99bb430SDouglas Gregor 
91d99bb430SDouglas Gregor   X1<int*>::Inner3<int> id3;
92d99bb430SDouglas Gregor   id3.f0(ip, i);
9396596c98SDouglas Gregor   id3.f0(dp, i); // expected-error{{cannot initialize a parameter of type 'int *' with an lvalue of type 'double *'}}
94d99bb430SDouglas Gregor   id3.f1(ip, i, ip);
95d99bb430SDouglas Gregor   id3.f1(ip, i, dp); // expected-note{{instantiation}}
96d99bb430SDouglas Gregor 
97d99bb430SDouglas Gregor   X1<int*>::Inner3<double*> id3b;
98d99bb430SDouglas Gregor   id3b.f0(ip, dp); // expected-note{{instantiation}}
995c580931SDouglas Gregor 
1005c580931SDouglas Gregor   X1<int*>::Inner4<int> id4;
1015c580931SDouglas Gregor   id4.f2(ip, i, dp); // expected-note{{instantiation}}
10211395b66SDouglas Gregor 
10311395b66SDouglas Gregor   X1<int*>::Inner4<int>::value = 17;
10411395b66SDouglas Gregor   i = X1<int*>::Inner4<int&>::value; // expected-note{{instantiation}}
10501afeeffSDouglas Gregor }
106a8ab71baSDouglas Gregor 
107a8ab71baSDouglas Gregor 
108a8ab71baSDouglas Gregor template<typename T>
109a8ab71baSDouglas Gregor struct X2 {
110a8ab71baSDouglas Gregor   template<T *Ptr> // expected-error{{pointer to a reference}}
111a8ab71baSDouglas Gregor   struct Inner;
112a8ab71baSDouglas Gregor 
113a8ab71baSDouglas Gregor   template<T Value> // expected-error{{cannot have type 'float'}}
114a8ab71baSDouglas Gregor   struct Inner2;
115a8ab71baSDouglas Gregor };
116a8ab71baSDouglas Gregor 
117a8ab71baSDouglas Gregor X2<int&> x2a; // expected-note{{instantiation}}
118a8ab71baSDouglas Gregor X2<float> x2b; // expected-note{{instantiation}}
11951c538beSDouglas Gregor 
12051c538beSDouglas Gregor namespace N0 {
12151c538beSDouglas Gregor   template<typename T>
12251c538beSDouglas Gregor   struct X0 { };
12351c538beSDouglas Gregor 
12451c538beSDouglas Gregor   struct X1 {
fN0::X112551c538beSDouglas Gregor     template<typename T> void f(X0<T>& vals) { g(vals); }
gN0::X112651c538beSDouglas Gregor     template<typename T> void g(X0<T>& vals) { }
12751c538beSDouglas Gregor   };
12851c538beSDouglas Gregor 
test(X1 x1,X0<int> x0i,X0<long> x0l)12951c538beSDouglas Gregor   void test(X1 x1, X0<int> x0i, X0<long> x0l) {
13051c538beSDouglas Gregor     x1.f(x0i);
13151c538beSDouglas Gregor     x1.f(x0l);
13251c538beSDouglas Gregor   }
13351c538beSDouglas Gregor }
134d225fa05SDouglas Gregor 
135d225fa05SDouglas Gregor namespace PR6239 {
136d225fa05SDouglas Gregor   template <typename T>
137d225fa05SDouglas Gregor   struct X0 {
138d225fa05SDouglas Gregor     class type {
139d225fa05SDouglas Gregor       typedef T E;
140d225fa05SDouglas Gregor       template <E e>  // subsitute T for E and bug goes away
141d225fa05SDouglas Gregor       struct sfinae {  };
142d225fa05SDouglas Gregor 
143d225fa05SDouglas Gregor       template <class U>
144d225fa05SDouglas Gregor       typename sfinae<&U::operator=>::type test(int);
145d225fa05SDouglas Gregor     };
146d225fa05SDouglas Gregor   };
147d225fa05SDouglas Gregor 
148d225fa05SDouglas Gregor   template <typename T>
149d225fa05SDouglas Gregor   struct X1 {
150d225fa05SDouglas Gregor     typedef T E;
151d225fa05SDouglas Gregor     template <E e>  // subsitute T for E and bug goes away
152d225fa05SDouglas Gregor     struct sfinae {  };
153d225fa05SDouglas Gregor 
154d225fa05SDouglas Gregor     template <class U>
155d225fa05SDouglas Gregor     typename sfinae<&U::operator=>::type test(int);
156d225fa05SDouglas Gregor   };
157d225fa05SDouglas Gregor 
158d225fa05SDouglas Gregor }
1599961ce94SDouglas Gregor 
1609961ce94SDouglas Gregor namespace PR7587 {
1619961ce94SDouglas Gregor   template<typename> class X0;
1629961ce94SDouglas Gregor   template<typename> struct X1;
1639961ce94SDouglas Gregor   template<typename> class X2;
1649961ce94SDouglas Gregor 
1659961ce94SDouglas Gregor   template<typename T> class X3
1669961ce94SDouglas Gregor   {
1679961ce94SDouglas Gregor     template<
1689961ce94SDouglas Gregor       template<typename> class TT,
1699961ce94SDouglas Gregor       typename U = typename X1<T>::type
1709961ce94SDouglas Gregor     >
1719961ce94SDouglas Gregor     struct Inner {
1729961ce94SDouglas Gregor       typedef X2<TT<typename X1<T>::type> > Type;
1739961ce94SDouglas Gregor     };
1749961ce94SDouglas Gregor 
1759961ce94SDouglas Gregor     const typename Inner<X0>::Type minCoeff() const;
1769961ce94SDouglas Gregor   };
1779961ce94SDouglas Gregor 
1789961ce94SDouglas Gregor   template<typename T> class X3<T*>
1799961ce94SDouglas Gregor   {
1809961ce94SDouglas Gregor     template<
1819961ce94SDouglas Gregor       template<typename> class TT,
1829961ce94SDouglas Gregor       typename U = typename X1<T>::type
1839961ce94SDouglas Gregor     >
1849961ce94SDouglas Gregor     struct Inner {
1859961ce94SDouglas Gregor       typedef X2<TT<typename X1<T>::type> > Type;
1869961ce94SDouglas Gregor     };
1879961ce94SDouglas Gregor 
1889961ce94SDouglas Gregor     const typename Inner<X0>::Type minCoeff() const;
1899961ce94SDouglas Gregor   };
1909961ce94SDouglas Gregor 
1919961ce94SDouglas Gregor }
192a477e2afSDouglas Gregor 
193a477e2afSDouglas Gregor namespace PR7669 {
194a477e2afSDouglas Gregor   template<class> struct X {
195a477e2afSDouglas Gregor     template<class> struct Y {
196a477e2afSDouglas Gregor       template<int,class> struct Z;
197a477e2afSDouglas Gregor       template<int Dummy> struct Z<Dummy,int> {};
198a477e2afSDouglas Gregor     };
199a477e2afSDouglas Gregor   };
200a477e2afSDouglas Gregor 
a()201a477e2afSDouglas Gregor   void a()
202a477e2afSDouglas Gregor   {
203a477e2afSDouglas Gregor     X<int>::Y<int>::Z<0,int>();
204a477e2afSDouglas Gregor   }
205a477e2afSDouglas Gregor }
206b9b39273SNick Lewycky 
207b9b39273SNick Lewycky namespace PR8489 {
208b9b39273SNick Lewycky   template <typename CT>
209b9b39273SNick Lewycky   class C {
210b9b39273SNick Lewycky     template<typename FT>
F()211b9b39273SNick Lewycky     void F() {} // expected-note{{FT}}
212b9b39273SNick Lewycky   };
f()213b9b39273SNick Lewycky   void f() {
214b9b39273SNick Lewycky     C<int> c;
215b9b39273SNick Lewycky     c.F(); // expected-error{{no matching member function}}
216b9b39273SNick Lewycky   }
217b9b39273SNick Lewycky }
218*43f788f1SDouglas Gregor 
219*43f788f1SDouglas Gregor namespace rdar8986308 {
220*43f788f1SDouglas Gregor   template <bool> struct __static_assert_test;
221*43f788f1SDouglas Gregor   template <> struct __static_assert_test<true> {};
222*43f788f1SDouglas Gregor   template <unsigned> struct __static_assert_check {};
223*43f788f1SDouglas Gregor 
224*43f788f1SDouglas Gregor   namespace std {
225*43f788f1SDouglas Gregor 
226*43f788f1SDouglas Gregor     template <class _Tp, class _Up>
227*43f788f1SDouglas Gregor     struct __has_rebind
228*43f788f1SDouglas Gregor     {
229*43f788f1SDouglas Gregor     private:
230*43f788f1SDouglas Gregor       struct __two {char _; char __;};
231*43f788f1SDouglas Gregor       template <class _Xp> static __two __test(...);
232*43f788f1SDouglas Gregor       template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
233*43f788f1SDouglas Gregor     public:
234*43f788f1SDouglas Gregor       static const bool value = sizeof(__test<_Tp>(0)) == 1;
235*43f788f1SDouglas Gregor     };
236*43f788f1SDouglas Gregor 
237*43f788f1SDouglas Gregor   }
238*43f788f1SDouglas Gregor 
239*43f788f1SDouglas Gregor   template <class T> struct B1 {};
240*43f788f1SDouglas Gregor 
241*43f788f1SDouglas Gregor   template <class T>
242*43f788f1SDouglas Gregor   struct B
243*43f788f1SDouglas Gregor   {
244*43f788f1SDouglas Gregor     template <class U> struct rebind {typedef B1<U> other;};
245*43f788f1SDouglas Gregor   };
246*43f788f1SDouglas Gregor 
247*43f788f1SDouglas Gregor   template <class T, class U> struct D1 {};
248*43f788f1SDouglas Gregor 
249*43f788f1SDouglas Gregor   template <class T, class U>
250*43f788f1SDouglas Gregor   struct D
251*43f788f1SDouglas Gregor   {
252*43f788f1SDouglas Gregor     template <class V> struct rebind {typedef D1<V, U> other;};
253*43f788f1SDouglas Gregor   };
254*43f788f1SDouglas Gregor 
main()255*43f788f1SDouglas Gregor   int main()
256*43f788f1SDouglas Gregor   {
257*43f788f1SDouglas Gregor     typedef __static_assert_check<sizeof(__static_assert_test<((std::__has_rebind<B<int>, double>::value))>)> __t64;
258*43f788f1SDouglas Gregor     typedef __static_assert_check<sizeof(__static_assert_test<((std::__has_rebind<D<char, int>, double>::value))>)> __t64;
259*43f788f1SDouglas Gregor   }
260*43f788f1SDouglas Gregor 
261*43f788f1SDouglas Gregor }
262