312 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp14 -std=gnu++14 %s2// RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp14 -std=gnu++14 %s -fdelayed-template-parsing -DDELAYED_TEMPLATE_PARSING3// RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp17 -std=gnu++1z %s4 5 6 7// Errors8export class foo { }; // expected-error {{expected template}}9template x; // expected-error {{a type specifier is required for all declarations}} \10 // expected-error {{does not refer}}11export template x; // expected-error {{expected '<' after 'template'}}12export template<class T> class x0; // expected-warning {{exported templates are unsupported}}13template < ; // expected-error {{expected template parameter}} \14// expected-error{{expected ',' or '>' in template-parameter-list}} \15// expected-error {{declaration does not declare anything}}16template <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 parameters19template <int +, T> struct x2; // expected-error {{expected ',' or '>' in template-parameter-list}} \20 expected-error {{expected unqualified-id}}21template<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}}24template <template X> struct Err1; // expected-error {{expected '<' after 'template'}} \25// expected-error{{extraneous}}26template <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}}28template <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 31template <template <typename> typename Foo> struct Cxx1z;32#if __cplusplus <= 201402L33// expected-warning@-2 {{extension}}34#endif35 36// Template function declarations37template <typename T> void foo();38template <typename T, typename U> void foo();39 40// Template function definitions.41template <typename T> void foo() { }42 43// Template class (forward) declarations44template <typename T> struct A;45template <typename T, typename U> struct b;46template <typename> struct C;47template <typename, typename> struct D;48 49// Forward declarations with default parameters?50template <typename T = int> class X1;51template <typename = int> class X2;52 53// Forward declarations w/template template parameters54template <template <typename> class T> class TTP1;55template <template <typename> class> class TTP2;56template <template <typename> class T = foo> class TTP3; // expected-error{{must be a class template}}57template <template <typename> class = foo> class TTP3; // expected-error{{must be a class template}}58template <template <typename X, typename Y> class T> class TTP5;59 60// Forward declarations with non-type params61template <int> class NTP0;62template <int N> class NTP1;63template <int N = 5> class NTP2;64template <int = 10> class NTP3;65template <unsigned int N = 12u> class NTP4;66template <unsigned int = 12u> class NTP5;67template <unsigned = 15u> class NTP6;68template <typename T, T Obj> class NTP7;69 70// Template class declarations71template <typename T> struct A { };72template <typename T, typename U> struct B { };73 74// Template parameter shadowing75template<typename T, // expected-note{{template parameter is declared here}}76 typename T> // expected-error{{declaration of 'T' shadows template parameter}}77 void shadow1();78 79template<typename T> // expected-note{{template parameter is declared here}}80void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}}81 82template<typename T> // expected-note{{template parameter is declared here}}83class T { // expected-error{{declaration of 'T' shadows template parameter}}84};85 86template<int Size> // expected-note{{template parameter is declared here}}87void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}}88 89template<typename T> // expected-note{{here}}90struct shadow4 {91 int T; // expected-error{{shadows}}92};93 94template<typename T> // expected-note{{here}}95struct shadow5 {96 int T(int, float); // expected-error{{shadows}}97};98 99template<typename T, // expected-note{{template parameter is declared here}}100 T T> // expected-error{{declaration of 'T' shadows template parameter}}101void shadow6();102 103template<typename T, // expected-note{{template parameter is declared here}}104 template<typename> class T> // expected-error{{declaration of 'T' shadows template parameter}}105void shadow7();106 107// PR8302108template<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 112template<class>113class shadow9_;114 115template<class T> // expected-note{{template parameter is declared here}}116class 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 scope121template<int Size>122void f(int& i) {123 i = Size;124 #ifdef DELAYED_TEMPLATE_PARSING125 Size = i;126 #else127 Size = i; // expected-error{{expression is not assignable}}128 #endif129}130 131template<typename T>132const T& min(const T&, const T&);133 134void f2() {135 int x;136 A< typeof(x>1) > a;137}138 139 140// PR3844141template <> struct S<int> { }; // expected-error{{explicit specialization of undeclared template struct 'S'}}142template <> union U<int> { }; // expected-error{{explicit specialization of undeclared template union 'U'}}143 144struct SS;145union UU;146template <> struct SS<int> { }; // expected-error{{explicit specialization of non-template struct 'SS'}}147template <> union UU<int> { }; // expected-error{{explicit specialization of non-template union 'UU'}}148 149namespace 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.160namespace PR17637 {161template <int>162struct L {163 template <typename T>164 struct O {165 template <typename U>166 static void Fun(U);167 };168};169 170template <int k>171template <typename T>172template <typename U>173void L<k>::O<T>::Fun(U) {}174 175void Instantiate() { L<0>::O<int>::Fun(0); }176 177}178 179namespace explicit_partial_specializations {180typedef char (&oneT)[1];181typedef char (&twoT)[2];182typedef char (&threeT)[3];183typedef char (&fourT)[4];184typedef char (&fiveT)[5];185typedef char (&sixT)[6];186 187char one[1];188char two[2];189char three[3];190char four[4];191char five[5];192char six[6];193 194template<bool b> struct bool_ { typedef int type; };195template<> struct bool_<false> { };196 197#define XCAT(x,y) x ## y198#define CAT(x,y) XCAT(x,y)199#define sassert(_b_) bool_<(_b_)>::type CAT(var, __LINE__);200 201 202template <int>203struct L {204 template <typename T>205 struct O {206 template <typename U>207 static oneT Fun(U);208 209 };210};211template <int k>212template <typename T>213template <typename U>214oneT L<k>::O<T>::Fun(U) { return one; }215 216template<>217template<>218template<typename U>219oneT L<0>::O<char>::Fun(U) { return one; }220 221 222void 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 229namespace func_tmpl_spec_def_in_func {230// We failed to diagnose function template specialization definitions inside231// functions during recovery previously.232template <class> void FuncTemplate() {}233void 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 242namespace broken_baseclause {243template<typename T>244struct base { };245 246struct 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}}250struct 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 257namespace 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 267namespace 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 275namespace 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 283namespace PR45063 {284 template<class=class a::template b<>> struct X {}; // expected-error {{undeclared identifier 'a'}}285}286 287namespace 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 293namespace PR45239 {294 // Ensure we don't crash here. We used to deallocate the TemplateIdAnnotation295 // before we'd parsed it.296 template<int> int b;297 template<int> auto f() -> b<0>; // expected-error +{{}}298}299 300namespace 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 309namespace PR99933 {310 void foo() { template <typename> int i; } // expected-error {{templates can only be declared in namespace or class scope}}311}312