summaryrefslogtreecommitdiff
path: root/test/SemaTemplate/friend-template.cpp
blob: e9b2b9b8e64e53dd4f71e81799e817929618d80c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// RUN: %clang_cc1 -fsyntax-only -verify %s
// PR5057
namespace test0 {
  namespace std {
    class X {
    public:
      template<typename T> friend struct Y;
    };
  }

  namespace std {
    template<typename T> struct Y {};
  }
}

namespace test1 {
  template<typename T> void f1(T) { } // expected-note{{here}}

  class X {
    template<typename T> friend void f0(T);
    template<typename T> friend void f1(T);
  };

  template<typename T> void f0(T) { }
  template<typename T> void f1(T) { } // expected-error{{redefinition}}
}

// PR4768
namespace test2 {
  template<typename T> struct X0 {
    template<typename U> friend struct X0;
  };
  
  template<typename T> struct X0<T*> {
    template<typename U> friend struct X0;
  };

  template<> struct X0<int> {
    template<typename U> friend struct X0;
  };

  template<typename T> struct X1 {
    template<typename U> friend void f2(U);
    template<typename U> friend void f3(U);
  };

  template<typename U> void f2(U);

  X1<int> x1i;
  X0<int*> x0ip;

  template<> void f2(int);

  // FIXME: Should this declaration of f3 be required for the specialization of
  // f3<int> (further below) to work? GCC and EDG don't require it, we do...
  template<typename U> void f3(U);

  template<> void f3(int);
}

// PR5332
namespace test3 {
  template <typename T> class Foo {
    template <typename U>
    friend class Foo;
  };

  Foo<int> foo;

  template<typename T, T Value> struct X2a;

  template<typename T, int Size> struct X2b;

  template<typename T>
  class X3 {
    template<typename U, U Value> friend struct X2a;

    // FIXME: the redeclaration note ends up here because redeclaration
    // lookup ends up finding the friend target from X3<int>.
    template<typename U, T Value> friend struct X2b; // expected-error {{template non-type parameter has a different type 'long' in template redeclaration}} \
      // expected-note {{previous non-type template parameter with type 'int' is here}}
  };

  X3<int> x3i; // okay

  X3<long> x3l; // expected-note {{in instantiation}}
}

// PR5716
namespace test4 {
  template<typename> struct A {
    template<typename T> friend void f(const A<T>&);
  };

  template<typename T> void f(const A<T>&) {
    int a[sizeof(T) ? -1 : -1]; // expected-error {{array with a negative size}}
  }

  void f() {
    f(A<int>()); // expected-note {{in instantiation of function template specialization}}
  }
}

namespace test5 {
  class outer {
    class foo;
    template <typename T> friend struct cache;
  };
  class outer::foo {
    template <typename T> friend struct cache;
  };
}

// PR6022
namespace PR6022 {
  template <class T1, class T2 , class T3  > class A;

  namespace inner {
    template<class T1, class T2, class T3, class T> 
    A<T1, T2, T3>& f0(A<T1, T2, T3>&, T);
  } 

  template<class T1, class T2, class T3>
  class A {
    template<class U1, class U2, class U3, class T>  
    friend A<U1, U2, U3>& inner::f0(A<U1, U2, U3>&, T);
  };
}

namespace FriendTemplateDefinition {
  template<unsigned > struct int_c { };

  template<typename T>
  struct X {
    template<unsigned N>
    friend void f(X, int_c<N>) {
      int value = N;
    };
  };

  void test_X(X<int> x, int_c<5> i5) {
    f(x, i5);
  }
}

namespace PR7013a {
  template<class > struct X0
  {
    typedef int type;
  };
  template<typename > struct X1
  {
  };
  template<typename , typename T> struct X2
  {
    typename T::type e;
  };
  namespace N
  {
    template <typename = int, typename = X1<int> > struct X3
    {
      template <typename T1, typename T2, typename B> friend void op(X2<T1, T2>& , B);
    };
    template <typename Ch, typename Tr, typename B> void op(X2<Ch, Tr>& , B)
    {
      X2<int, Tr> s;
    }
  }
  int n()
  {
    X2<int, X0<int> > ngs;
    N::X3<> b;
    op(ngs, b);
    return 0;
  }
}

