//成员模板就是模板中包含模板
template <class T1,class T2>
struct pair
{
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair()
:first(T1()),second(T2()){}
pair(const T1&a, const T2& b)
:first(a),second(b){}
template<class U1,class U2>
pair(const pair<u1,U2>&p)
:first(p.first),second(p.second){}
};
构造函数大量使用
成员模板函数必须 U1是T1子类 U2是T2子类中,必须与T1 ,T2中的成员相匹配
本文深入探讨了C++中的成员模板概念,特别是在pair结构中的应用。解释了成员模板如何在构造函数中实现类型参数的灵活性,以及如何通过构造函数初始化列表来创建不同类型的pair实例。
3706

被折叠的 条评论
为什么被折叠?



