1cc5f8f0dSDavid Blaikie // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat %s
2*9ea0817cSCharles Li // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat -std=c++98 %s
3*9ea0817cSCharles Li // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=c++11 %s
41b57ff32SDouglas Gregor //
51b57ff32SDouglas Gregor // Tests explicit instantiation of templates.
61b57ff32SDouglas Gregor template<typename T, typename U = T> class X0 { };
71b57ff32SDouglas Gregor 
81b57ff32SDouglas Gregor namespace N {
91b57ff32SDouglas Gregor   template<typename T, typename U = T> class X1 { };
101b57ff32SDouglas Gregor }
111b57ff32SDouglas Gregor 
12a1f49973SDouglas Gregor // Check the syntax of explicit instantiations.
131b57ff32SDouglas Gregor template class X0<int, float>;
14a1f49973SDouglas Gregor template class X0<int>; // expected-note{{previous}}
151b57ff32SDouglas Gregor 
161b57ff32SDouglas Gregor template class N::X1<int>;
171b57ff32SDouglas Gregor template class ::N::X1<int, float>;
181b57ff32SDouglas Gregor 
191b57ff32SDouglas Gregor using namespace N;
201b57ff32SDouglas Gregor 
21a1f49973SDouglas Gregor // Check for some bogus syntax that probably means that the user
22a1f49973SDouglas Gregor // wanted to write an explicit specialization, but forgot the '<>'
23a1f49973SDouglas Gregor // after 'template'.
241b57ff32SDouglas Gregor template class X0<double> { }; // expected-error{{explicit specialization}}
25a1f49973SDouglas Gregor 
26a1f49973SDouglas Gregor // Check for explicit instantiations that come after other kinds of
27a1f49973SDouglas Gregor // instantiations or declarations.
28f61eca93SDouglas Gregor template class X0<int, int>; // expected-error{{duplicate}}
29a1f49973SDouglas Gregor 
30a1f49973SDouglas Gregor template<> class X0<char> { }; // expected-note{{previous}}
31e4caa48dSRichard Smith template class X0<char>; // expected-warning{{has no effect}}
32a1f49973SDouglas Gregor 
foo(X0<short>)33f61eca93SDouglas Gregor void foo(X0<short>) { }
34f61eca93SDouglas Gregor template class X0<short>;
35a1f49973SDouglas Gregor 
36a1f49973SDouglas Gregor // Check that explicit instantiations actually produce definitions. We
37a1f49973SDouglas Gregor // determine whether this happens by placing semantic errors in the
38a1f49973SDouglas Gregor // definition of the template we're instantiating.
39a1f49973SDouglas Gregor template<typename T> struct X2; // expected-note{{declared here}}
40a1f49973SDouglas Gregor 
41a1f49973SDouglas Gregor template struct X2<float>; // expected-error{{undefined template}}
42a1f49973SDouglas Gregor 
43a1f49973SDouglas Gregor template<typename T>
44a1f49973SDouglas Gregor struct X2 {
45a1f49973SDouglas Gregor   void f0(T*); // expected-error{{pointer to a reference}}
46a1f49973SDouglas Gregor };
47a1f49973SDouglas Gregor 
48a1f49973SDouglas Gregor template struct X2<int>; // okay
49a1f49973SDouglas Gregor template struct X2<int&>; // expected-note{{in instantiation of}}
50bbbb02d4SDouglas Gregor 
51bbbb02d4SDouglas Gregor // Check that explicit instantiations instantiate member classes.
52bbbb02d4SDouglas Gregor template<typename T> struct X3 {
538567358cSDouglas Gregor   struct Inner {
54bbbb02d4SDouglas Gregor     void f(T*); // expected-error{{pointer to a reference}}
55bbbb02d4SDouglas Gregor   };
56bbbb02d4SDouglas Gregor };
57bbbb02d4SDouglas Gregor 
58bbbb02d4SDouglas Gregor void f1(X3<int&>); // okay, Inner, not instantiated
59bbbb02d4SDouglas Gregor 
60bbbb02d4SDouglas Gregor template struct X3<int&>; // expected-note{{instantiation}}
61bbbb02d4SDouglas Gregor 
62bbbb02d4SDouglas Gregor template<typename T> struct X4 {
638567358cSDouglas Gregor   struct Inner {
648567358cSDouglas Gregor     struct VeryInner {
65bbbb02d4SDouglas Gregor       void f(T*); // expected-error 2{{pointer to a reference}}
66bbbb02d4SDouglas Gregor     };
67bbbb02d4SDouglas Gregor   };
68bbbb02d4SDouglas Gregor };
69bbbb02d4SDouglas Gregor 
70bbbb02d4SDouglas Gregor void f2(X4<int&>); // okay, Inner, not instantiated
71bbbb02d4SDouglas Gregor void f3(X4<int&>::Inner); // okay, Inner::VeryInner, not instantiated
72bbbb02d4SDouglas Gregor 
73bbbb02d4SDouglas Gregor template struct X4<int&>; // expected-note{{instantiation}}
74bbbb02d4SDouglas Gregor template struct X4<float&>; // expected-note{{instantiation}}
752ec748cdSDouglas Gregor 
762ec748cdSDouglas Gregor // Check explicit instantiation of member classes
772ec748cdSDouglas Gregor namespace N2 {
782ec748cdSDouglas Gregor 
792ec748cdSDouglas Gregor template<typename T>
802ec748cdSDouglas Gregor struct X5 {
812ec748cdSDouglas Gregor   struct Inner1 {
822ec748cdSDouglas Gregor     void f(T&);
832ec748cdSDouglas Gregor   };
842ec748cdSDouglas Gregor 
85050d261eSRichard Smith   struct Inner2 { // expected-note {{here}}
868567358cSDouglas Gregor     struct VeryInner {
872ec748cdSDouglas Gregor       void g(T*); // expected-error 2{{pointer to a reference}}
882ec748cdSDouglas Gregor     };
892ec748cdSDouglas Gregor   };
902ec748cdSDouglas Gregor };
912ec748cdSDouglas Gregor 
922ec748cdSDouglas Gregor }
932ec748cdSDouglas Gregor 
942ec748cdSDouglas Gregor template struct N2::X5<void>::Inner2;
952ec748cdSDouglas Gregor 
962ec748cdSDouglas Gregor using namespace N2;
972ec748cdSDouglas Gregor template struct X5<int&>::Inner2; // expected-note{{instantiation}}
982ec748cdSDouglas Gregor 
992ec748cdSDouglas Gregor void f4(X5<float&>::Inner2);
1002ec748cdSDouglas Gregor template struct X5<float&>::Inner2; // expected-note{{instantiation}}
1012ec748cdSDouglas Gregor 
1022ec748cdSDouglas Gregor namespace N3 {
103*9ea0817cSCharles Li   template struct N2::X5<int>::Inner2;
104*9ea0817cSCharles Li #if __cplusplus <= 199711L
105*9ea0817cSCharles Li // expected-warning@-2 {{explicit instantiation of 'Inner2' not in a namespace enclosing 'N2'}}
106*9ea0817cSCharles Li #else
107*9ea0817cSCharles Li // expected-error@-4 {{explicit instantiation of 'Inner2' not in a namespace enclosing 'N2'}}
108*9ea0817cSCharles Li #endif
1092ec748cdSDouglas Gregor }
1102ec748cdSDouglas Gregor 
1112ec748cdSDouglas Gregor struct X6 {
1122ec748cdSDouglas Gregor   struct Inner { // expected-note{{here}}
1132ec748cdSDouglas Gregor     void f();
1142ec748cdSDouglas Gregor   };
1152ec748cdSDouglas Gregor };
1162ec748cdSDouglas Gregor 
1172ec748cdSDouglas Gregor template struct X6::Inner; // expected-error{{non-templated}}
11865911498SDouglas Gregor 
11965911498SDouglas Gregor // PR5559
12065911498SDouglas Gregor template <typename T>
12165911498SDouglas Gregor struct Foo;
12265911498SDouglas Gregor 
12365911498SDouglas Gregor template <>
12465911498SDouglas Gregor struct Foo<int> // expected-note{{header not required for explicitly-specialized}}
12565911498SDouglas Gregor {
12665911498SDouglas Gregor     template <typename U>
12765911498SDouglas Gregor     struct Bar
12865911498SDouglas Gregor     {};
12965911498SDouglas Gregor };
13065911498SDouglas Gregor 
13165911498SDouglas Gregor template <> // expected-warning{{extraneous template parameter list}}
13265911498SDouglas Gregor template <>
13365911498SDouglas Gregor struct Foo<int>::Bar<void>
13465911498SDouglas Gregor {};
135c97d7a2cSDouglas Gregor 
136c97d7a2cSDouglas Gregor namespace N1 {
137c97d7a2cSDouglas Gregor 
138c97d7a2cSDouglas Gregor   template<typename T> struct X7 { }; // expected-note{{here}}
139c97d7a2cSDouglas Gregor 
140c97d7a2cSDouglas Gregor   namespace Inner {
141c97d7a2cSDouglas Gregor     template<typename T> struct X8 { };
142c97d7a2cSDouglas Gregor   }
143c97d7a2cSDouglas Gregor 
144c97d7a2cSDouglas Gregor   template struct X7<int>;
145c97d7a2cSDouglas Gregor   template struct Inner::X8<int>;
146c97d7a2cSDouglas Gregor }
147c97d7a2cSDouglas Gregor 
148c97d7a2cSDouglas Gregor template<typename T> struct X9 { }; // expected-note{{here}}
149c97d7a2cSDouglas Gregor 
150c97d7a2cSDouglas Gregor template struct ::N1::Inner::X8<float>;
151c97d7a2cSDouglas Gregor 
152c97d7a2cSDouglas Gregor namespace N2 {
153c97d7a2cSDouglas Gregor   using namespace N1;
154c97d7a2cSDouglas Gregor 
155*9ea0817cSCharles Li   template struct X7<double>;
156*9ea0817cSCharles Li #if __cplusplus <= 199711L
157*9ea0817cSCharles Li // expected-warning@-2 {{explicit instantiation of 'N1::X7' must occur in namespace 'N1'}}
158*9ea0817cSCharles Li #else
159*9ea0817cSCharles Li // expected-error@-4 {{explicit instantiation of 'N1::X7' must occur in namespace 'N1'}}
160*9ea0817cSCharles Li #endif
161c97d7a2cSDouglas Gregor 
162*9ea0817cSCharles Li   template struct X9<float>;
163*9ea0817cSCharles Li #if __cplusplus <= 199711L
164*9ea0817cSCharles Li // expected-warning@-2 {{explicit instantiation of 'X9' must occur at global scope}}
165*9ea0817cSCharles Li #else
166*9ea0817cSCharles Li // expected-error@-4 {{explicit instantiation of 'X9' must occur at global scope}}
167*9ea0817cSCharles Li #endif
168c97d7a2cSDouglas Gregor }
169