给定一个带有非类型模板参数的 class,是否有可能对于参数的任何一对值,classes 是相互友好的?
Given a a class with a non-type template parameter, is it possible that for any pair of values of the argument the classes are mutually friend?
在下面的代码中,对于任何一对 M
和 N
,我希望 A<N>
是 A<M>
的朋友。可能吗?
下面你可以找到我的尝试,它没有编译。
template <int N> class A {
int x = N;
template<int M> friend A<M>;
public:
template<int M> void copy(const A<M>& a) {
x = a.x;
}
};
void foo() {
A<2> a2{};
A<3> a3{};
a2.copy(a3);
}
是的。正确的语法是
template<int M> friend class A;
在下面的代码中,对于任何一对 M
和 N
,我希望 A<N>
是 A<M>
的朋友。可能吗?
下面你可以找到我的尝试,它没有编译。
template <int N> class A {
int x = N;
template<int M> friend A<M>;
public:
template<int M> void copy(const A<M>& a) {
x = a.x;
}
};
void foo() {
A<2> a2{};
A<3> a3{};
a2.copy(a3);
}
是的。正确的语法是
template<int M> friend class A;