xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/explicit-instantiation.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc template void *; // expected-error{{expected unqualified-id}}
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc template typedef void f0; // expected-error{{explicit instantiation of typedef}}
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc int v0; // expected-note{{refers here}}
8*f4a2713aSLionel Sambuc template int v0; // expected-error{{does not refer}}
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc template<typename T>
11*f4a2713aSLionel Sambuc struct X0 {
12*f4a2713aSLionel Sambuc   static T value;
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc   T f0(T x) {
15*f4a2713aSLionel Sambuc     return x + 1;  // expected-error{{invalid operands}}
16*f4a2713aSLionel Sambuc   }
17*f4a2713aSLionel Sambuc   T* f0(T*, T*) { return T(); } // expected-warning{{expression which evaluates to zero treated as a null pointer constant of type 'int *'}}
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc   template <typename U> T f0(T, U) { return T(); } // expected-note {{candidate template ignored: could not match 'int (int, U)' against 'int (int) const'}} \
20*f4a2713aSLionel Sambuc                                                    // expected-note {{candidate template ignored: could not match 'int' against 'int *'}}
21*f4a2713aSLionel Sambuc };
22*f4a2713aSLionel Sambuc 
23*f4a2713aSLionel Sambuc template<typename T>
24*f4a2713aSLionel Sambuc T X0<T>::value; // expected-error{{no matching constructor}}
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc template int X0<int>::value;
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc struct NotDefaultConstructible { // expected-note{{candidate constructor (the implicit copy constructor)}}
29*f4a2713aSLionel Sambuc   NotDefaultConstructible(int); // expected-note{{candidate constructor}}
30*f4a2713aSLionel Sambuc };
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc template NotDefaultConstructible X0<NotDefaultConstructible>::value; // expected-note{{instantiation}}
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc template int X0<int>::f0(int);
35*f4a2713aSLionel Sambuc template int* X0<int>::f0(int*, int*); // expected-note{{in instantiation of member function 'X0<int>::f0' requested here}}
36*f4a2713aSLionel Sambuc template int X0<int>::f0(int, float);
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc template int X0<int>::f0(int) const; // expected-error{{does not refer}}
39*f4a2713aSLionel Sambuc template int* X0<int>::f0(int*, float*); // expected-error{{does not refer}}
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc struct X1 { };
42*f4a2713aSLionel Sambuc typedef int X1::*MemPtr;
43*f4a2713aSLionel Sambuc 
44*f4a2713aSLionel Sambuc template MemPtr X0<MemPtr>::f0(MemPtr); // expected-note{{requested here}}
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc struct X2 {
47*f4a2713aSLionel Sambuc   int f0(int); // expected-note{{refers here}}
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc   template<typename T> T f1(T) { return T(); }
50*f4a2713aSLionel Sambuc   template<typename T> T* f1(T*) { return 0; }
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc   template<typename T, typename U> void f2(T, U*) { } // expected-note{{candidate}}
53*f4a2713aSLionel Sambuc   template<typename T, typename U> void f2(T*, U) { } // expected-note{{candidate}}
54*f4a2713aSLionel Sambuc };
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc template int X2::f0(int); // expected-error{{not an instantiation}}
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc template int *X2::f1(int *); // okay
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc template void X2::f2(int *, int *); // expected-error{{ambiguous}}
61*f4a2713aSLionel Sambuc 
62*f4a2713aSLionel Sambuc template <typename T>
63*f4a2713aSLionel Sambuc void print_type() {} // expected-note {{candidate template ignored: could not match 'void ()' against 'void (float *)'}}
64*f4a2713aSLionel Sambuc 
65*f4a2713aSLionel Sambuc template void print_type<int>();
66*f4a2713aSLionel Sambuc template void print_type<float>();
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc template <typename T>
69*f4a2713aSLionel Sambuc void print_type(T *) {} // expected-note {{candidate template ignored: could not match 'void (int *)' against 'void (float *)'}}
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc template void print_type(int*);
72*f4a2713aSLionel Sambuc template void print_type<int>(float*); // expected-error{{does not refer}}
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc void print_type(double*);
75*f4a2713aSLionel Sambuc template void print_type<double>(double*);
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc // PR5069
78*f4a2713aSLionel Sambuc template<int I> void foo0 (int (&)[I + 1]) { }
79*f4a2713aSLionel Sambuc template void foo0<2> (int (&)[3]);
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc namespace explicit_instantiation_after_implicit_instantiation {
82*f4a2713aSLionel Sambuc   template <int I> struct X0 { static int x; };
83*f4a2713aSLionel Sambuc   template <int I> int X0<I>::x;
84*f4a2713aSLionel Sambuc   void test1() { (void)&X0<1>::x; }
85*f4a2713aSLionel Sambuc   template struct X0<1>;
86*f4a2713aSLionel Sambuc }
87*f4a2713aSLionel Sambuc 
88*f4a2713aSLionel Sambuc template<typename> struct X3 { };
89*f4a2713aSLionel Sambuc inline template struct X3<int>; // expected-warning{{ignoring 'inline' keyword on explicit template instantiation}}
90*f4a2713aSLionel Sambuc static template struct X3<float>; // expected-warning{{ignoring 'static' keyword on explicit template instantiation}}
91*f4a2713aSLionel Sambuc 
92*f4a2713aSLionel Sambuc namespace PR7622 {
93*f4a2713aSLionel Sambuc   template<typename,typename=int>
94*f4a2713aSLionel Sambuc   struct basic_streambuf;
95*f4a2713aSLionel Sambuc 
96*f4a2713aSLionel Sambuc   template<typename,typename>
97*f4a2713aSLionel Sambuc   struct basic_streambuf{friend bob<>()}; // expected-error{{unknown type name 'bob'}} \
98*f4a2713aSLionel Sambuc                                           // expected-error{{expected member name or ';' after declaration specifiers}}
99*f4a2713aSLionel Sambuc   template struct basic_streambuf<int>;
100*f4a2713aSLionel Sambuc }
101*f4a2713aSLionel Sambuc 
102*f4a2713aSLionel Sambuc // Test that we do not crash.
103*f4a2713aSLionel Sambuc class TC1 {
104*f4a2713aSLionel Sambuc   class TC2 {
105*f4a2713aSLionel Sambuc     template // FIXME: error here.
106*f4a2713aSLionel Sambuc     void foo() { }
107*f4a2713aSLionel Sambuc    };
108*f4a2713aSLionel Sambuc };
109*f4a2713aSLionel Sambuc 
110*f4a2713aSLionel Sambuc namespace PR8020 {
111*f4a2713aSLionel Sambuc   template <typename T> struct X { X() {} };
112*f4a2713aSLionel Sambuc   template<> struct X<int> { X(); };
113*f4a2713aSLionel Sambuc   template X<int>::X() {}  // expected-error{{function cannot be defined in an explicit instantiation}}
114*f4a2713aSLionel Sambuc }
115*f4a2713aSLionel Sambuc 
116*f4a2713aSLionel Sambuc namespace PR10086 {
117*f4a2713aSLionel Sambuc   template void foobar(int i) {}  // expected-error{{function cannot be defined in an explicit instantiation}}
118*f4a2713aSLionel Sambuc   int func() {
119*f4a2713aSLionel Sambuc     foobar(5);
120*f4a2713aSLionel Sambuc   }
121*f4a2713aSLionel Sambuc }
122*f4a2713aSLionel Sambuc 
123*f4a2713aSLionel Sambuc namespace undefined_static_data_member {
124*f4a2713aSLionel Sambuc   template<typename T> struct A {
125*f4a2713aSLionel Sambuc     static int a; // expected-note {{here}}
126*f4a2713aSLionel Sambuc     template<typename U> static int b; // expected-note {{here}} expected-warning {{extension}}
127*f4a2713aSLionel Sambuc   };
128*f4a2713aSLionel Sambuc   struct B {
129*f4a2713aSLionel Sambuc     template<typename U> static int c; // expected-note {{here}} expected-warning {{extension}}
130*f4a2713aSLionel Sambuc   };
131*f4a2713aSLionel Sambuc 
132*f4a2713aSLionel Sambuc   template int A<int>::a; // expected-error {{explicit instantiation of undefined static data member 'a' of class template 'undefined_static_data_member::A<int>'}}
133*f4a2713aSLionel Sambuc   template int A<int>::b<int>; // expected-error {{explicit instantiation of undefined variable template 'undefined_static_data_member::A<int>::b<int>'}}
134*f4a2713aSLionel Sambuc   template int B::c<int>; // expected-error {{explicit instantiation of undefined variable template 'undefined_static_data_member::B::c<int>'}}
135*f4a2713aSLionel Sambuc 
136*f4a2713aSLionel Sambuc 
137*f4a2713aSLionel Sambuc   template<typename T> struct C {
138*f4a2713aSLionel Sambuc     static int a;
139*f4a2713aSLionel Sambuc     template<typename U> static int b; // expected-warning {{extension}}
140*f4a2713aSLionel Sambuc   };
141*f4a2713aSLionel Sambuc   struct D {
142*f4a2713aSLionel Sambuc     template<typename U> static int c; // expected-warning {{extension}}
143*f4a2713aSLionel Sambuc   };
144*f4a2713aSLionel Sambuc   template<typename T> int C<T>::a;
145*f4a2713aSLionel Sambuc   template<typename T> template<typename U> int C<T>::b; // expected-warning {{extension}}
146*f4a2713aSLionel Sambuc   template<typename U> int D::c; // expected-warning {{extension}}
147*f4a2713aSLionel Sambuc 
148*f4a2713aSLionel Sambuc   template int C<int>::a;
149*f4a2713aSLionel Sambuc   template int C<int>::b<int>;
150*f4a2713aSLionel Sambuc   template int D::c<int>;
151*f4a2713aSLionel Sambuc }
152