Partial specialization friend declaration

非 Y 不嫁゛ 提交于 2020-01-02 04:11:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!