xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/temp_explicit.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat %s
2*f4a2713aSLionel Sambuc //
3*f4a2713aSLionel Sambuc // Tests explicit instantiation of templates.
4*f4a2713aSLionel Sambuc template<typename T, typename U = T> class X0 { };
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc namespace N {
7*f4a2713aSLionel Sambuc   template<typename T, typename U = T> class X1 { };
8*f4a2713aSLionel Sambuc }
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc // Check the syntax of explicit instantiations.
11*f4a2713aSLionel Sambuc template class X0<int, float>;
12*f4a2713aSLionel Sambuc template class X0<int>; // expected-note{{previous}}
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc template class N::X1<int>;
15*f4a2713aSLionel Sambuc template class ::N::X1<int, float>;
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc using namespace N;
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc // Check for some bogus syntax that probably means that the user
20*f4a2713aSLionel Sambuc // wanted to write an explicit specialization, but forgot the '<>'
21*f4a2713aSLionel Sambuc // after 'template'.
22*f4a2713aSLionel Sambuc template class X0<double> { }; // expected-error{{explicit specialization}}
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc // Check for explicit instantiations that come after other kinds of
25*f4a2713aSLionel Sambuc // instantiations or declarations.
26*f4a2713aSLionel Sambuc template class X0<int, int>; // expected-error{{duplicate}}
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc template<> class X0<char> { }; // expected-note{{previous}}
29*f4a2713aSLionel Sambuc template class X0<char>; // expected-warning{{ignored}}
30*f4a2713aSLionel Sambuc 
foo(X0<short>)31*f4a2713aSLionel Sambuc void foo(X0<short>) { }
32*f4a2713aSLionel Sambuc template class X0<short>;
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc // Check that explicit instantiations actually produce definitions. We
35*f4a2713aSLionel Sambuc // determine whether this happens by placing semantic errors in the
36*f4a2713aSLionel Sambuc // definition of the template we're instantiating.
37*f4a2713aSLionel Sambuc template<typename T> struct X2; // expected-note{{declared here}}
38*f4a2713aSLionel Sambuc 
39*f4a2713aSLionel Sambuc template struct X2<float>; // expected-error{{undefined template}}
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc template<typename T>
42*f4a2713aSLionel Sambuc struct X2 {
43*f4a2713aSLionel Sambuc   void f0(T*); // expected-error{{pointer to a reference}}
44*f4a2713aSLionel Sambuc };
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc template struct X2<int>; // okay
47*f4a2713aSLionel Sambuc template struct X2<int&>; // expected-note{{in instantiation of}}
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc // Check that explicit instantiations instantiate member classes.
50*f4a2713aSLionel Sambuc template<typename T> struct X3 {
51*f4a2713aSLionel Sambuc   struct Inner {
52*f4a2713aSLionel Sambuc     void f(T*); // expected-error{{pointer to a reference}}
53*f4a2713aSLionel Sambuc   };
54*f4a2713aSLionel Sambuc };
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc void f1(X3<int&>); // okay, Inner, not instantiated
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc template struct X3<int&>; // expected-note{{instantiation}}
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc template<typename T> struct X4 {
61*f4a2713aSLionel Sambuc   struct Inner {
62*f4a2713aSLionel Sambuc     struct VeryInner {
63*f4a2713aSLionel Sambuc       void f(T*); // expected-error 2{{pointer to a reference}}
64*f4a2713aSLionel Sambuc     };
65*f4a2713aSLionel Sambuc   };
66*f4a2713aSLionel Sambuc };
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc void f2(X4<int&>); // okay, Inner, not instantiated
69*f4a2713aSLionel Sambuc void f3(X4<int&>::Inner); // okay, Inner::VeryInner, not instantiated
70*f4a2713aSLionel Sambuc 
71*f4a2713aSLionel Sambuc template struct X4<int&>; // expected-note{{instantiation}}
72*f4a2713aSLionel Sambuc template struct X4<float&>; // expected-note{{instantiation}}
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc // Check explicit instantiation of member classes
75*f4a2713aSLionel Sambuc namespace N2 {
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc template<typename T>
78*f4a2713aSLionel Sambuc struct X5 {
79*f4a2713aSLionel Sambuc   struct Inner1 {
80*f4a2713aSLionel Sambuc     void f(T&);
81*f4a2713aSLionel Sambuc   };
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc   struct Inner2 { // expected-note {{here}}
84*f4a2713aSLionel Sambuc     struct VeryInner {
85*f4a2713aSLionel Sambuc       void g(T*); // expected-error 2{{pointer to a reference}}
86*f4a2713aSLionel Sambuc     };
87*f4a2713aSLionel Sambuc   };
88*f4a2713aSLionel Sambuc };
89*f4a2713aSLionel Sambuc 
90*f4a2713aSLionel Sambuc }
91*f4a2713aSLionel Sambuc 
92*f4a2713aSLionel Sambuc template struct N2::X5<void>::Inner2;
93*f4a2713aSLionel Sambuc 
94*f4a2713aSLionel Sambuc using namespace N2;
95*f4a2713aSLionel Sambuc template struct X5<int&>::Inner2; // expected-note{{instantiation}}
96*f4a2713aSLionel Sambuc 
97*f4a2713aSLionel Sambuc void f4(X5<float&>::Inner2);
98*f4a2713aSLionel Sambuc template struct X5<float&>::Inner2; // expected-note{{instantiation}}
99*f4a2713aSLionel Sambuc 
100*f4a2713aSLionel Sambuc namespace N3 {
101*f4a2713aSLionel Sambuc   template struct N2::X5<int>::Inner2; // expected-warning {{explicit instantiation of 'Inner2' not in a namespace enclosing 'N2'}}
102*f4a2713aSLionel Sambuc }
103*f4a2713aSLionel Sambuc 
104*f4a2713aSLionel Sambuc struct X6 {
105*f4a2713aSLionel Sambuc   struct Inner { // expected-note{{here}}
106*f4a2713aSLionel Sambuc     void f();
107*f4a2713aSLionel Sambuc   };
108*f4a2713aSLionel Sambuc };
109*f4a2713aSLionel Sambuc 
110*f4a2713aSLionel Sambuc template struct X6::Inner; // expected-error{{non-templated}}
111*f4a2713aSLionel Sambuc 
112*f4a2713aSLionel Sambuc // PR5559
113*f4a2713aSLionel Sambuc template <typename T>
114*f4a2713aSLionel Sambuc struct Foo;
115*f4a2713aSLionel Sambuc 
116*f4a2713aSLionel Sambuc template <>
117*f4a2713aSLionel Sambuc struct Foo<int> // expected-note{{header not required for explicitly-specialized}}
118*f4a2713aSLionel Sambuc {
119*f4a2713aSLionel Sambuc     template <typename U>
120*f4a2713aSLionel Sambuc     struct Bar
121*f4a2713aSLionel Sambuc     {};
122*f4a2713aSLionel Sambuc };
123*f4a2713aSLionel Sambuc 
124*f4a2713aSLionel Sambuc template <> // expected-warning{{extraneous template parameter list}}
125*f4a2713aSLionel Sambuc template <>
126*f4a2713aSLionel Sambuc struct Foo<int>::Bar<void>
127*f4a2713aSLionel Sambuc {};
128*f4a2713aSLionel Sambuc 
129*f4a2713aSLionel Sambuc namespace N1 {
130*f4a2713aSLionel Sambuc 
131*f4a2713aSLionel Sambuc   template<typename T> struct X7 { }; // expected-note{{here}}
132*f4a2713aSLionel Sambuc 
133*f4a2713aSLionel Sambuc   namespace Inner {
134*f4a2713aSLionel Sambuc     template<typename T> struct X8 { };
135*f4a2713aSLionel Sambuc   }
136*f4a2713aSLionel Sambuc 
137*f4a2713aSLionel Sambuc   template struct X7<int>;
138*f4a2713aSLionel Sambuc   template struct Inner::X8<int>;
139*f4a2713aSLionel Sambuc }
140*f4a2713aSLionel Sambuc 
141*f4a2713aSLionel Sambuc template<typename T> struct X9 { }; // expected-note{{here}}
142*f4a2713aSLionel Sambuc 
143*f4a2713aSLionel Sambuc template struct ::N1::Inner::X8<float>;
144*f4a2713aSLionel Sambuc 
145*f4a2713aSLionel Sambuc namespace N2 {
146*f4a2713aSLionel Sambuc   using namespace N1;
147*f4a2713aSLionel Sambuc 
148*f4a2713aSLionel Sambuc   template struct X7<double>; // expected-warning{{must occur in namespace}}
149*f4a2713aSLionel Sambuc 
150*f4a2713aSLionel Sambuc   template struct X9<float>; // expected-warning{{must occur at global scope}}
151*f4a2713aSLionel Sambuc }
152