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

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

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

問題描述

限時送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
}

但請注意特化方法中的內(nèi)聯(lián).由于方法被定義了多次,因此需要避免鏈接器錯誤(在 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?代碼不應(yīng)在每次使用時重復(fù).我搜索過谷歌,在 SO 中閱讀了一些問題,并嘗試了許多建議的解決方案,但都沒有成功構(gòu)建(至少在 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).

謝謝!

推薦答案

與簡單函數(shù)一樣,您可以使用聲明和實現(xiàn).放入您的標(biāo)題聲明:

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

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

并將實現(xiàn)放入您的一個 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
}

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

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

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

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

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導(dǎo)致訪問沖突?)
主站蜘蛛池模板: 欧美精品二区 | 国产欧美一区二区精品忘忧草 | 男女免费在线观看视频 | 欧美一级在线 | 亚洲69p| 中文字幕视频在线 | 精品一二区 | 精品国产一区二区三区性色av | 黄色在线免费观看 | 精品av久久久久电影 | 亚洲综合在线视频 | 欧美日韩国产一区二区三区 | 色综合av | 性高湖久久久久久久久aaaaa | 亚洲一区二区在线电影 | 久久精品视频一区二区 | 亚洲人成人一区二区在线观看 | 99在线免费观看视频 | 亚洲国产欧美一区二区三区久久 | 国产ts人妖系列高潮 | 欧美精品成人一区二区三区四区 | 亚洲精选一区二区 | 日本成年免费网站 | 99tv成人影院 | av av在线 | 一区二区三区久久 | 国产精品一区二区在线 | 中文字幕免费在线 | 最新国产精品精品视频 | 一级黄在线观看 | 欧美性乱 | 污视频免费在线观看 | 久在草| 久久四虎| 中文字幕亚洲在线 | 一级黄色片毛片 | 伊人性伊人情综合网 | 黄视频网址 | 狠狠婷婷综合久久久久久妖精 | 成人在线视频网站 | 超碰在线97国产 |