將C+模板函數(shù)定義存儲在.cpp文件中我有一些模板代碼,我更希望存儲在CPP文件中,而不是頭中的內(nèi)聯(lián)代碼。我知道,只要您知道將使用哪種模板類型,就可以這樣做。例如:.h文件class foo{public:
template <typename T>
void do(const T& t);};.cpp文件template <typename T>void foo::do(const T& t){
// Do something with t}template void foo::do<int>(const int&);template void foo::do<std::string>(const std::string&);注意最后兩行-foo:do模板函數(shù)僅用于ints和std:string,因此這些定義意味著應用程序?qū)㈡溄?。我的問題是-這是一個令人討厭的黑客攻擊,還是與其他編譯器/鏈接器一起工作?目前我只在VS 2008中使用此代碼,但希望移植到其他環(huán)境。
3 回答

森欄
TA貢獻1810條經(jīng)驗 獲得超5個贊
template<typename T>class foo{public: void bar(const T &t);};
template <class T>void foo<T>::bar(const T &t){ }// Explicit template instantiationtemplate class foo<int>;

一只名叫tom的貓
TA貢獻1906條經(jīng)驗 獲得超3個贊
非導出函數(shù)模板、非導出成員函數(shù)模板或類模板的非導出成員函數(shù)或靜態(tài)數(shù)據(jù)成員的定義應存在于顯式實例化的每個轉(zhuǎn)換單元中。
- 3 回答
- 0 關(guān)注
- 1803 瀏覽
添加回答
舉報
0/150
提交
取消