blob: 0292964a1a765e6a2c3e891ba3ec353919b183ab [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;
18template class X1<float>;
19
Douglas Gregor93dfdb12009-05-13 00:25:59 +000020// Check for some bogus syntax that probably means that the user
21// wanted to write an explicit specialization, but forgot the '<>'
22// after 'template'.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +000023template class X0<double> { }; // expected-error{{explicit specialization}}
Douglas Gregor93dfdb12009-05-13 00:25:59 +000024
25// Check for explicit instantiations that come after other kinds of
26// instantiations or declarations.
Douglas Gregorff668032009-05-13 18:28:20 +000027template class X0<int, int>; // expected-error{{duplicate}}
Douglas Gregor93dfdb12009-05-13 00:25:59 +000028
29template<> class X0<char> { }; // expected-note{{previous}}
Douglas Gregorff668032009-05-13 18:28:20 +000030template class X0<char>; // expected-warning{{ignored}}
Douglas Gregor93dfdb12009-05-13 00:25:59 +000031
Douglas Gregorff668032009-05-13 18:28:20 +000032void foo(X0<short>) { }
33template class X0<short>;
Douglas Gregor93dfdb12009-05-13 00:25:59 +000034
35// Check that explicit instantiations actually produce definitions. We
36// determine whether this happens by placing semantic errors in the
37// definition of the template we're instantiating.
38template<typename T> struct X2; // expected-note{{declared here}}
39
40template struct X2<float>; // expected-error{{undefined template}}
41
42template<typename T>
43struct X2 {
44 void f0(T*); // expected-error{{pointer to a reference}}
45};
46
47template struct X2<int>; // okay
48template struct X2<int&>; // expected-note{{in instantiation of}}
Douglas Gregora58861f2009-05-13 20:28:22 +000049
50// Check that explicit instantiations instantiate member classes.
51template<typename T> struct X3 {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000052 struct Inner {
Douglas Gregora58861f2009-05-13 20:28:22 +000053 void f(T*); // expected-error{{pointer to a reference}}
54 };
55};
56
57void f1(X3<int&>); // okay, Inner, not instantiated
58
59template struct X3<int&>; // expected-note{{instantiation}}
60
61template<typename T> struct X4 {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000062 struct Inner {
63 struct VeryInner {
Douglas Gregora58861f2009-05-13 20:28:22 +000064 void f(T*); // expected-error 2{{pointer to a reference}}
65 };
66 };
67};
68
69void f2(X4<int&>); // okay, Inner, not instantiated
70void f3(X4<int&>::Inner); // okay, Inner::VeryInner, not instantiated
71
72template struct X4<int&>; // expected-note{{instantiation}}
73template struct X4<float&>; // expected-note{{instantiation}}
Douglas Gregor3f5b61c2009-05-14 00:28:11 +000074
75// Check explicit instantiation of member classes
76namespace N2 {
77
78template<typename T>
79struct X5 {
80 struct Inner1 {
81 void f(T&);
82 };
83
84 struct Inner2 {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000085 struct VeryInner {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +000086 void g(T*); // expected-error 2{{pointer to a reference}}
87 };
88 };
89};
90
91}
92
93template struct N2::X5<void>::Inner2;
94
95using namespace N2;
96template struct X5<int&>::Inner2; // expected-note{{instantiation}}
97
98void f4(X5<float&>::Inner2);
99template struct X5<float&>::Inner2; // expected-note{{instantiation}}
100
101namespace N3 {
102 template struct N2::X5<int>::Inner2;
103}
104
105struct X6 {
106 struct Inner { // expected-note{{here}}
107 void f();
108 };
109};
110
111template struct X6::Inner; // expected-error{{non-templated}}