本部分包括类模板,函数模板以及成员模板。
一、class template(类模板)
理解起来比较简单
template<typename T>
class complex
{
public:
complex (T r = 0, T i = 0)
: re (r), im (i)
{ }
complex& operator += (const complex&);
T real () const { return re; }
T imag () const { return im; }
private:
T re, im;
friend complex& __doapl (complex*, const complex&);
};
{
complex<double> c1(2.5, 1.5);
complex<int> c2(2, 6);
}
二、function template(函数模板)
template <class T>
inline
const T& min(const T& a, const T& b)
{
return b < a ? b : a;
}
stone r1(2, 3), r2(3, 3), r3;
r3 = min(r1, r2); //使用时不需要指明类型,因为函数调用肯定有参数,所以编译器可以进行实参推导
与类模板不同的是,函数模板不需要指明类型,编译器会进行实参推导(argument deduction)。还有一点要注意的是:模板块的编译编出来是一个“半成品”,真正使用的时候会和应用程序再编译一次。比如说这个例子,在模板阶段编译器并不知道T能不能用<比大小,只有在使用的时候才知道。
三、member template(成员模板)
先看一个标准库中的例子:
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) {}
};
粗略的定义:自己是模板内的一个成员,同时自己也是模板。那么这个特性有什么用呢?看下面这个例子:
class Base1 {};
class Derived1 : public Base1 {};
class Base2 {};
class Derived2 : public Base2 {};
pair<Derived1, Derived2> p;
pair<Base1, Base2> p2(p);//这里实际调用的是pair<Base1, Base2> p2(pair<Derived1, Derived2>())
我们会在标准库的大量类的构造函数中看到这种用法,能让构造函数更有弹性。下面是一个shared_ptr中的例子:
template <typename _Tp>
class shared_ptr : public __shared_ptr<_Tp>
{
...
template<typename _Tp1>
explicit shared_ptr(_Tp1* __p)
:__shared_ptr<_Tp>(__p) {}
...
};
Base1* ptr = new Derived1; //up-cast
shared_ptr<Base1> sptr(new Derived1); //模拟up-cast