朋友拥有自己的类模板和其他模板参数

Friend with own class template with other template parameter

本文关键字:其他 参数 拥有 朋友 自己的      更新时间:2023-10-16

是否可以将自己的类模板与其他模板参数声明为好友?

template<class T, class... Ts>
class A {
    template<class U> friend class A<U, Ts...>;    //compile error - C3772  'A<U>': invalid friend template declaration 
};
template<class T, class... Ts>
class A {
    template<class U, class... Us> friend class A; //here you go
};

无需在A 之后指定模板参数

类模板的部分专业化不能声明为友元。只有一个(完整的)专业,或者一个完整的课程模板。如果你真的需要与所有U专业成为朋友,你需要与整个模板成为朋友:

template<class U, class... Us> friend class A;