1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp14 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp14 %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 {{C++ requires a type specifier 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 // <rdar://problem/6952203>
90 template<typename T> // expected-note{{here}}
91 struct shadow4 {
92   int T; // expected-error{{shadows}}
93 };
94 
95 template<typename T> // expected-note{{here}}
96 struct shadow5 {
97   int T(int, float); // expected-error{{shadows}}
98 };
99 
100 template<typename T, // expected-note{{template parameter is declared here}}
101          T T> // expected-error{{declaration of 'T' shadows template parameter}}
102 void shadow6();
103 
104 template<typename T, // expected-note{{template parameter is declared here}}
105          template<typename> class T> // expected-error{{declaration of 'T' shadows template parameter}}
106 void shadow7();
107 
108 // PR8302
109 template<template<typename> class T> struct shadow8 { // expected-note{{template parameter is declared here}}
110   template<template<typename> class T> struct inner; // expected-error{{declaration of 'T' shadows template parameter}}
111 };
112 
113 // Non-type template parameters in scope
114 template<int Size>
115 void f(int& i) {
116   i = Size;
117  #ifdef DELAYED_TEMPLATE_PARSING
118   Size = i;
119  #else
120   Size = i; // expected-error{{expression is not assignable}}
121  #endif
122 }
123 
124 template<typename T>
125 const T& min(const T&, const T&);
126 
127 void f2() {
128   int x;
129   A< typeof(x>1) > a;
130 }
131 
132 
133 // PR3844
134 template <> struct S<int> { }; // expected-error{{explicit specialization of undeclared template struct 'S'}}
135 template <> union U<int> { }; // expected-error{{explicit specialization of undeclared template union 'U'}}
136 
137 struct SS;
138 union UU;
139 template <> struct SS<int> { }; // expected-error{{explicit specialization of non-template struct 'SS'}}
140 template <> union UU<int> { }; // expected-error{{explicit specialization of non-template union 'UU'}}
141 
142 namespace PR6184 {
143   namespace N {
144     template <typename T>
145     void bar(typename T::x);
146   }
147 
148   template <typename T>
149   void N::bar(typename T::x) { }
150 }
151 
152 // This PR occurred only in template parsing mode.
153 namespace PR17637 {
154 template <int>
155 struct L {
156   template <typename T>
157   struct O {
158     template <typename U>
159     static void Fun(U);
160   };
161 };
162 
163 template <int k>
164 template <typename T>
165 template <typename U>
166 void L<k>::O<T>::Fun(U) {}
167 
168 void Instantiate() { L<0>::O<int>::Fun(0); }
169 
170 }
171 
172 namespace explicit_partial_specializations {
173 typedef char (&oneT)[1];
174 typedef char (&twoT)[2];
175 typedef char (&threeT)[3];
176 typedef char (&fourT)[4];
177 typedef char (&fiveT)[5];
178 typedef char (&sixT)[6];
179 
180 char one[1];
181 char two[2];
182 char three[3];
183 char four[4];
184 char five[5];
185 char six[6];
186 
187 template<bool b> struct bool_ { typedef int type; };
188 template<> struct bool_<false> {  };
189 
190 #define XCAT(x,y) x ## y
191 #define CAT(x,y) XCAT(x,y)
192 #define sassert(_b_) bool_<(_b_)>::type CAT(var, __LINE__);
193 
194 
195 template <int>
196 struct L {
197   template <typename T>
198   struct O {
199     template <typename U>
200     static oneT Fun(U);
201 
202   };
203 };
204 template <int k>
205 template <typename T>
206 template <typename U>
207 oneT L<k>::O<T>::Fun(U) { return one; }
208 
209 template<>
210 template<>
211 template<typename U>
212 oneT L<0>::O<char>::Fun(U) { return one; }
213 
214 
215 void Instantiate() {
216   sassert(sizeof(L<0>::O<int>::Fun(0)) == sizeof(one));
217   sassert(sizeof(L<0>::O<char>::Fun(0)) == sizeof(one));
218 }
219 
220 }
221 
222 namespace func_tmpl_spec_def_in_func {
223 // We failed to diagnose function template specialization definitions inside
224 // functions during recovery previously.
225 template <class> void FuncTemplate() {}
226 void TopLevelFunc() {
227   // expected-error@+2 {{expected a qualified name after 'typename'}}
228   // expected-error@+1 {{function definition is not allowed here}}
229   typename template <> void FuncTemplate<void>() { }
230   // expected-error@+1 {{function definition is not allowed here}}
231   void NonTemplateInner() { }
232 }
233 }
234 
235 namespace broken_baseclause {
236 template<typename T>
237 struct base { };
238 
239 struct t1 : base<int, // expected-note {{to match this '<'}}
240   public:  // expected-error {{expected expression}} expected-error {{expected '>'}}
241 };
242 // expected-error@-1 {{expected '{' after base class list}}
243 struct t2 : base<int, // expected-note {{to match this '<'}}
244   public  // expected-error {{expected expression}} expected-error {{expected '>'}}
245 };
246 // expected-error@-1 {{expected '{' after base class list}}
247 
248 }
249 
250 namespace class_scope_instantiation {
251   struct A {
252     template<typename T> void f(T);
253     template void f<int>(int); // expected-error {{expected '<' after 'template'}}
254     template void f(float); // expected-error {{expected '<' after 'template'}}
255     extern template // expected-error {{expected member name or ';'}}
256       void f(double);
257   };
258 }
259 
260 namespace PR42071 {
261   template<int SomeTemplateName<void>> struct A; // expected-error {{parameter name cannot have template arguments}}
262   template<int operator+> struct B; // expected-error {{'operator+' cannot be the name of a parameter}}
263   struct Q {};
264   template<int Q::N> struct C; // expected-error {{parameter declarator cannot be qualified}}
265   template<int f(int a = 0)> struct D; // expected-error {{default arguments can only be specified for parameters in a function declaration}}
266 }
267 
268 namespace AnnotateAfterInvalidTemplateId {
269   template<int I, int J> struct A { };
270   template<int J> struct A<0, J> { }; // expected-note {{J = 0}}
271   template<int I> struct A<I, 0> { }; // expected-note {{I = 0}}
272 
273   void f() { A<0, 0>::f(); } // expected-error {{ambiguous partial specializations}}
274 }
275 
276 namespace PR45063 {
277   template<class=class a::template b<>> struct X {}; // expected-error {{undeclared identifier 'a'}}
278 }
279 
280 namespace NoCrashOnEmptyNestedNameSpecifier {
281   template <typename FnT,
282             typename T = typename ABC<FnT>::template arg_t<0>> // expected-error {{no template named 'ABC'}}
283   void foo(FnT) {}
284 }
285 
286 namespace PR45239 {
287   // Ensure we don't crash here. We used to deallocate the TemplateIdAnnotation
288   // before we'd parsed it.
289   template<int> int b;
290   template<int> auto f() -> b<0>; // expected-error +{{}}
291 }
292 
293 namespace PR46231 {
294   template; // expected-error {{declaration does not declare anything}}
295   template<>; // expected-error {{declaration does not declare anything}}
296   template<int>; // expected-error {{declaration does not declare anything}}
297   template int; // expected-error {{declaration does not declare anything}}
298   template<> int; // expected-error {{declaration does not declare anything}}
299   template<int> int; // expected-error {{declaration does not declare anything}}
300 }
301