friend-template.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. // PR5057
  3. namespace test0 {
  4. namespace std {
  5. class X {
  6. public:
  7. template<typename T> friend struct Y;
  8. };
  9. }
  10. namespace std {
  11. template<typename T> struct Y {};
  12. }
  13. }
  14. namespace test1 {
  15. template<typename T> void f1(T) { } // expected-note{{here}}
  16. class X {
  17. template<typename T> friend void f0(T);
  18. template<typename T> friend void f1(T);
  19. };
  20. template<typename T> void f0(T) { }
  21. template<typename T> void f1(T) { } // expected-error{{redefinition}}
  22. }
  23. // PR4768
  24. namespace test2 {
  25. template<typename T> struct X0 {
  26. template<typename U> friend struct X0;
  27. };
  28. template<typename T> struct X0<T*> {
  29. template<typename U> friend struct X0;
  30. };
  31. template<> struct X0<int> {
  32. template<typename U> friend struct X0;
  33. };
  34. template<typename T> struct X1 {
  35. template<typename U> friend void f2(U);
  36. template<typename U> friend void f3(U);
  37. };
  38. template<typename U> void f2(U);
  39. X1<int> x1i;
  40. X0<int*> x0ip;
  41. template<> void f2(int);
  42. // FIXME: Should this declaration of f3 be required for the specialization of
  43. // f3<int> (further below) to work? GCC and EDG don't require it, we do...
  44. template<typename U> void f3(U);
  45. template<> void f3(int);
  46. }
  47. // PR5332
  48. namespace test3 {
  49. template <typename T> class Foo {
  50. template <typename U>
  51. friend class Foo;
  52. };
  53. Foo<int> foo;
  54. template<typename T, T Value> struct X2a;
  55. template<typename T, int Size> struct X2b;
  56. template<typename T>
  57. class X3 {
  58. template<typename U, U Value> friend struct X2a;
  59. // FIXME: the redeclaration note ends up here because redeclaration
  60. // lookup ends up finding the friend target from X3<int>.
  61. template<typename U, T Value> friend struct X2b; // expected-error {{template non-type parameter has a different type 'long' in template redeclaration}} \
  62. // expected-note {{previous non-type template parameter with type 'int' is here}}
  63. };
  64. X3<int> x3i; // okay
  65. X3<long> x3l; // expected-note {{in instantiation}}
  66. }
  67. // PR5716
  68. namespace test4 {
  69. template<typename> struct A {
  70. template<typename T> friend void f(const A<T>&);
  71. };
  72. template<typename T> void f(const A<T>&) {
  73. int a[sizeof(T) ? -1 : -1]; // expected-error {{array with a negative size}}
  74. }
  75. void f() {
  76. f(A<int>()); // expected-note {{in instantiation of function template specialization}}
  77. }
  78. }
  79. namespace test5 {
  80. class outer {
  81. class foo;
  82. template <typename T> friend struct cache;
  83. };
  84. class outer::foo {
  85. template <typename T> friend struct cache;
  86. };
  87. }
  88. // PR6022
  89. namespace PR6022 {
  90. template <class T1, class T2 , class T3 > class A;
  91. namespace inner {
  92. template<class T1, class T2, class T3, class T>
  93. A<T1, T2, T3>& f0(A<T1, T2, T3>&, T);
  94. }
  95. template<class T1, class T2, class T3>
  96. class A {
  97. template<class U1, class U2, class U3, class T>
  98. friend A<U1, U2, U3>& inner::f0(A<U1, U2, U3>&, T);
  99. };
  100. }
  101. namespace FriendTemplateDefinition {
  102. template<unsigned > struct int_c { };
  103. template<typename T>
  104. struct X {
  105. template<unsigned N>
  106. friend void f(X, int_c<N>) {
  107. int value = N;
  108. };
  109. };
  110. void test_X(X<int> x, int_c<5> i5) {
  111. f(x, i5);
  112. }
  113. }
  114. namespace PR7013a {
  115. template<class > struct X0
  116. {
  117. typedef int type;
  118. };
  119. template<typename > struct X1
  120. {
  121. };
  122. template<typename , typename T> struct X2
  123. {
  124. typename T::type e;
  125. };
  126. namespace N
  127. {
  128. template <typename = int, typename = X1<int> > struct X3
  129. {
  130. template <typename T1, typename T2, typename B> friend void op(X2<T1, T2>& , B);
  131. };
  132. template <typename Ch, typename Tr, typename B> void op(X2<Ch, Tr>& , B)
  133. {
  134. X2<int, Tr> s;
  135. }
  136. }
  137. int n()
  138. {
  139. X2<int, X0<int> > ngs;
  140. N::X3<> b;
  141. op(ngs, b);
  142. return 0;
  143. }
  144. }
  145. namespace PR7013b {
  146. template<class > struct X0
  147. {
  148. typedef int type;
  149. };
  150. template<typename > struct X1
  151. {
  152. };
  153. template<typename , typename T> struct X2
  154. {
  155. typename T::type e;
  156. };
  157. namespace N
  158. {
  159. template <typename = X1<int> > struct X3
  160. {
  161. template <typename T1, typename T2, typename B> friend void op(X2<T1, T2>& , B);
  162. };
  163. template <typename Ch, typename Tr, typename B> void op(X2<Ch, Tr>& , B)
  164. {
  165. X2<int, Tr> s;
  166. }
  167. }
  168. int n()
  169. {
  170. X2<int, X0<int> > ngs;
  171. N::X3<> b;
  172. op(ngs, b);
  173. return 0;
  174. }
  175. }
  176. namespace PR8649 {
  177. template<typename T, typename U, unsigned N>
  178. struct X {
  179. template<unsigned M> friend class X<T, U, M>; // expected-error{{partial specialization cannot be declared as a friend}}
  180. };
  181. X<int, float, 7> x;
  182. }
  183. // Don't crash, and error on invalid friend type template.
  184. namespace friend_type_template_no_tag {
  185. template <typename T> struct S {
  186. template <typename U> friend S<U>; // expected-error{{friend type templates must use an elaborated type}}
  187. };
  188. template struct S<int>;
  189. }
  190. namespace PR10660 {
  191. struct A {
  192. template <> friend class B; // expected-error{{extraneous 'template<>' in declaration of class 'B'}}
  193. };
  194. }
  195. namespace rdar11147355 {
  196. template <class T>
  197. struct A {
  198. template <class U> class B;
  199. template <class S> template <class U> friend class A<S>::B; // expected-warning {{dependent nested name specifier 'A<S>::' for friend template declaration is not supported; ignoring this friend declaration}}
  200. private:
  201. int n; // expected-note {{here}}
  202. };
  203. template <class S> template <class U> class A<S>::B {
  204. public:
  205. // FIXME: This should be permitted.
  206. int f(A<S*> a) { return a.n; } // expected-error {{private}}
  207. };
  208. A<double>::B<double> ab;
  209. A<double*> a;
  210. int k = ab.f(a); // expected-note {{instantiation of}}
  211. }
  212. namespace RedeclUnrelated {
  213. struct S {
  214. int packaged_task;
  215. template<typename> class future {
  216. template<typename> friend class packaged_task;
  217. };
  218. future<void> share;
  219. };
  220. }
  221. namespace PR12557 {
  222. template <typename>
  223. struct Foo;
  224. template <typename Foo_>
  225. struct Bar {
  226. typedef Foo_ Foo; // expected-note {{previous}}
  227. template <typename> friend struct Foo; // expected-error {{redefinition of 'Foo' as different kind of symbol}}
  228. };
  229. Bar<int> b;
  230. }
  231. namespace PR12585 {
  232. struct A { };
  233. template<typename> struct B {
  234. template<typename> friend class A::does_not_exist; // \
  235. // expected-error {{friend declaration of 'does_not_exist' does not match any declaration in 'PR12585::A'}}
  236. };
  237. struct C {
  238. template<typename> struct D;
  239. };
  240. template<typename> class E {
  241. int n;
  242. template<typename> friend struct C::D;
  243. };
  244. template<typename T> struct C::D {
  245. int f() {
  246. return E<int>().n;
  247. }
  248. };
  249. int n = C::D<void*>().f();
  250. struct F {
  251. template<int> struct G;
  252. };
  253. template<typename T> struct H {
  254. // FIXME: As with cases above, the note here is on an unhelpful declaration,
  255. // and should point to the declaration of G within F.
  256. template<T> friend struct F::G; // \
  257. // expected-error {{different type 'char' in template redeclaration}} \
  258. // expected-note {{previous}}
  259. };
  260. H<int> h1; // ok
  261. H<char> h2; // expected-note {{instantiation}}
  262. }
  263. // Ensure that we can still instantiate a friend function template
  264. // after the friend declaration is instantiated during the delayed
  265. // parsing of a member function, but before the friend function has
  266. // been parsed.
  267. namespace rdar12350696 {
  268. template <class T> struct A {
  269. void foo() {
  270. A<int> a;
  271. }
  272. template <class U> friend void foo(const A<U> & a) {
  273. int array[sizeof(T) == sizeof(U) ? -1 : 1]; // expected-error {{negative size}}
  274. }
  275. };
  276. void test() {
  277. A<int> b;
  278. foo(b); // expected-note {{in instantiation}}
  279. }
  280. }