C++ template in header file
This question already has an answer here:
The C++ compiler needs to see the template definition in order to perform implicit instantiation. This means, the C++ compiler can only generate the object code for the template function call automatically if it knows the implementation at the time you invoke it.
You can, however, rely on explicit instantiation instead. This means you ask the compiler to generate the C++ code for the specific instance of the template that you specify. Then, the C++ compiler during link time will be able to find this implementation (via external linkage) when the template is used in main.cpp
.
//a.cpp
#include "a.h"
template<typename T> T foo( DWORD bar )
{
return T();
}
template int foo<int> (DWORD); // explicit instantiation
链接地址: http://www.djcxy.com/p/37136.html
上一篇: 内联函数模板专业化
下一篇: 头文件中的C ++模板