pre*_*eys 5 c++ templates friend
在以下代码中:
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.
};
Run Code Online (Sandbox Code Playgroud)
我想B<U,T>成为朋友C<T>,也就是说,B的第二个参数必须与C的参数匹配,尽管它的第一个参数可以是任何参数.我该如何实现这一目标?朋友的声明A<U,V>太多了,但如果我不能再限制它,我会接受.
也许定义一个元功能
template <typename, typename = void> struct FriendTraits { struct type{}; };
或类似的东西在C?
前2行
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;
Run Code Online (Sandbox Code Playgroud)
编译,但不是重要的第3行(因为它是同样的问题).