主题
模板友元
把模板和友元合在一起会产生几种组合:一个模板类可以拥有非模板友元、约束模板友元(bound template friend)、或非约束模板友元(unbound template friend)。本节讲清楚三种写法的区别和各自用途。
回顾:友元的作用
友元函数能直接访问类的私有成员。普通类的友元声明很直接:friend void foo(MyClass&);。但模板类 HasFriend<T> 在为不同类型实例化后会生成 HasFriend<int>、HasFriend<double> 等不同类——每个都是独立类,友元关系要单独建立。这衍生出下面几种方案。
方案一:非模板友元(对所有实例化都友好)
声明一个普通函数为友元,不依赖模板参数。那么对 HasFriend<T> 的每一种实例化,这个友元函数都能访问它:
cpp
// frnd2tmp.cpp —— 非模板友元
#include <iostream>
template <typename T>
class HasFriend
{
private:
T item;
static int count; // 每种实例化有各自的静态成员
public:
HasFriend(const T& i) : item(i) { ++count; }
~HasFriend() { --count; }
friend void TotalCount(); // 不带模板参数
friend void ReportInt(HasFriend<int>& hf); // 只对 int 实例友好
friend void ReportDouble(HasFriend<double>& hf);
};
template <typename T>
int HasFriend<T>::count = 0; // 每种特化各自一份静态成员
void TotalCount()
{
std::cout << "int 实例数: " << HasFriend<int>::count << "; "
<< "double 实例数: " << HasFriend<double>::count << "\n";
}
void ReportInt(HasFriend<int>& hf)
{
std::cout << "int 实例的 item = " << hf.item << "\n";
}
void ReportDouble(HasFriend<double>& hf)
{
std::cout << "double 实例的 item = " << hf.item << "\n";
}
int main()
{
TotalCount();
HasFriend<int> a(10), b(20);
HasFriend<double> c(3.5);
TotalCount();
ReportInt(a);
ReportDouble(c);
return 0;
}TotalCount() 不带模板参数,所以它能访问所有 HasFriend<T> 实例化的私有部分——这里它直接读了 HasFriend<int>::count 和 HasFriend<double>::count。注意每种实例化各有自己的 static count:HasFriend<int>::count 和 HasFriend<double>::count 是两份独立变量。
缺点也很明显:要为每种想报告的类型各写一个 ReportXxx 重载,类型一多就很啰嗦。
方案二:约束模板友元(bound)
让友元本身也是模板,并绑定到类的模板参数——HasFriend<T> 的友元是 counts<T>(),类型一一对应:
cpp
// tmp2tmp.cpp —— 约束模板友元
#include <iostream>
template <typename T> void counts(); // 模板前置声明
template <typename T> void report(T&); // 模板前置声明
template <typename TT>
class HasFriendT
{
private:
TT item;
static int count;
public:
HasFriendT(const TT& i) : item(i) { ++count; }
~HasFriendT() { --count; }
friend void counts<TT>(); // 绑定到具体 TT
friend void report<>(HasFriendT<TT>&); // 绑定到具体 TT
};
template <typename T>
int HasFriendT<T>::count = 0;
template <typename T>
void counts()
{
std::cout << "sizeof = " << sizeof(HasFriendT<T>)
<< " count = " << HasFriendT<T>::count << "\n";
}
template <typename T>
void report(T& hf)
{
std::cout << "item = " << hf.item << "\n";
}
int main()
{
counts<int>(); // 先看空状态
HasFriendT<int> a(10), b(20);
HasFriendT<double> c(7.7);
report(a); // 生成 report(HasFriendT<int>&)
report(c); // 生成 report(HasFriendT<double>&)
counts<int>();
counts<double>();
return 0;
}关键写法:
- 先在类外前置声明友元函数模板。
- 类内
friend void counts<TT>();中的<TT>表示"用我的模板参数实例化这个友元模板"——HasFriendT<int>的友元就是counts<int>(),一一绑定。 friend void report<>(...)的空尖括号让编译器根据参数推导出模板参数。
好处是一个友元模板定义服务所有类型,不必手写重载。
方案三:非约束模板友元(unbound)
第三种:把友元声明成一个模板,但不绑定到类的模板参数——任何类型实例化的友元都能访问任何类型实例化的类:
cpp
// manyfrnd.cpp —— 非约束模板友元
#include <iostream>
template <typename T>
class ManyFriend
{
private:
T item;
public:
ManyFriend(const T& i) : item(i) {}
template <typename C, typename D>
friend void ShowTwo(const C& c, const D& d); // 不绑定到 T
};
template <typename C, typename D>
void ShowTwo(const C& c, const D& d)
{
std::cout << c.item << " 和 " << d.item << "\n";
}
int main()
{
ManyFriend<int> a(10);
ManyFriend<double> b(3.14);
ShowTwo(a, b); // C=ManyFriend<int>, D=ManyFriend<double>
return 0;
}ShowTwo<C,D> 的参数 C、D 与类的 T 完全独立。ShowTwo 能同时访问 ManyFriend<int> 和 ManyFriend<double> 的私有 item——这是前两种方案做不到的(绑定方案里一个友元实例只对应一种类实例化)。这种写法在需要"跨实例化访问"时有用。
三种方案对照
| 方案 | 友元是否模板 | 是否绑定类模板参数 | 适用 |
|---|---|---|---|
| 非模板友元 | 否 | 无关参数 | 友元逻辑通用、不依赖类型 |
| 约束模板友元 | 是 | 一一绑定 | 友元按类型各自处理 |
| 非约束模板友元 | 是 | 不绑定 | 友元需跨多种实例化访问 |
课后练习
- 静态成员分实例:照方案一写
HasFriend<T>,给int和double各实例化几个对象,调用TotalCount()观察两种count各自独立递增。 - 绑定友元:照方案二实现
HasFriendT<T>和绑定的counts<T>、report<T>,分别对int和double调用,验证"一份友元模板代码服务多类型"。 - 跨实例化访问:照方案三实现
ManyFriend<T>和非约束友元ShowTwo,用一个调用同时打印ManyFriend<int>和ManyFriend<std::string>的私有item。思考为什么绑定方案做不到这一点。 - static 初值:把
template <typename T> int HasFriendT<T>::count = 0;这行删掉再编译,观察链接报错,理解"模板静态成员需要在类外显式定义(且本身是模板)"。