cxx-template-decl.cpp 7.6 KB

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