64,282
社区成员
发帖
与我相关
我的任务
分享
extern template 已经在vs .net 2003中已经支持。
//Singleton.hpp
#include "Singleton.h"
.....
2.
//test.h
#include "Singleton.h"
class CTest
{
public:
CTest()
{
cout<<"i: "<<i<<endl;
++i;
}
~CTest()
{
}
private:
static int i;
};
//prevent the automatic instantiation of members.
extern template class Singleton<CTest>;
/test.cpp
#inlcude "test.h"
#include "Singleton.hpp"
int CTest::i = 0;
template class Singleton<CTest>; //Explicit Instantiation
3. in main.cpp or other src files that will use Singleton<CTest>
#include "CTest.h"
这样可加速编译, 只在CTest.cpp中实例化模板类Singleton<CTest>,main.cpp等用户只检查语法错误, 没有模板膨胀的问题.