问题
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).
来源:https://stackoverflow.com/questions/27750755/partial-specialization-friend-declaration