In the code below, I would like that A<N> is a friend of A<M> for any pair of M and N. Is it possible? Below you can find my attempt, which does not compile.

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);
}
1

There are 1 answers

0
cpplearner On BEST ANSWER

Yes. The correct syntax is

template<int M> friend class A;