十分鐘學(xué)會(huì)C++?Traits
最近和一個(gè)朋友閑聊的時(shí)候他對(duì)我說一個(gè)人對(duì)C++的理解很多種境界,朋友不是個(gè)喜歡吹牛的人,于是聽他細(xì)說,覺得很是有道理。
想寫一篇C++ traits方面的文章已經(jīng)有一段時(shí)間了,但是說實(shí)話traits這項(xiàng)技術(shù)確實(shí)有些晦澀,很擔(dān)心寫完了達(dá)不到期望的效果,于是每每試圖以簡煉的文字表達(dá),慢慢的就等到了今天。
先說說我為什么專門對(duì)這項(xiàng)技術(shù)寫一篇文章吧。記得當(dāng)時(shí)在看STL/boost代碼的時(shí)候經(jīng)常遇到traits,當(dāng)時(shí)驚嘆于代碼原來可以這樣寫,但是最初根本是看不懂的,查了一些資料才徹底理解了traits存在的意義。
本質(zhì)定義:加上一層間接性,換來以定的靈活性。
看下面的代碼:
template <typename T> struct is_void { static const bool value = false; }; template <> struct is_void<void> { static const bool value = true; };
我們可以這樣使用這份代碼:
Is_void<false>::value 調(diào)用第一份代碼,也就是說只要我們傳入一個(gè)參數(shù)像下面這樣:
Is_void<T>::value,其中T可以為任意類型,我們就可以判斷這個(gè)類型是不是void在編譯期。
完整測(cè)試代碼如下:
template <typename T> struct is_void { static const bool value = false; }; template <> struct is_void<void> { static const bool value = true; }; int _tmain(int argc, _TCHAR* argv[]) { std::cout<<is_void<int>::value; std::cout<<is_void<void>::value; return 0; }
下面我們來看一個(gè)復(fù)雜點(diǎn)的例子,考驗(yàn)一下你的理解:
namespace detail{ template <bool b> struct copier { template<typename I1, typename I2> static I2 do_copy(I1 first, I1 last, I2 out); }; template <bool b> template<typename I1, typename I2> I2 copier<b>::do_copy(I1 first, I1 last, I2 out) { while(first != last) { *out = *first; ++out; ++first; } return out; } template <> struct copier<true> { template<typename I1, typename I2> static I2* do_copy(I1* first, I1* last, I2* out) { memcpy(out, first, (last-first)*sizeof(I2)); return out+(last-first); } }; } template<typename I1, typename I2> inline I2 copy(I1 first, I1 last, I2 out) { typedef typename boost::remove_cv< typename std::iterator_traits<I1> ::value_type>::type v1_t; typedef typename boost::remove_cv< typename std::iterator_traits<I2> ::value_type>::type v2_t; enum{ can_opt = boost::is_same<v1_t, v2_t>::value && boost::is_pointer<I1>::value && boost::is_pointer<I2>::value && boost:: has_trivial_assign<v1_t>::value }; return detail::copier<can_opt>:: do_copy(first, last, out); }
總結(jié)
本文試圖以最簡潔的方式闡述對(duì)C++ traits 的理解,當(dāng)你理解了第二個(gè)例子的時(shí)候,相信你已經(jīng)理解了C++ traits,恭喜你對(duì)C++ 的理解上了一個(gè)層次。
Bibliography:
http://www.boost.org/doc/libs/1_31_0/libs/type_traits/c++_type_traits.htm
到此這篇關(guān)于十分鐘學(xué)會(huì)C++ Traits的文章就介紹到這了,更多相關(guān)C++ Traits內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
快來領(lǐng)取!你想要的C++/C語言優(yōu)秀書籍
如何選擇合適的C++/C語言書籍,是不是已經(jīng)眼花繚亂,不知道該選擇哪本好了呢?今天我來為大家分享兩本不可錯(cuò)過的優(yōu)秀書籍2017-09-09Qt利用QNetwork實(shí)現(xiàn)上傳數(shù)據(jù)的示例代碼
這篇文章主要為大家詳細(xì)介紹了Qt如何利用QNetwork實(shí)現(xiàn)上傳數(shù)據(jù)的 功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02C++實(shí)現(xiàn)圖書管理系統(tǒng)最新版
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)圖書管理系統(tǒng)最新版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06C語言中 int main(int argc,char *argv[])的兩個(gè)參數(shù)詳解
這篇文章主要介紹了C語言中 int main(int argc,char *argv[])的兩個(gè)參數(shù)詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03C++常用函數(shù)總結(jié)(algorithm 頭文件)
本文給大家詳細(xì)介紹了algorithm 頭文件中最常用的函數(shù)及其使用方法,當(dāng)然這只是其中的一部分,algorithm 頭文件中還有很多其他的函數(shù),感興趣的朋友一起看看吧2023-12-12