Debian Codegraph @

clang/test/Parser/cxx-template-decl.cpp

raw
1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp14 -std=gnu++14 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp14 -std=gnu++14 %s -fdelayed-template-parsing -DDELAYED_TEMPLATE_PARSING
3 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp17 -std=gnu++1z %s
4
5
6
7 // Errors
8 export class foo { }; // expected-error {{expected template}}
9 template x; // expected-error {{a type specifier is required for all declarations}} \
10 // expected-error {{does not refer}}
11 export template x; // expected-error {{expected '<' after 'template'}}
12 export template<class T> class x0; // expected-warning {{exported templates are unsupported}}
13 template < ; // expected-error {{expected template parameter}} \
14 // expected-error{{expected ',' or '>' in template-parameter-list}} \
15 // expected-error {{declaration does not declare anything}}
16 template <int +> struct x1; // expected-error {{expected ',' or '>' in template-parameter-list}}
17
18 // verifies that we only walk to the ',' & still produce errors on the rest of the template parameters
19 template <int +, T> struct x2; // expected-error {{expected ',' or '>' in template-parameter-list}} \
20 expected-error {{expected unqualified-id}}
21 template<template<int+>> struct x3; // expected-error {{expected ',' or '>' in template-parameter-list}} \
22 cpp14-error {{template template parameter requires 'class' after the parameter list}} \
23 cpp17-error {{template template parameter requires 'class' or 'typename' after the parameter list}}
24 template <template X> struct Err1; // expected-error {{expected '<' after 'template'}} \
25 // expected-error{{extraneous}}
26 template <template <typename> > struct Err2; // cpp14-error {{template template parameter requires 'class' after the parameter list}}
27 // cpp17-error@-1{{template template parameter requires 'class' or 'typename' after the parameter list}}
28 template <template <typename> Foo> struct Err3; // cpp14-error {{template template parameter requires 'class' after the parameter list}}
29 // cpp17-error@-1{{template template parameter requires 'class' or 'typename' after the parameter list}}
30
31 template <template <typename> typename Foo> struct Cxx1z;
32 #if __cplusplus <= 201402L
33 // expected-warning@-2 {{extension}}
34 #endif
35
36 // Template function declarations
37 template <typename T> void foo();
38 template <typename T, typename U> void foo();
39
40 // Template function definitions.
41 template <typename T> void foo() { }
42
43 // Template class (forward) declarations
44 template <typename T> struct A;
45 template <typename T, typename U> struct b;
46 template <typename> struct C;
47 template <typename, typename> struct D;
48
49 // Forward declarations with default parameters?
50 template <typename T = int> class X1;
51 template <typename = int> class X2;
52
53 // Forward declarations w/template template parameters
54 template <template <typename> class T> class TTP1;
55 template <template <typename> class> class TTP2;
56 template <template <typename> class T = foo> class TTP3; // expected-error{{must be a class template}}
57 template <template <typename> class = foo> class TTP3; // expected-error{{must be a class template}}
58 template <template <typename X, typename Y> class T> class TTP5;
59
60 // Forward declarations with non-type params
61 template <int> class NTP0;
62 template <int N> class NTP1;
63 template <int N = 5> class NTP2;
64 template <int = 10> class NTP3;
65 template <unsigned int N = 12u> class NTP4;
66 template <unsigned int = 12u> class NTP5;
67 template <unsigned = 15u> class NTP6;
68 template <typename T, T Obj> class NTP7;
69
70 // Template class declarations
71 template <typename T> struct A { };
72 template <typename T, typename U> struct B { };
73
74 // Template parameter shadowing
75 template<typename T, // expected-note{{template parameter is declared here}}
76 typename T> // expected-error{{declaration of 'T' shadows template parameter}}
77 void shadow1();
78
79 template<typename T> // expected-note{{template parameter is declared here}}
80 void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}}
81
82 template<typename T> // expected-note{{template parameter is declared here}}
83 class T { // expected-error{{declaration of 'T' shadows template parameter}}
84 };
85
86 template<int Size> // expected-note{{template parameter is declared here}}
87 void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}
88
89 template<typename T> // expected-note{{here}}
90 struct shadow4 {
91 int T; // expected-error{{shadows}}
92 };
93
94 template<typename T> // expected-note{{here}}
95 struct shadow5 {
96 int T(int, float); // expected-error{{shadows}}
97 };
98
99 template<typename T, // expected-note{{template parameter is declared here}}
100 T T> // expected-error{{declaration of 'T' shadows template parameter}}
101 void shadow6();
102
103 template<typename T, // expected-note{{template parameter is declared here}}
104 template<typename> class T> // expected-error{{declaration of 'T' shadows template parameter}}
105 void shadow7();
106
107 // PR8302
108 template<template<typename> class T> struct shadow8 { // expected-note{{template parameter is declared here}}
109 template<template<typename> class T> struct inner; // expected-error{{declaration of 'T' shadows template parameter}}
110 };
111
112 template<class>
113 class shadow9_;
114
115 template<class T> // expected-note{{template parameter is declared here}}
116 class shadow9 : public shadow9_<T> {
117 using typename shadow9_<T>::T; // expected-error{{declaration of 'T' shadows template parameter}}
118 };
119
120 // Non-type template parameters in scope
121 template<int Size>
122 void f(int& i) {
123 i = Size;
124 #ifdef DELAYED_TEMPLATE_PARSING
125 Size = i;
126 #else
127 Size = i; // expected-error{{expression is not assignable}}
128 #endif
129 }
130
131 template<typename T>
132 const T& min(const T&, const T&);
133
134 void f2() {
135 int x;
136 A< typeof(x>1) > a;
137 }
138
139
140 // PR3844
141 template <> struct S<int> { }; // expected-error{{explicit specialization of undeclared template struct 'S'}}
142 template <> union U<int> { }; // expected-error{{explicit specialization of undeclared template union 'U'}}
143
144 struct SS;
145 union UU;
146 template <> struct SS<int> { }; // expected-error{{explicit specialization of non-template struct 'SS'}}
147 template <> union UU<int> { }; // expected-error{{explicit specialization of non-template union 'UU'}}
148
149 namespace PR6184 {
150 namespace N {
151 template <typename T>
152 void bar(typename T::x);
153 }
154
155 template <typename T>
156 void N::bar(typename T::x) { }
157 }
158
159 // This PR occurred only in template parsing mode.
160 namespace PR17637 {
161 template <int>
162 struct L {
163 template <typename T>
164 struct O {
165 template <typename U>
166 static void Fun(U);
167 };
168 };
169
170 template <int k>
171 template <typename T>
172 template <typename U>
173 void L<k>::O<T>::Fun(U) {}
174
175 void Instantiate() { L<0>::O<int>::Fun(0); }
176
177 }
178
179 namespace explicit_partial_specializations {
180 typedef char (&oneT)[1];
181 typedef char (&twoT)[2];
182 typedef char (&threeT)[3];
183 typedef char (&fourT)[4];
184 typedef char (&fiveT)[5];
185 typedef char (&sixT)[6];
186
187 char one[1];
188 char two[2];
189 char three[3];
190 char four[4];
191 char five[5];
192 char six[6];
193
194 template<bool b> struct bool_ { typedef int type; };
195 template<> struct bool_<false> { };
196
197 #define XCAT(x,y) x ## y
198 #define CAT(x,y) XCAT(x,y)
199 #define sassert(_b_) bool_<(_b_)>::type CAT(var, __LINE__);
200
201
202 template <int>
203 struct L {
204 template <typename T>
205 struct O {
206 template <typename U>
207 static oneT Fun(U);
208
209 };
210 };
211 template <int k>
212 template <typename T>
213 template <typename U>
214 oneT L<k>::O<T>::Fun(U) { return one; }
215
216 template<>
217 template<>
218 template<typename U>
219 oneT L<0>::O<char>::Fun(U) { return one; }
220
221
222 void Instantiate() {
223 sassert(sizeof(L<0>::O<int>::Fun(0)) == sizeof(one));
224 sassert(sizeof(L<0>::O<char>::Fun(0)) == sizeof(one));
225 }
226
227 }
228
229 namespace func_tmpl_spec_def_in_func {
230 // We failed to diagnose function template specialization definitions inside
231 // functions during recovery previously.
232 template <class> void FuncTemplate() {}
233 void TopLevelFunc() {
234 // expected-error@+2 {{expected a qualified name after 'typename'}}
235 // expected-error@+1 {{function definition is not allowed here}}
236 typename template <> void FuncTemplate<void>() { }
237 // expected-error@+1 {{function definition is not allowed here}}
238 void NonTemplateInner() { }
239 }
240 }
241
242 namespace broken_baseclause {
243 template<typename T>
244 struct base { };
245
246 struct t1 : base<int, // expected-note {{to match this '<'}}
247 public: // expected-error {{expected expression}} expected-error {{expected '>'}}
248 };
249 // expected-error@-1 {{expected '{' after base class list}}
250 struct t2 : base<int, // expected-note {{to match this '<'}}
251 public // expected-error {{expected expression}} expected-error {{expected '>'}}
252 };
253 // expected-error@-1 {{expected '{' after base class list}}
254
255 }
256
257 namespace class_scope_instantiation {
258 struct A {
259 template<typename T> void f(T);
260 template void f<int>(int); // expected-error {{expected '<' after 'template'}}
261 template void f(float); // expected-error {{expected '<' after 'template'}}
262 extern template // expected-error {{expected member name or ';'}}
263 void f(double);
264 };
265 }
266
267 namespace PR42071 {
268 template<int SomeTemplateName<void>> struct A; // expected-error {{parameter name cannot have template arguments}}
269 template<int operator+> struct B; // expected-error {{'operator+' cannot be the name of a parameter}}
270 struct Q {};
271 template<int Q::N> struct C; // expected-error {{parameter declarator cannot be qualified}}
272 template<int f(int a = 0)> struct D; // expected-error {{default arguments can only be specified for parameters in a function declaration}}
273 }
274
275 namespace AnnotateAfterInvalidTemplateId {
276 template<int I, int J> struct A { };
277 template<int J> struct A<0, J> { }; // expected-note {{J = 0}}
278 template<int I> struct A<I, 0> { }; // expected-note {{I = 0}}
279
280 void f() { A<0, 0>::f(); } // expected-error {{ambiguous partial specializations}}
281 }
282
283 namespace PR45063 {
284 template<class=class a::template b<>> struct X {}; // expected-error {{undeclared identifier 'a'}}
285 }
286
287 namespace NoCrashOnEmptyNestedNameSpecifier {
288 template <typename FnT,
289 typename T = typename ABC<FnT>::template arg_t<0>> // expected-error {{no template named 'ABC'}}
290 void foo(FnT) {}
291 }
292
293 namespace PR45239 {
294 // Ensure we don't crash here. We used to deallocate the TemplateIdAnnotation
295 // before we'd parsed it.
296 template<int> int b;
297 template<int> auto f() -> b<0>; // expected-error +{{}}
298 }
299
300 namespace PR46231 {
301 template; // expected-error {{declaration does not declare anything}}
302 template<>; // expected-error {{declaration does not declare anything}}
303 template<int>; // expected-error {{declaration does not declare anything}}
304 template int; // expected-error {{declaration does not declare anything}}
305 template<> int; // expected-error {{declaration does not declare anything}}
306 template<int> int; // expected-error {{declaration does not declare anything}}
307 }
308
309 namespace PR99933 {
310 void foo() { template <typename> int i; } // expected-error {{templates can only be declared in namespace or class scope}}
311 }