There are four kinds of relationships between classes
and their friends when templates are involved:
- One-to-many: A non-template function may be a friend to
all template class instantiations.
- Many-to-one: All instantiations of a template function
may be friends to a regular non-template class.
- One-to-one: A template function instantiated with one set
of template arguments may be a friend to one template class instantiated
with the same set of template arguments. This is also the relationship
between a regular non-template class and a regular non-template friend
function.
- Many-to-many: All instantiations of a template function
may be a friend to all instantiations of the template class.
The following example demonstrates these relationships:
class B{
template<class V> friend int j();
}
template<class S> g();
template<class T> class A {
friend int e();
friend int f(T);
friend int g<T>();
template<class U> friend int h();
};