模板是一种源码复用技术,在某些情况下使用它可以使代码看起来非常优雅,著名的boost库就是模板发扬的典范.
然而模板也存在弊端,大量使用模板可能导致代码膨胀.下面介绍一种解决的方案:
我们首先看一段一般代码:
template < typename T >
class Vector
![clip_image002 clip_image002](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image002_thumb.gif)
{
public :
virtual void Say()
![clip_image005 clip_image005](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image005_thumb.gif)
{
const type_info & t = typeid(T);
std::cout << " Vector< " << t.name() << " >::Hello " << std::endl;
}
} ;
![clip_image001 clip_image001](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image001_thumb_2.gif)
// 特例化
template <>
class Vector < void *>
![clip_image002 clip_image002](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image002_thumb_1.gif)
{
public :
virtual void Say()
![clip_image005 clip_image005](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image005_thumb_1.gif)
{
std::cout << " Vector<void*>::Hello " << std::endl;
}
} ;
![clip_image001 clip_image001](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image001_thumb_6.gif)
int _tmain( int argc, _TCHAR * argv[])
![clip_image002 clip_image002](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image002_thumb_2.gif)
{
Vector < int *> pIntVec;
pIntVec.Say();
Vector < double *> pDoubleVec;
pDoubleVec.Say();
return 0 ;
}
输出:
Vector<int *>::Hello
Vector<double *>::Hello
从这里,我们可以看出程序在运行的过程中生产了两分Vector的代码(int*和double*),尽管这里有特例化,然而类型不匹配,编译器在编译过程中无法定位到特例化版本.如果这个Vector被滥用,即使是一个中等规模的程序也可能耗费成兆字节的代码控件.
我们必须寻找一种中间桥梁使编译器在编译过程中定位到void*的特例化版本.按照模板选择策略,编译器总是选择最特例化的模板,我们可以通过一个中间的模板联系起上面两个版本的模板.下面看一段代码:
template < typename T >
class Vector
![clip_image002 clip_image002](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image002_thumb_3.gif)
{
public :
virtual void Say()
![clip_image005 clip_image005](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image005_thumb_2.gif)
{
std::cout << " Vector::Hello " << std::endl;
}
} ;
![clip_image001 clip_image001](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image001_thumb_10.gif)
// 特例化
template <>
class Vector < void *>
![clip_image002 clip_image002](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image002_thumb_4.gif)
{
public :
virtual void Say()
![clip_image005 clip_image005](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image005_thumb_3.gif)
{
std::cout << " Vector<void*>::Hello " << std::endl;
}
} ;
![clip_image001 clip_image001](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image001_thumb_14.gif)
// 特例化
template < typename T >
class Vector < T *> : public Vector < void *>
![clip_image002 clip_image002](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image002_thumb_5.gif)
{
} ;
![clip_image001 clip_image001](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image001_thumb_18.gif)
int _tmain( int argc, _TCHAR * argv[])
![clip_image002 clip_image002](http://www.cppblog.com/images/cppblog_com/woaidongmao/WindowsLiveWriter/3d1865f053d3_908B/clip_image002_thumb_6.gif)
{
Vector < int *> pIntVec;
pIntVec.Say();
Vector < double *> pDoubleVec;
pDoubleVec.Say();
return 0 ;
}
输出:
Vector<void*>::Hello
Vector<void*>::Hello
从这里,我们可以看出程序在运行过程中全部使用Vector<void*>的版本.class Vector<T*>模板便是关键的环节.编译器在编译过程中,由于class Vector<T*>比class Vector更特例化,所以遇到Vector<int*>和Vector<double*>时都选择class Vector<T*>,而class Vector<T*>继承Vector<void*>,一个更加特例化版本,所以编译器不再产生新的模板而采用 Vector<void*>这个最特例化版本,这样所有指针模板都归结到Vector<void*>,而class Vector<T*>为用户提供了一个精美而且安全的界面,而Vector<void*>作为底层实现细节被隐藏.