xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/instantiate-method.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc template<typename T>
3*f4a2713aSLionel Sambuc class X {
4*f4a2713aSLionel Sambuc public:
5*f4a2713aSLionel Sambuc   void f(T x); // expected-error{{argument may not have 'void' type}}
6*f4a2713aSLionel Sambuc   void g(T*);
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc   static int h(T, T); // expected-error {{argument may not have 'void' type}}
9*f4a2713aSLionel Sambuc };
10*f4a2713aSLionel Sambuc 
11*f4a2713aSLionel Sambuc int identity(int x) { return x; }
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc void test(X<int> *xi, int *ip, X<int(int)> *xf) {
14*f4a2713aSLionel Sambuc   xi->f(17);
15*f4a2713aSLionel Sambuc   xi->g(ip);
16*f4a2713aSLionel Sambuc   xf->f(&identity);
17*f4a2713aSLionel Sambuc   xf->g(identity);
18*f4a2713aSLionel Sambuc   X<int>::h(17, 25);
19*f4a2713aSLionel Sambuc   X<int(int)>::h(identity, &identity);
20*f4a2713aSLionel Sambuc }
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc void test_bad() {
23*f4a2713aSLionel Sambuc   X<void> xv; // expected-note{{in instantiation of template class 'X<void>' requested here}}
24*f4a2713aSLionel Sambuc }
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc template<typename T, typename U>
27*f4a2713aSLionel Sambuc class Overloading {
28*f4a2713aSLionel Sambuc public:
29*f4a2713aSLionel Sambuc   int& f(T, T); // expected-note{{previous declaration is here}}
30*f4a2713aSLionel Sambuc   float& f(T, U); // expected-error{{functions that differ only in their return type cannot be overloaded}}
31*f4a2713aSLionel Sambuc };
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc void test_ovl(Overloading<int, long> *oil, int i, long l) {
34*f4a2713aSLionel Sambuc   int &ir = oil->f(i, i);
35*f4a2713aSLionel Sambuc   float &fr = oil->f(i, l);
36*f4a2713aSLionel Sambuc }
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc void test_ovl_bad() {
39*f4a2713aSLionel Sambuc   Overloading<float, float> off; // expected-note{{in instantiation of template class 'Overloading<float, float>' requested here}}
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc 
42*f4a2713aSLionel Sambuc template<typename T>
43*f4a2713aSLionel Sambuc class HasDestructor {
44*f4a2713aSLionel Sambuc public:
45*f4a2713aSLionel Sambuc   virtual ~HasDestructor() = 0;
46*f4a2713aSLionel Sambuc };
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc int i = sizeof(HasDestructor<int>); // FIXME: forces instantiation, but
49*f4a2713aSLionel Sambuc                 // the code below should probably instantiate by itself.
50*f4a2713aSLionel Sambuc int abstract_destructor[__is_abstract(HasDestructor<int>)? 1 : -1];
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc 
53*f4a2713aSLionel Sambuc template<typename T>
54*f4a2713aSLionel Sambuc class Constructors {
55*f4a2713aSLionel Sambuc public:
56*f4a2713aSLionel Sambuc   Constructors(const T&);
57*f4a2713aSLionel Sambuc   Constructors(const Constructors &other);
58*f4a2713aSLionel Sambuc };
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc void test_constructors() {
61*f4a2713aSLionel Sambuc   Constructors<int> ci1(17);
62*f4a2713aSLionel Sambuc   Constructors<int> ci2 = ci1;
63*f4a2713aSLionel Sambuc }
64*f4a2713aSLionel Sambuc 
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc template<typename T>
67*f4a2713aSLionel Sambuc struct ConvertsTo {
68*f4a2713aSLionel Sambuc   operator T();
69*f4a2713aSLionel Sambuc };
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc void test_converts_to(ConvertsTo<int> ci, ConvertsTo<int *> cip) {
72*f4a2713aSLionel Sambuc   int i = ci;
73*f4a2713aSLionel Sambuc   int *ip = cip;
74*f4a2713aSLionel Sambuc }
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc // PR4660
77*f4a2713aSLionel Sambuc template<class T> struct A0 { operator T*(); };
78*f4a2713aSLionel Sambuc template<class T> struct A1;
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc int *a(A0<int> &x0, A1<int> &x1) {
81*f4a2713aSLionel Sambuc   int *y0 = x0;
82*f4a2713aSLionel Sambuc   int *y1 = x1; // expected-error{{no viable conversion}}
83*f4a2713aSLionel Sambuc }
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc struct X0Base {
86*f4a2713aSLionel Sambuc   int &f();
87*f4a2713aSLionel Sambuc   int& g(int);
88*f4a2713aSLionel Sambuc   static double &g(double);
89*f4a2713aSLionel Sambuc };
90*f4a2713aSLionel Sambuc 
91*f4a2713aSLionel Sambuc template<typename T>
92*f4a2713aSLionel Sambuc struct X0 : X0Base {
93*f4a2713aSLionel Sambuc };
94*f4a2713aSLionel Sambuc 
95*f4a2713aSLionel Sambuc template<typename U>
96*f4a2713aSLionel Sambuc struct X1 : X0<U> {
97*f4a2713aSLionel Sambuc   int &f2() {
98*f4a2713aSLionel Sambuc     return X0Base::f();
99*f4a2713aSLionel Sambuc   }
100*f4a2713aSLionel Sambuc };
101*f4a2713aSLionel Sambuc 
102*f4a2713aSLionel Sambuc void test_X1(X1<int> x1i) {
103*f4a2713aSLionel Sambuc   int &ir = x1i.f2();
104*f4a2713aSLionel Sambuc }
105*f4a2713aSLionel Sambuc 
106*f4a2713aSLionel Sambuc template<typename U>
107*f4a2713aSLionel Sambuc struct X2 : X0Base, U {
108*f4a2713aSLionel Sambuc   int &f2() { return X0Base::f(); }
109*f4a2713aSLionel Sambuc };
110*f4a2713aSLionel Sambuc 
111*f4a2713aSLionel Sambuc template<typename T>
112*f4a2713aSLionel Sambuc struct X3 {
113*f4a2713aSLionel Sambuc   void test(T x) {
114*f4a2713aSLionel Sambuc     double& d1 = X0Base::g(x);
115*f4a2713aSLionel Sambuc   }
116*f4a2713aSLionel Sambuc };
117*f4a2713aSLionel Sambuc 
118*f4a2713aSLionel Sambuc 
119*f4a2713aSLionel Sambuc template struct X3<double>;
120*f4a2713aSLionel Sambuc 
121*f4a2713aSLionel Sambuc // Don't try to instantiate this, it's invalid.
122*f4a2713aSLionel Sambuc namespace test1 {
123*f4a2713aSLionel Sambuc   template <class T> class A {};
124*f4a2713aSLionel Sambuc   template <class T> class B {
125*f4a2713aSLionel Sambuc     void foo(A<test1::Undeclared> &a) // expected-error {{no member named 'Undeclared' in namespace 'test1'}}
126*f4a2713aSLionel Sambuc     {}
127*f4a2713aSLionel Sambuc   };
128*f4a2713aSLionel Sambuc   template class B<int>;
129*f4a2713aSLionel Sambuc }
130*f4a2713aSLionel Sambuc 
131*f4a2713aSLionel Sambuc namespace PR6947 {
132*f4a2713aSLionel Sambuc   template< class T >
133*f4a2713aSLionel Sambuc   struct X {
134*f4a2713aSLionel Sambuc     int f0( )
135*f4a2713aSLionel Sambuc     {
136*f4a2713aSLionel Sambuc       typedef void ( X::*impl_fun_ptr )( );
137*f4a2713aSLionel Sambuc       impl_fun_ptr pImpl = &X::template
138*f4a2713aSLionel Sambuc         f0_impl1<int>;
139*f4a2713aSLionel Sambuc     }
140*f4a2713aSLionel Sambuc   private:
141*f4a2713aSLionel Sambuc     int f1() {
142*f4a2713aSLionel Sambuc     }
143*f4a2713aSLionel Sambuc     template< class Processor>
144*f4a2713aSLionel Sambuc     void f0_impl1( )
145*f4a2713aSLionel Sambuc     {
146*f4a2713aSLionel Sambuc     }
147*f4a2713aSLionel Sambuc   };
148*f4a2713aSLionel Sambuc 
149*f4a2713aSLionel Sambuc   char g0() {
150*f4a2713aSLionel Sambuc     X<int> pc;
151*f4a2713aSLionel Sambuc     pc.f0();
152*f4a2713aSLionel Sambuc   }
153*f4a2713aSLionel Sambuc 
154*f4a2713aSLionel Sambuc }
155*f4a2713aSLionel Sambuc 
156*f4a2713aSLionel Sambuc namespace PR7022 {
157*f4a2713aSLionel Sambuc   template <typename >
158*f4a2713aSLionel Sambuc   struct X1
159*f4a2713aSLionel Sambuc   {
160*f4a2713aSLionel Sambuc     typedef int state_t( );
161*f4a2713aSLionel Sambuc     state_t g ;
162*f4a2713aSLionel Sambuc   };
163*f4a2713aSLionel Sambuc 
164*f4a2713aSLionel Sambuc   template <  typename U = X1<int> > struct X2
165*f4a2713aSLionel Sambuc   {
166*f4a2713aSLionel Sambuc     X2( U = U())
167*f4a2713aSLionel Sambuc     {
168*f4a2713aSLionel Sambuc     }
169*f4a2713aSLionel Sambuc   };
170*f4a2713aSLionel Sambuc 
171*f4a2713aSLionel Sambuc   void m(void)
172*f4a2713aSLionel Sambuc   {
173*f4a2713aSLionel Sambuc     typedef X2<> X2_type;
174*f4a2713aSLionel Sambuc     X2_type c;
175*f4a2713aSLionel Sambuc   }
176*f4a2713aSLionel Sambuc }
177*f4a2713aSLionel Sambuc 
178*f4a2713aSLionel Sambuc namespace SameSignatureAfterInstantiation {
179*f4a2713aSLionel Sambuc   template<typename T> struct S {
180*f4a2713aSLionel Sambuc     void f(T *); // expected-note {{previous}}
181*f4a2713aSLionel Sambuc     void f(const T*); // expected-error {{multiple overloads of 'f' instantiate to the same signature 'void (const int *)'}}
182*f4a2713aSLionel Sambuc   };
183*f4a2713aSLionel Sambuc   S<const int> s; // expected-note {{instantiation}}
184*f4a2713aSLionel Sambuc }
185