我有一个带有三个模板参数的类模板Obj1
template < class A, class B, class C >
class Obj1
{
// some implementation
};
和第二个类模板Obj2有两个模板参数,
template < class A, class B >
class Obj2
{
// some implementation
};
所以我的问题如下:
我想让类Obj1成为Obj2类的朋友,前两个模板参数具有相同的值,但我不知道如何编写它的确切语法, 起初我试过这种方式
template < class A, class B>
class Obj2
{
template< class C>
friend class Obj1<A,B,C>;
};
但它没有编译,如果可以,请求帮助我。
答案 0 :(得分:3)
您的问题的答案如下:https://stackoverflow.com/a/1458897/2436175 “你只能'与'类模板或特定完整专业化的所有实例'成为朋友。”
所以,这是允许的:
template < class A, class B, class C >
class Obj1
{
// some implementation
};
template < class A, class B>
class Obj2
{
public:
template <class C, class D, class E> friend class Obj1; ///All instances
};
template < class A, class B>
class Obj3
{
public:
friend class Obj1<A,B,int>; ///One specific instantiation, possibly depending from your own templated parameters
friend class Obj1<A,B,A>; ///One specific instantiation, possibly depending from your own templated parameters
friend class Obj1<char,float,int>; ///One specific instantiation, possibly depending from your own templated parameters
};