8

In the following code:

template <typename U, typename V> class A {};
template <typename U, typename V> class B {};

template <typename T>
class C {
    template <typename U, typename V> friend class A;  // Works fine.
//  template <typename U> friend class B<U,T>;  // Won't compile.
};

I want B<U,T> to be friend to C<T>, that is, the second parameter of B must match C's parameter, though its first parameter can be anything. How do I achieve this? The friend declaration of A<U,V> is too much, though I will take that if I can't restrict it any further.

Perhaps define a meta-function

template <typename, typename = void> struct FriendTraits { struct type{}; };

or something like that within C?

The first 2 lines of

template <typename, typename = void> struct FriendTraits { struct type{}; };
template <typename U> struct FriendTraits<U,T> { using type = B<U,T> ; }  ;
template <typename U> friend typename FriendTraits<U,T>::type;

compiled, but not the important 3rd line (because it is the same problem).

prestokeys
  • 4,817
  • 3
  • 20
  • 43
  • 1
    You can't. This is banned by [temp.friend]/p8. – T.C. Jan 03 '15 at 01:52
  • If true, then I have no choice but to declare the entire family friends then. Excessive exposure, but there's no other choice really. – prestokeys Jan 03 '15 at 02:01
  • 1
    Very late, but I just wanted to mention that there could be a workaround to simulate friendship of partial specializations, if you can change the definition of `B`. [Here's some code](http://melpon.org/wandbox/permlink/XSJHKvYZ9xuEYGyK). Maybe it will help in the future. – bogdan Nov 06 '15 at 17:46

0 Answers0