<分区>
我想制作 A<T>
A<T2>
的 friend 对于任何类型 T
和 T2
.
这可能吗?
谢谢。
测试(也在 godbolt.org ):
template <class T>
class A {
public:
template <typename T2> void test(A<T2>& a) { a.v_ = 2;}
private:
int v_;
template <typename T2> friend A;
};
int main() {
A<int> a;
A<int> b;
b.test(a);
return 0;
}
编译器错误:
<source>:7:28: error: friend type templates must use an elaborated type
template <typename T2> friend A;
^~~~~~~~