1 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=c++11 %s
5 // Tests explicit instantiation of templates.
6 template<typename T, typename U = T> class X0 { };
9 template<typename T, typename U = T> class X1 { };
12 // Check the syntax of explicit instantiations.
13 template class X0<int, float>;
14 template class X0<int>; // expected-note{{previous}}
16 template class N::X1<int>;
17 template class ::N::X1<int, float>;
21 // Check for some bogus syntax that probably means that the user
22 // wanted to write an explicit specialization, but forgot the '<>'
24 template class X0<double> { }; // expected-error{{explicit specialization}}
26 // Check for explicit instantiations that come after other kinds of
27 // instantiations or declarations.
28 template class X0<int, int>; // expected-error{{duplicate}}
30 template<> class X0<char> { }; // expected-note{{previous}}
31 template class X0<char>; // expected-warning{{has no effect}}
33 void foo(X0<short>) { }
34 template class X0<short>;
36 // Check that explicit instantiations actually produce definitions. We
37 // determine whether this happens by placing semantic errors in the
38 // definition of the template we're instantiating.
39 template<typename T> struct X2; // expected-note{{declared here}}
41 template struct X2<float>; // expected-error{{undefined template}}
45 void f0(T*); // expected-error{{pointer to a reference}}
48 template struct X2<int>; // okay
49 template struct X2<int&>; // expected-note{{in instantiation of}}
51 // Check that explicit instantiations instantiate member classes.
52 template<typename T> struct X3 {
54 void f(T*); // expected-error{{pointer to a reference}}
58 void f1(X3<int&>); // okay, Inner, not instantiated
60 template struct X3<int&>; // expected-note{{instantiation}}
62 template<typename T> struct X4 {
65 void f(T*); // expected-error 2{{pointer to a reference}}
70 void f2(X4<int&>); // okay, Inner, not instantiated
71 void f3(X4<int&>::Inner); // okay, Inner::VeryInner, not instantiated
73 template struct X4<int&>; // expected-note{{instantiation}}
74 template struct X4<float&>; // expected-note{{instantiation}}
76 // Check explicit instantiation of member classes
85 struct Inner2 { // expected-note {{here}}
87 void g(T*); // expected-error 2{{pointer to a reference}}
94 template struct N2::X5<void>::Inner2;
97 template struct X5<int&>::Inner2; // expected-note{{instantiation}}
99 void f4(X5<float&>::Inner2);
100 template struct X5<float&>::Inner2; // expected-note{{instantiation}}
103 template struct N2::X5<int>::Inner2;
104 #if __cplusplus <= 199711L
105 // expected-warning@-2 {{explicit instantiation of 'Inner2' not in a namespace enclosing 'N2'}}
107 // expected-error@-4 {{explicit instantiation of 'Inner2' not in a namespace enclosing 'N2'}}
112 struct Inner { // expected-note{{here}}
117 template struct X6::Inner; // expected-error{{non-templated}}
120 template <typename T>
124 struct Foo<int> // expected-note{{header not required for explicitly-specialized}}
126 template <typename U>
131 template <> // expected-warning{{extraneous template parameter list}}
133 struct Foo<int>::Bar<void>
138 template<typename T> struct X7 { }; // expected-note{{here}}
141 template<typename T> struct X8 { };
144 template struct X7<int>;
145 template struct Inner::X8<int>;
148 template<typename T> struct X9 { }; // expected-note{{here}}
150 template struct ::N1::Inner::X8<float>;
155 template struct X7<double>;
156 #if __cplusplus <= 199711L
157 // expected-warning@-2 {{explicit instantiation of 'N1::X7' must occur in namespace 'N1'}}
159 // expected-error@-4 {{explicit instantiation of 'N1::X7' must occur in namespace 'N1'}}
162 template struct X9<float>;
163 #if __cplusplus <= 199711L
164 // expected-warning@-2 {{explicit instantiation of 'X9' must occur at global scope}}
166 // expected-error@-4 {{explicit instantiation of 'X9' must occur at global scope}}