Notes on Templates and Friends

We have seen that functions and entire classes can be declared as friends of nontemplate classes. With class templates, friendship can be established between a class template and a global function, a member function of another class (possibly a class-template specialization), or even an entire class (possibly a class-template specialization).

Throughout this section, we assume that we have defined a class template for a class named X with a single type parameter T, as in:

template< typename T > class X

Under this assumption, it is possible to make a function f1 a friend of every class-template specialization instantiated from the class template for class X. To do so, use a friendship declaration of the form


friend void f1();

For example, function f1 is a friend of X< double >, X< string > and X< Employee >, etc.

It is also possible to make a function f2 a friend of only a class-template specialization with the same type argument. To do so, use a friendship declaration of the form

friend void f2( X< T > & );

For example, if T is a float, function f2( X< float > & ) is a friend of class-template specialization X< float > but not a friend of class-template specification X< string >.

You can declare that a member function of another class is a friend of any class-template specialization generated from the class template. To do so, the friend declaration must qualify the name of the other class's member function using the class name and the binary scope resolution operator, as in:

friend void A::f3();

The declaration makes member function f3 of class A a friend of every class-template specialization instantiated from the preceding class template. For example, function f3 of class A is a friend of X< double >, X< string > and X< Employee >, etc.

As with a global function, another class's member function can be a friend of only a class-template specialization with the same type argument. A friendship declaration of the form

friend void C< T >::f4( X< T > & );

for a particular type T such as float makes member function

C< float >::f4( X< float > & )

a friend function of only class-template specialization X< float >.

In some cases, it is desirable to make an entire class's set of member functions friends of a class template. In this case, a friend declaration of the form

friend class Y;

makes every member function of class Y a friend of every class-template specialization produced from the class template X.

Finally, it is possible to make all member functions of one class-template specialization friends of another class-template specialization with the same type argument. For example, a friend declaration of the form:

friend class Z< T >;

indicates that when a class-template specialization is instantiated with a particular type for T (such as float), all members of class Z< float > become friends of class-template specialization X< float >. We use this particular relationship in several examples of Chapter 21, Data Structures.

Категории