c++ 一些模板總結和反思
template.h
template <typename T>class Test{
public:
T testFunction();
template <typename D>
void printD(D data);
};
template <typename T>
template <typename It>
void Test<T>::printD(It data) {
}
template.cpp
#include "template.h"
template <typename T>
T Test<T>::testFunction() {
}
//explicit instantation
template class Test<int>;
注意類模板內的成員模板則必須要在一個文件中,如果定義的是成員函數則不必
再就是c++ 的一個控制實例化問題
比如說類模板的顯示實例化
template class Test<int>;
就是在編譯器就已經實例化了,如果我們告訴編譯器已經在別的位置被顯示實例化了,可以使用extern
extern template class Test<int>;