To understand what this does, imagine the specialization produced if you declare an object of a particular type:
HasFriend
The compiler would replace the template parameter T with int, giving the friend declaration this form:
class HasFriend
{
friend void report(HasFriend
...
};
That is, report() with a HasFriend
Note that report() is not itself a template function; it just has a parameter that is a template. This means that you have to define explicit specializations for the friends you plan to use:
void report(HasFriend
void report(HasFriend
Listing 14.22 illustrates these points. The HasFriend template has a static member ct. Note that this means that each particular specialization of the class has its own static member. The counts() method, which is a friend to all HasFriend specializations, reports the value of ct for two particular specializations: HasFriend
Listing 14.22. frnd2tmp.cpp
// frnd2tmp.cpp -- template class with non-template friends
#include
using std::cout;
using std::endl;
template
class HasFriend
{
private:
T item;
static int ct;
public:
HasFriend(const T & i) : item(i) {ct++;}
~HasFriend() {ct--; }
friend void counts();
friend void reports(HasFriend
};
// each specialization has its own static data member
template
int HasFriend
// non-template friend to all HasFriend
void counts()
{
cout << "int count: " << HasFriend
cout << "double count: " << HasFriend
}
// non-template friend to the HasFriend
void reports(HasFriend
{
cout <<"HasFriend
}
// non-template friend to the HasFriend
void reports(HasFriend
{
cout <<"HasFriend
}
int main()
{
cout << "No objects declared: ";
counts();
HasFriend
cout << "After hfi1 declared: ";
counts();
HasFriend
cout << "After hfi2 declared: ";
counts();
HasFriend
cout << "After hfdb declared: ";
counts();
reports(hfi1);
reports(hfi2);
reports(hfdb);
return 0;
}
Some compilers will warn about using a non-template friend. Here is the output of the program in Listing 14.22:
No objects declared: int count: 0; double count: 0
After hfi1 declared: int count: 1; double count: 0
After hfi2 declared: int count: 2; double count: 0
After hfdb declared: int count: 2; double count: 1
HasFriend
HasFriend
HasFriend
Bound Template Friend Functions to Template Classes
You can modify the preceding example by making the friend functions templates themselves. In particular, you can set things up for bound template friends, so each specialization of a class gets a matching specialization for a friend. The technique is a bit more complex than for non-template friends and involves three steps.
For the first step, you declare each template function before the class definition:
template
template
Next, you declare the templates again as friends inside the function. These statements declare specializations based on the class template parameter type:
template
class HasFriendT
{
...
friend void counts();
friend void report<>(HasFriendT &);
};
The <> in the declarations identifies these as template specializations. In the case of report(), the <> can be left empty because the following template type argument can be deduced from the function argument:
HasFriendT
You could, however, use this instead:
report
However, the counts() function has no parameters, so you have to use the template argument syntax () to indicate its specialization. Note, too, that TT is the parameter type for the HasFriendT class.
Again, the best way to understand these declarations is to imagine what they become when you declare an object of a particular specialization. For example, suppose you declare this object:
HasFriendT