Mutual Specialization Declaration

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 to B<U,T>be a friend C<T>, that is, the second parameter B must match the parameter C, although its first parameter can be anything. How do I achieve this? A friendly declaration is A<U,V>too much, although I will consider that if I cannot limit it.

Maybe define a meta function

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

or something like that in C?

First two lines

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;

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

+4
source share

Source: https://habr.com/ru/post/1569785/


All Articles