blob: 9c824d6f414562e424b548f5a97979636edd469a [file] [log] [blame]
Douglas Gregora58861f2009-05-13 20:28:22 +00001// RUN: clang-cc -fsyntax-only -pedantic -verify %s
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002//
3// Tests explicit instantiation of templates.
4template<typename T, typename U = T> class X0 { };
5
6namespace N {
7 template<typename T, typename U = T> class X1 { };
8}
9
Douglas Gregor93dfdb12009-05-13 00:25:59 +000010// Check the syntax of explicit instantiations.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +000011template class X0<int, float>;
Douglas Gregor93dfdb12009-05-13 00:25:59 +000012template class X0<int>; // expected-note{{previous}}
Douglas Gregor4d9a16f2009-05-12 23:25:50 +000013
14template class N::X1<int>;
15template class ::N::X1<int, float>;
16
17using namespace N;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +000018
Douglas Gregor93dfdb12009-05-13 00:25:59 +000019// Check for some bogus syntax that probably means that the user
20// wanted to write an explicit specialization, but forgot the '<>'
21// after 'template'.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +000022template class X0<double> { }; // expected-error{{explicit specialization}}
Douglas Gregor93dfdb12009-05-13 00:25:59 +000023
24// Check for explicit instantiations that come after other kinds of
25// instantiations or declarations.
Douglas Gregorff668032009-05-13 18:28:20 +000026template class X0<int, int>; // expected-error{{duplicate}}
Douglas Gregor93dfdb12009-05-13 00:25:59 +000027
28template<> class X0<char> { }; // expected-note{{previous}}
Douglas Gregorff668032009-05-13 18:28:20 +000029template class X0<char>; // expected-warning{{ignored}}
Douglas Gregor93dfdb12009-05-13 00:25:59 +000030
Douglas Gregorff668032009-05-13 18:28:20 +000031void foo(X0<short>) { }
32template class X0<short>;
Douglas Gregor93dfdb12009-05-13 00:25:59 +000033
34// Check that explicit instantiations actually produce definitions. We
35// determine whether this happens by placing semantic errors in the
36// definition of the template we're instantiating.
37template<typename T> struct X2; // expected-note{{declared here}}
38
39template struct X2<float>; // expected-error{{undefined template}}
40
41template<typename T>
42struct X2 {
43 void f0(T*); // expected-error{{pointer to a reference}}
44};
45
46template struct X2<int>; // okay
47template struct X2<int&>; // expected-note{{in instantiation of}}
Douglas Gregora58861f2009-05-13 20:28:22 +000048
49// Check that explicit instantiations instantiate member classes.
50template<typename T> struct X3 {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000051 struct Inner {
Douglas Gregora58861f2009-05-13 20:28:22 +000052 void f(T*); // expected-error{{pointer to a reference}}
53 };
54};
55
56void f1(X3<int&>); // okay, Inner, not instantiated
57
58template struct X3<int&>; // expected-note{{instantiation}}
59
60template<typename T> struct X4 {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000061 struct Inner {
62 struct VeryInner {
Douglas Gregora58861f2009-05-13 20:28:22 +000063 void f(T*); // expected-error 2{{pointer to a reference}}
64 };
65 };
66};
67
68void f2(X4<int&>); // okay, Inner, not instantiated
69void f3(X4<int&>::Inner); // okay, Inner::VeryInner, not instantiated
70
71template struct X4<int&>; // expected-note{{instantiation}}
72template struct X4<float&>; // expected-note{{instantiation}}
Douglas Gregor3f5b61c2009-05-14 00:28:11 +000073
74// Check explicit instantiation of member classes
75namespace N2 {
76
77template<typename T>
78struct X5 {
79 struct Inner1 {
80 void f(T&);
81 };
82
83 struct Inner2 {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000084 struct VeryInner {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +000085 void g(T*); // expected-error 2{{pointer to a reference}}
86 };
87 };
88};
89
90}
91
92template struct N2::X5<void>::Inner2;
93
94using namespace N2;
95template struct X5<int&>::Inner2; // expected-note{{instantiation}}
96
97void f4(X5<float&>::Inner2);
98template struct X5<float&>::Inner2; // expected-note{{instantiation}}
99
100namespace N3 {
101 template struct N2::X5<int>::Inner2;
102}
103
104struct X6 {
105 struct Inner { // expected-note{{here}}
106 void f();
107 };
108};
109
110template struct X6::Inner; // expected-error{{non-templated}}