Template Friends


A function that is declared to be a friend of a class template is not a member of the class. Hence, friends of a class template are not implicitly function templates (i.e. by virtue of their inclusion in the class template); however, they may explicitly be declared to be function templates. The following example demonstrates the declaration of friends within a class template.

template<class T>
class test
{
 private:
  ...
 
 public:
 
  friend void f();               // a friend of classes of the form test<T>.
 
  friend test<T>& g(test<T>*);   // function of type: test<T>& g(test<T>*)
                                 // friendly with class test<T>.
 
  friend test* h();              // syntax error - no such type as test.
};
 
template<class T>                  // definition of function template.
test<T>& g(test<T>* pointerTestT)
{...}