Keep in mind that when friending function templates, you can only friend the entire template, or its explicit specialization (e.g. friendvoid f<> (A, B<int>);
template<typename T>
class B { };
template<typename T>
class A {
int private_n;
// this friends every f
template<typename T1, typename T2>
friendvoid f(A<T1>, B<T2>);
};
template<typename T1, typename T2>
void f(A<T1> a, B<T2>){
a.private_n = 1;
}
int main()
{
A<int> a;
B<double> b;
f(a, b);
}
Alternatively, you could choose to generate a separate friend template for each T1:
template<typename T>
class B { };
template<typename T>
class A {
int private_n;
// this generates a separate f for every T1
template<typename T2>
friendvoid f(A a, B<T2>) {
a.private_n = 2;
}
};
int main()
{
A<int> a;
B<double> b;
f(a, b);
}
template<typename T>
class A; // declares A as a class template
template<typename T>
class B {
int private_n;
template<typename T1, typename T2>
friendvoid f(A<T1>, B<T2>); // friends every f
};
template<typename T>
class A {
int private_n;
template<typename T1, typename T2>
friendvoid f(A<T1>, B<T2>); // friends every f
};
template<typename T1, typename T2>
void f(A<T1> a, B<T2> b){ // defines f
a.private_n = 1;
b.private_n = 2;
}
int main()
{
A<int> a;
B<double> b;
f(a, b);
}