1 // RUN: %clang_cc1 -fsyntax-only -verify %s 2 // RUN: %clang_cc1 -fsyntax-only -verify %s -fdelayed-template-parsing -DDELAYED_TEMPLATE_PARSING 3 4 5 6 // Errors 7 export class foo { }; // expected-error {{expected template}} 8 template x; // expected-error {{C++ requires a type specifier for all declarations}} \ 9 // expected-error {{does not refer}} 10 export template x; // expected-error {{expected '<' after 'template'}} 11 export template<class T> class x0; // expected-warning {{exported templates are unsupported}} 12 template < ; // expected-error {{expected template parameter}} \ 13 // expected-error{{expected ',' or '>' in template-parameter-list}} \ 14 // expected-warning {{declaration does not declare anything}} 15 template <int +> struct x1; // expected-error {{expected ',' or '>' in template-parameter-list}} 16 17 // verifies that we only walk to the ',' & still produce errors on the rest of the template parameters 18 template <int +, T> struct x2; // expected-error {{expected ',' or '>' in template-parameter-list}} \ 19 expected-error {{expected unqualified-id}} 20 template<template<int+>> struct x3; // expected-error {{expected ',' or '>' in template-parameter-list}} \ 21 expected-error {{template template parameter requires 'class' after the parameter list}} 22 template <template X> struct Err1; // expected-error {{expected '<' after 'template'}} \ 23 // expected-error{{extraneous}} 24 template <template <typename> > struct Err2; // expected-error {{template template parameter requires 'class' after the parameter list}} 25 template <template <typename> Foo> struct Err3; // expected-error {{template template parameter requires 'class' after the parameter list}} 26 27 // Template function declarations 28 template <typename T> void foo(); 29 template <typename T, typename U> void foo(); 30 31 // Template function definitions. 32 template <typename T> void foo() { } 33 34 // Template class (forward) declarations 35 template <typename T> struct A; 36 template <typename T, typename U> struct b; 37 template <typename> struct C; 38 template <typename, typename> struct D; 39 40 // Forward declarations with default parameters? 41 template <typename T = int> class X1; 42 template <typename = int> class X2; 43 44 // Forward declarations w/template template parameters 45 template <template <typename> class T> class TTP1; 46 template <template <typename> class> class TTP2; 47 template <template <typename> class T = foo> class TTP3; // expected-error{{must be a class template}} 48 template <template <typename> class = foo> class TTP3; // expected-error{{must be a class template}} 49 template <template <typename X, typename Y> class T> class TTP5; 50 51 // Forward declarations with non-type params 52 template <int> class NTP0; 53 template <int N> class NTP1; 54 template <int N = 5> class NTP2; 55 template <int = 10> class NTP3; 56 template <unsigned int N = 12u> class NTP4; 57 template <unsigned int = 12u> class NTP5; 58 template <unsigned = 15u> class NTP6; 59 template <typename T, T Obj> class NTP7; 60 61 // Template class declarations 62 template <typename T> struct A { }; 63 template <typename T, typename U> struct B { }; 64 65 // Template parameter shadowing 66 template<typename T, // expected-note{{template parameter is declared here}} 67 typename T> // expected-error{{declaration of 'T' shadows template parameter}} 68 void shadow1(); 69 70 template<typename T> // expected-note{{template parameter is declared here}} 71 void shadow2(int T); // expected-error{{declaration of 'T' shadows template parameter}} 72 73 template<typename T> // expected-note{{template parameter is declared here}} 74 class T { // expected-error{{declaration of 'T' shadows template parameter}} 75 }; 76 77 template<int Size> // expected-note{{template parameter is declared here}} 78 void shadow3(int Size); // expected-error{{declaration of 'Size' shadows template parameter}} 79 80 // <rdar://problem/6952203> 81 template<typename T> // expected-note{{here}} 82 struct shadow4 { 83 int T; // expected-error{{shadows}} 84 }; 85 86 template<typename T> // expected-note{{here}} 87 struct shadow5 { 88 int T(int, float); // expected-error{{shadows}} 89 }; 90 91 template<typename T, // expected-note{{template parameter is declared here}} 92 T T> // expected-error{{declaration of 'T' shadows template parameter}} 93 void shadow6(); 94 95 template<typename T, // expected-note{{template parameter is declared here}} 96 template<typename> class T> // expected-error{{declaration of 'T' shadows template parameter}} 97 void shadow7(); 98 99 // PR8302 100 template<template<typename> class T> struct shadow8 { // expected-note{{template parameter is declared here}} 101 template<template<typename> class T> struct inner; // expected-error{{declaration of 'T' shadows template parameter}} 102 }; 103 104 // Non-type template parameters in scope 105 template<int Size> 106 void f(int& i) { 107 i = Size; 108 #ifdef DELAYED_TEMPLATE_PARSING 109 Size = i; 110 #else 111 Size = i; // expected-error{{expression is not assignable}} 112 #endif 113 } 114 115 template<typename T> 116 const T& min(const T&, const T&); 117 118 void f2() { 119 int x; 120 A< typeof(x>1) > a; 121 } 122 123 124 // PR3844 125 template <> struct S<int> { }; // expected-error{{explicit specialization of non-template struct 'S'}} 126 template <> union U<int> { }; // expected-error{{explicit specialization of non-template union 'U'}} 127 128 namespace PR6184 { 129 namespace N { 130 template <typename T> 131 void bar(typename T::x); 132 } 133 134 template <typename T> 135 void N::bar(typename T::x) { } 136 } 137 138 // This PR occurred only in template parsing mode. 139 namespace PR17637 { 140 template <int> 141 struct L { 142 template <typename T> 143 struct O { 144 template <typename U> 145 static void Fun(U); 146 }; 147 }; 148 149 template <int k> 150 template <typename T> 151 template <typename U> 152 void L<k>::O<T>::Fun(U) {} 153 154 void Instantiate() { L<0>::O<int>::Fun(0); } 155 156 } 157 158 namespace explicit_partial_specializations { 159 typedef char (&oneT)[1]; 160 typedef char (&twoT)[2]; 161 typedef char (&threeT)[3]; 162 typedef char (&fourT)[4]; 163 typedef char (&fiveT)[5]; 164 typedef char (&sixT)[6]; 165 166 char one[1]; 167 char two[2]; 168 char three[3]; 169 char four[4]; 170 char five[5]; 171 char six[6]; 172 173 template<bool b> struct bool_ { typedef int type; }; 174 template<> struct bool_<false> { }; 175 176 #define XCAT(x,y) x ## y 177 #define CAT(x,y) XCAT(x,y) 178 #define sassert(_b_) bool_<(_b_)>::type CAT(var, __LINE__); 179 180 181 template <int> 182 struct L { 183 template <typename T> 184 struct O { 185 template <typename U> 186 static oneT Fun(U); 187 188 }; 189 }; 190 template <int k> 191 template <typename T> 192 template <typename U> 193 oneT L<k>::O<T>::Fun(U) { return one; } 194 195 template<> 196 template<> 197 template<typename U> 198 oneT L<0>::O<char>::Fun(U) { return one; } 199 200 201 void Instantiate() { 202 sassert(sizeof(L<0>::O<int>::Fun(0)) == sizeof(one)); 203 sassert(sizeof(L<0>::O<char>::Fun(0)) == sizeof(one)); 204 } 205 206 } 207