namespace PR7013b {
  template<class > struct X0
  {
    typedef int type;
  };
  template<typename > struct X1
  {
  };
  template<typename , typename T> struct X2
  {
    typename T::type e;
  };
  namespace N
  {
    template <typename = X1<int> > struct X3
    {
      template <typename T1, typename T2, typename B> friend void op(X2<T1, T2>& , B);
    };
    template <typename Ch, typename Tr, typename B> void op(X2<Ch, Tr>& , B)
    {
      X2<int, Tr> s;
    }
  }
  int n()
  {
    X2<int, X0<int> > ngs;
    N::X3<> b;
    op(ngs, b);
    return 0;
  }

}

namespace PR8649 {
  template<typename T, typename U, unsigned N>
  struct X {
    template<unsigned M> friend class X<T, U, M>; // expected-error{{partial specialization cannot be declared as a friend}}
  };

  X<int, float, 7> x;
}

// Don't crash, and error on invalid friend type template.
namespace friend_type_template_no_tag {
  template <typename T> struct S {
    template <typename U> friend S<U>; // expected-error{{friend type templates must use an elaborated type}}
  };
  template struct S<int>;
}

namespace PR10660 {
  struct A {
    template <> friend class B; // expected-error{{extraneous 'template<>' in declaration of class 'B'}}
  };
}

namespace rdar11147355 {
  template <class T>
  struct A {
    template <class U> class B;
    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}}
  private:
    int n; // expected-note {{here}}
  };

  template <class S> template <class U> class A<S>::B {
  public:
    // FIXME: This should be permitted.
    int f(A<S*> a) { return a.n; } // expected-error {{private}}
  };

  A<double>::B<double>  ab;
  A<double*> a;
  int k = ab.f(a); // expected-note {{instantiation of}}
}

namespace RedeclUnrelated {
  struct S {
    int packaged_task;
    template<typename> class future {
      template<typename> friend class packaged_task;
    };
    future<void> share;
  };
}

namespace PR12557 {
  template <typename>
  struct Foo;

  template <typename Foo_>
  struct Bar {
    typedef Foo_  Foo; // expected-note {{previous}}

    template <typename> friend struct Foo; // expected-error {{redefinition of 'Foo' as different kind of symbol}}
  };

  Bar<int> b;
}

namespace PR12585 {
  struct A { };
  template<typename> struct B {
    template<typename> friend class A::does_not_exist; // \
     // expected-error {{friend declaration of 'does_not_exist' does not match any declaration in 'PR12585::A'}}
  };

  struct C {
    template<typename> struct D;
  };
  template<typename> class E {
    int n;
    template<typename> friend struct C::D;
  };
  template<typename T> struct C::D {
    int f() {
      return E<int>().n;
    }
  };
  int n = C::D<void*>().f();

  struct F {
    template<int> struct G;
  };
  template<typename T> struct H {
    // FIXME: As with cases above, the note here is on an unhelpful declaration,
    // and should point to the declaration of G within F.
    template<T> friend struct F::G; // \
      // expected-error {{different type 'char' in template redeclaration}} \
      // expected-note {{previous}}
  };
  H<int> h1; // ok
  H<char> h2; // expected-note {{instantiation}}
}

// Ensure that we can still instantiate a friend function template
// after the friend declaration is instantiated during the delayed
// parsing of a member function, but before the friend function has
// been parsed.
namespace rdar12350696 {
  template <class T> struct A {
    void foo() {
      A<int> a;
    }
    template <class U> friend void foo(const A<U> & a) {
      int array[sizeof(T) == sizeof(U) ? -1 : 1]; // expected-error {{negative size}}
    }
  };

  void test() {
    A<int> b;
    foo(b); // expected-note {{in instantiation}}
  }
}