Jen*_*lom 9 c++ templates c++11
考虑以下:
template<int N>
class A
{
public:
A() : i(N) {}
template<int K>
void foo(A<K> other)
{
i = other.i; // <-- other.i is private
}
private:
int i;
};
int main()
{
A<1> a1;
A<2> a2;
a1.foo(a2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使'other.i'可见而不将成员i和foo移动到一个共同的基类或做一些疯狂的事情,如添加朋友类A <1>?
也就是说,有没有办法制作相同模板类朋友的模板?
Pup*_*ppy 10
C++ 03没有为此提供机制,但C++ 11确实如此.
template<int N2> friend class A;
Run Code Online (Sandbox Code Playgroud)
应该朋友的所有实例A.