| David Blaikie | cc5f8f0 | 2011-10-18 05:54:07 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat %s |
| Charles Li | 9ea0817 | 2017-02-24 22:22:05 +0000 | [diff] [blame] | 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 |
| Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 4 | // |
| 5 | // Tests explicit instantiation of templates. |
| 6 | template<typename T, typename U = T> class X0 { }; |
| 7 | |
| 8 | namespace N { |
| 9 | template<typename T, typename U = T> class X1 { }; |
| 10 | } |
| 11 | |
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 12 | // Check the syntax of explicit instantiations. |
| Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 13 | template class X0<int, float>; |
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 14 | template class X0<int>; // expected-note{{previous}} |
| Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 15 | |
| 16 | template class N::X1<int>; |
| 17 | template class ::N::X1<int, float>; |
| 18 | |
| 19 | using namespace N; |
| Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 20 | |
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 21 | // Check for some bogus syntax that probably means that the user |
| 22 | // wanted to write an explicit specialization, but forgot the '<>' |
| 23 | // after 'template'. |
| Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 24 | template class X0<double> { }; // expected-error{{explicit specialization}} |
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 25 | |
| 26 | // Check for explicit instantiations that come after other kinds of |
| 27 | // instantiations or declarations. |
| Douglas Gregor | f61eca9 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 28 | template class X0<int, int>; // expected-error{{duplicate}} |
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 29 | |
| 30 | template<> class X0<char> { }; // expected-note{{previous}} |
| Richard Smith | e4caa48 | 2016-08-31 23:23:25 +0000 | [diff] [blame] | 31 | template class X0<char>; // expected-warning{{has no effect}} |
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 32 | |
| Douglas Gregor | f61eca9 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 33 | void foo(X0<short>) { } |
| 34 | template class X0<short>; |
| Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 35 | |
| 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}} |
| 40 | |
| 41 | template struct X2<float>; // expected-error{{undefined template}} |
| 42 | |
| 43 | template<typename T> |
| 44 | struct X2 { |
| 45 | void f0(T*); // expected-error{{pointer to a reference}} |
| 46 | }; |
| 47 | |
| 48 | template struct X2<int>; // okay |
| 49 | template struct X2<int&>; // expected-note{{in instantiation of}} |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 50 | |
| 51 | // Check that explicit instantiations instantiate member classes. |
| 52 | template<typename T> struct X3 { |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 53 | struct Inner { |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 54 | void f(T*); // expected-error{{pointer to a reference}} |
| 55 | }; |
| 56 | }; |
| 57 | |
| 58 | void f1(X3<int&>); // okay, Inner, not instantiated |
| 59 | |
| 60 | template struct X3<int&>; // expected-note{{instantiation}} |
| 61 | |
| 62 | template<typename T> struct X4 { |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 63 | struct Inner { |
| 64 | struct VeryInner { |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 65 | void f(T*); // expected-error 2{{pointer to a reference}} |
| 66 | }; |
| 67 | }; |
| 68 | }; |
| 69 | |
| 70 | void f2(X4<int&>); // okay, Inner, not instantiated |
| 71 | void f3(X4<int&>::Inner); // okay, Inner::VeryInner, not instantiated |
| 72 | |
| 73 | template struct X4<int&>; // expected-note{{instantiation}} |
| 74 | template struct X4<float&>; // expected-note{{instantiation}} |
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 75 | |
| 76 | // Check explicit instantiation of member classes |
| 77 | namespace N2 { |
| 78 | |
| 79 | template<typename T> |
| 80 | struct X5 { |
| 81 | struct Inner1 { |
| 82 | void f(T&); |
| 83 | }; |
| 84 | |
| Richard Smith | 050d261 | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 85 | struct Inner2 { // expected-note {{here}} |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 86 | struct VeryInner { |
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 87 | void g(T*); // expected-error 2{{pointer to a reference}} |
| 88 | }; |
| 89 | }; |
| 90 | }; |
| 91 | |
| 92 | } |
| 93 | |
| 94 | template struct N2::X5<void>::Inner2; |
| 95 | |
| 96 | using namespace N2; |
| 97 | template struct X5<int&>::Inner2; // expected-note{{instantiation}} |
| 98 | |
| 99 | void f4(X5<float&>::Inner2); |
| 100 | template struct X5<float&>::Inner2; // expected-note{{instantiation}} |
| 101 | |
| 102 | namespace N3 { |
| Charles Li | 9ea0817 | 2017-02-24 22:22:05 +0000 | [diff] [blame] | 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'}} |
| 106 | #else |
| 107 | // expected-error@-4 {{explicit instantiation of 'Inner2' not in a namespace enclosing 'N2'}} |
| 108 | #endif |
| Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | struct X6 { |
| 112 | struct Inner { // expected-note{{here}} |
| 113 | void f(); |
| 114 | }; |
| 115 | }; |
| 116 | |
| 117 | template struct X6::Inner; // expected-error{{non-templated}} |
| Douglas Gregor | 6591149 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 118 | |
| 119 | // PR5559 |
| 120 | template <typename T> |
| 121 | struct Foo; |
| 122 | |
| 123 | template <> |
| 124 | struct Foo<int> // expected-note{{header not required for explicitly-specialized}} |
| 125 | { |
| 126 | template <typename U> |
| 127 | struct Bar |
| 128 | {}; |
| 129 | }; |
| 130 | |
| 131 | template <> // expected-warning{{extraneous template parameter list}} |
| 132 | template <> |
| 133 | struct Foo<int>::Bar<void> |
| 134 | {}; |
| Douglas Gregor | c97d7a2 | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 135 | |
| 136 | namespace N1 { |
| 137 | |
| 138 | template<typename T> struct X7 { }; // expected-note{{here}} |
| 139 | |
| 140 | namespace Inner { |
| 141 | template<typename T> struct X8 { }; |
| 142 | } |
| 143 | |
| 144 | template struct X7<int>; |
| 145 | template struct Inner::X8<int>; |
| 146 | } |
| 147 | |
| 148 | template<typename T> struct X9 { }; // expected-note{{here}} |
| 149 | |
| 150 | template struct ::N1::Inner::X8<float>; |
| 151 | |
| 152 | namespace N2 { |
| 153 | using namespace N1; |
| 154 | |
| Charles Li | 9ea0817 | 2017-02-24 22:22:05 +0000 | [diff] [blame] | 155 | template struct X7<double>; |
| 156 | #if __cplusplus <= 199711L |
| 157 | // expected-warning@-2 {{explicit instantiation of 'N1::X7' must occur in namespace 'N1'}} |
| 158 | #else |
| 159 | // expected-error@-4 {{explicit instantiation of 'N1::X7' must occur in namespace 'N1'}} |
| 160 | #endif |
| Douglas Gregor | c97d7a2 | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 161 | |
| Charles Li | 9ea0817 | 2017-02-24 22:22:05 +0000 | [diff] [blame] | 162 | template struct X9<float>; |
| 163 | #if __cplusplus <= 199711L |
| 164 | // expected-warning@-2 {{explicit instantiation of 'X9' must occur at global scope}} |
| 165 | #else |
| 166 | // expected-error@-4 {{explicit instantiation of 'X9' must occur at global scope}} |
| 167 | #endif |
| Douglas Gregor | c97d7a2 | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 168 | } |