久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

模板化類中單個方法的模板特化

Template specialization of a single method from a templated class(模板化類中單個方法的模板特化)
本文介紹了模板化類中單個方法的模板特化的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

始終考慮到包含我的模板化類的以下頭文件至少包含在兩個 .CPP 文件中,此代碼編譯正確:

Always considering that the following header, containing my templated class, is included in at least two .CPP files, this code compiles correctly:

template <class T>
class TClass 
{
public:
  void doSomething(std::vector<T> * v);
};

template <class T>
void TClass<T>::doSomething(std::vector<T> * v) {
  // Do something with a vector of a generic T
}

template <>
inline void TClass<int>::doSomething(std::vector<int> * v) {
  // Do something with a vector of int's
}

但請注意特化方法中的內聯.由于方法被定義了多次,因此需要避免鏈接器錯誤(在 VS2008 中為 LNK2005).我理解這一點是因為 AFAIK 完整的模板特化與簡單的方法定義相同.

But note the inline in the specialization method. It is required to avoid a linker error (in VS2008 is LNK2005) due to the method being defined more then once. I understand this because AFAIK a full template specialization is the same as a simple method definition.

那么,我如何刪除那個 inline?代碼不應在每次使用時重復.我搜索過谷歌,在 SO 中閱讀了一些問題,并嘗試了許多建議的解決方案,但都沒有成功構建(至少在 VS 2008 中沒有).

So, how do I remove that inline? The code should not be duplicated in every use of it. I've searched Google, read some questions here in SO and tried many of the suggested solutions but none successfully built (at least not in VS 2008).

謝謝!

推薦答案

與簡單函數一樣,您可以使用聲明和實現.放入您的標題聲明:

As with simple functions you can use declaration and implementation. Put in your header declaration:

template <>
void TClass<int>::doSomething(std::vector<int> * v);

并將實現放入您的一個 cpp 文件中:

and put implementation into one of your cpp-files:

template <>
void TClass<int>::doSomething(std::vector<int> * v) {
 // Do somtehing with a vector of int's
}

不要忘記刪除內聯(我忘記并認為此解決方案不起作用:)).在 VC++2005 上檢查

Don't forget to remove inline (I forgot and thought this solution will not work :) ). Checked on VC++2005

這篇關于模板化類中單個方法的模板特化的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 成年免费大片黄在线观看一级 | 久久精品国产一区二区电影 | 免费福利视频一区二区三区 | 古装人性做爰av网站 | 日韩手机在线看片 | 久久精品国产免费 | 在线国产视频 | 成人在线 | 欧美一级免费片 | 中国黄色毛片视频 | 精品网 | 在线免费观看黄网 | 国产免费麻豆视频 | 精久久久 | 一区二区三区国产精品 | 成人精品一区 | 欧美一区不卡 | 日韩av在线一区 | 亚洲精品久久久一区二区三区 | 欧美日韩高清一区二区三区 | 亚洲国产一区二区三区在线观看 | 久久无毛| 天天躁天天操 | 99精品欧美一区二区蜜桃免费 | 日韩欧美国产精品一区 | 国产一区二区三区亚洲 | 亚洲欧美一区二区三区视频 | 亚洲精品久久久久久久久久久 | 91资源在线| 中文字幕一区二区视频 | 午夜视频一区二区 | av电影一区二区 | 精品国产精品三级精品av网址 | 一区二区三区在线观看视频 | 欧美三级电影在线播放 | 日本精品裸体写真集在线观看 | 中文字幕视频一区二区 | 亚洲精品电影网在线观看 | 亚洲综合无码一区二区 | 成人免费看电影 | 亚洲区在线 |