1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -verify %s -fdelayed-template-parsing -DDELAYED_TEMPLATE_PARSING
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=gnu++1z %s
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-warning {{declaration does not declare anything}}
16 template <int +> struct x1; // expected-error {{expected ',' or '>' in template-parameter-list}}
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 expected-error {{template template parameter requires 'class' after the parameter list}}
23 template <template X> struct Err1; // expected-error {{expected '<' after 'template'}} \
24 // expected-error{{extraneous}}
25 template <template <typename> > struct Err2; // expected-error {{template template parameter requires 'class' after the parameter list}}
26 template <template <typename> Foo> struct Err3; // expected-error {{template template parameter requires 'class' after the parameter list}}
28 template <template <typename> typename Foo> struct Cxx1z;
29 #if __cplusplus <= 201402L
30 // expected-warning@-2 {{extension}}
33 // Template function declarations
34 template <typename T> void foo();
35 template <typename T, typename U> void foo();
37 // Template function definitions.
38 template <typename T> void foo() { }
40 // Template class (forward) declarations
41 template <typename T> struct A;
42 template <typename T, typename U> struct b;
43 template <typename> struct C;
44 template <typename, typename> struct D;
46 // Forward declarations with default parameters?
47 template <typename T = int> class X1;
48 template <typename = int> class X2;
50 // Forward declarations w/template template parameters
51 template <template <typename> class T> class TTP1;
52 template <template <typename> class> class TTP2;
53 template <template <typename> class T = foo> class TTP3; // expected-error{{must be a class template}}
54 template <template <typename> class = foo> class TTP3; // expected-error{{must be a class template}}
55 template <template <typename X, typename Y> class T> class TTP5;
57 // Forward declarations with non-type params
58 template <int> class NTP0;
59 template <int N> class NTP1;
60 template <int N = 5> class NTP2;
61 template <int = 10> class NTP3;
62 template <unsigned int N = 12u> class NTP4;
63 template <unsigned int = 12u> class NTP5;
64 template <unsigned = 15u> class NTP6;
65 template <typename T, T Obj> class NTP7;
67 // Template class declarations
68 template <typename T> struct A { };
69 template <typename T, typename U> struct B { };
71 // Template parameter shadowing
72 template<typename T, // expected-note{{template parameter is declared here}}
73 typename T> // expected-error{{declaration of 'T' shadows template parameter}}
76 template<typename T> // expected-note{{template parameter is declared here}}
77 void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}}
79 template<typename T> // expected-note{{template parameter is declared here}}
80 class T { // expected-error{{declaration of 'T' shadows template parameter}}
83 template<int Size> // expected-note{{template parameter is declared here}}
84 void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}
86 // <rdar://problem/6952203>
87 template<typename T> // expected-note{{here}}
89 int T; // expected-error{{shadows}}
92 template<typename T> // expected-note{{here}}
94 int T(int, float); // expected-error{{shadows}}
97 template<typename T, // expected-note{{template parameter is declared here}}
98 T T> // expected-error{{declaration of 'T' shadows template parameter}}
101 template<typename T, // expected-note{{template parameter is declared here}}
102 template<typename> class T> // expected-error{{declaration of 'T' shadows template parameter}}
106 template<template<typename> class T> struct shadow8 { // expected-note{{template parameter is declared here}}
107 template<template<typename> class T> struct inner; // expected-error{{declaration of 'T' shadows template parameter}}
110 // Non-type template parameters in scope
114 #ifdef DELAYED_TEMPLATE_PARSING
117 Size = i; // expected-error{{expression is not assignable}}
122 const T& min(const T&, const T&);
131 template <> struct S<int> { }; // expected-error{{explicit specialization of non-template struct 'S'}}
132 template <> union U<int> { }; // expected-error{{explicit specialization of non-template union 'U'}}
136 template <typename T>
137 void bar(typename T::x);
140 template <typename T>
141 void N::bar(typename T::x) { }
144 // This PR occurred only in template parsing mode.
148 template <typename T>
150 template <typename U>
156 template <typename T>
157 template <typename U>
158 void L<k>::O<T>::Fun(U) {}
160 void Instantiate() { L<0>::O<int>::Fun(0); }
164 namespace explicit_partial_specializations {
165 typedef char (&oneT)[1];
166 typedef char (&twoT)[2];
167 typedef char (&threeT)[3];
168 typedef char (&fourT)[4];
169 typedef char (&fiveT)[5];
170 typedef char (&sixT)[6];
179 template<bool b> struct bool_ { typedef int type; };
180 template<> struct bool_<false> { };
182 #define XCAT(x,y) x ## y
183 #define CAT(x,y) XCAT(x,y)
184 #define sassert(_b_) bool_<(_b_)>::type CAT(var, __LINE__);
189 template <typename T>
191 template <typename U>
197 template <typename T>
198 template <typename U>
199 oneT L<k>::O<T>::Fun(U) { return one; }
204 oneT L<0>::O<char>::Fun(U) { return one; }
208 sassert(sizeof(L<0>::O<int>::Fun(0)) == sizeof(one));
209 sassert(sizeof(L<0>::O<char>::Fun(0)) == sizeof(one));
214 namespace func_tmpl_spec_def_in_func {
215 // We failed to diagnose function template specialization definitions inside
216 // functions during recovery previously.
217 template <class> void FuncTemplate() {}
218 void TopLevelFunc() {
219 // expected-error@+2 {{expected a qualified name after 'typename'}}
220 // expected-error@+1 {{function definition is not allowed here}}
221 typename template <> void FuncTemplate<void>() { }
222 // expected-error@+1 {{function definition is not allowed here}}
223 void NonTemplateInner() { }
227 namespace broken_baseclause {
231 struct t1 : base<int,
232 public: // expected-error {{expected expression}}
233 }; // expected-error {{expected class name}}
234 // expected-error@-1 {{expected '{' after base class list}}
235 struct t2 : base<int,
236 public // expected-error {{expected expression}}
237 }; // expected-error {{expected class name}}
238 // expected-error@-1 {{expected '{' after base class list}}