日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 芯聞號 > 充電吧
[導(dǎo)讀]C++11的模板類型判斷——std::is_same和std::decay問題提出:有一個模板函數(shù),函數(shù)在處理int型和double型時需要進行特殊的處理,那么怎么在編譯期知道傳入的參數(shù)的數(shù)據(jù)類型是i

C++11的模板類型判斷——std::is_same和std::decay

問題提出:有一個模板函數(shù),函數(shù)在處理int型和double型時需要進行特殊的處理,那么怎么在編譯期知道傳入的參數(shù)的數(shù)據(jù)類型是int型還是double型呢?
如:

#includetemplatevoid?typeCheck(TYPE?data)
{
????//do?something?check?data?type
????//std::cout<<?out?put?the?type
}

這里就需要用到C++11的type_traits頭文件了,type_traits頭文件定義了很多類型檢查相關(guān)的方法,上面的例子具體用到了其中兩個結(jié)構(gòu):

std::is_same 判斷類型是否一致

位于頭文件中

這個結(jié)構(gòu)體作用很簡單,就是兩個一樣的類型會返回true

bool?isInt?=?std::is_same::value;?//為true

下面是官方的例子:

#include#include#includevoid?print_separator()
{
????std::cout?<<?"-----n";
}

int?main()
{
????std::cout?<<?std::boolalpha;

????std::cout?<<?std::is_same::value?<<?'n';???//?true
????std::cout?<<?std::is_same::value?<<?'n';???//?false
????std::cout?<<?std::is_same::value?<<?'n';?//?false

????print_separator();

????std::cout?<<?std::is_same::value?<<?"n";??????????//?true
????std::cout?<<?std::is_same::value?<<?"n";?//?false
????std::cout?<<?std::is_same::value?<<?"n";???//?true

????print_separator();

????//?unlike?other?types?'char'?is?not?'unsigned'?and?not?'signed'
????std::cout?<<?std::is_same::value?<<?"n";??????????//?true
????std::cout?<<?std::is_same::value?<<?"n";?//?false
????std::cout?<<?std::is_same::value?<<?"n";???//?false
}

通過std::is_same即可判斷兩個類型是否一樣,特別在模板里面,在不清楚模板的參數(shù)時,此功能可以對一些特定的參數(shù)類型進行特殊的處理。

這里說個題外話,大家是否通過std::is_same發(fā)現(xiàn),char既不是unsigned char也不是signed char,char就是char,這和int是signed int的縮寫是不一樣的,char的表達范圍可能等同于signed char,也可能等同于unsigned char,取決于編譯器,一般是等同于signed char,但這個僅僅是范圍等同,就像32位上int和long范圍是一樣的,但不是同一個類型。

因為用途不同,char用于表達字符,理論上不應(yīng)該關(guān)心其正負的實現(xiàn),而signed char 和 unsigned char 用于表達數(shù)值,或可移植的char。

回到正文,std::is_same可以判斷兩種類似是否一樣,那么用在模板里就是利器了,本位一開始提到的那個問題就可以這樣寫:

#includetemplatetypeCheck(TYPE?data)
{
????if(std::is_same::value)
????{
????????std::cout<<"int?type";
????????//do?something?int?
????}
????else
????{
????????//.........
????}
}

視乎很美好,再看一個示例:

//?is_same?example
#include#include#includetypedef?int?integer_type;
struct?A?{?int?x,y;?};
struct?B?{?int?x,y;?};
typedef?A?C;

int?main()?{
??????std::cout?<<?std::boolalpha;
??????std::cout?<<?"is_same:"?<<?std::endl;
??????std::cout?<<?"int,?const?int:?"?<<?std::is_same::value?<<?std::endl;//false
??????std::cout?<<?"int,?int&:?"?<<?std::is_same::value?<<?std::endl;//false
??????std::cout?<<?"int,?const?int&:?"?<<?std::is_same::value?<<?std::endl;//false
??????std::cout?<<?"int,?integer_type:?"?<<?std::is_same::value?<<?std::endl;//true
??????std::cout?<<?"A,?B:?"?<<?std::is_same::value?<<?std::endl;//false
??????std::cout?<<?"A,?C:?"?<<?std::is_same::value?<<?std::endl;//true
??????std::cout?<<?"signed?char,?std::int8_t:?"?<<?std::is_same::value?<<?std::endl;//true
??????return?0;
}

輸出:

is_same:
int,?const?int:?false
int,?int&:?false
int,?const?int&:?false
int,?integer_type:?true
A,?B:?false
A,?C:?true
signed?char,?std::int8_t:?true

發(fā)現(xiàn)std::is_same的判斷是很嚴格的
但是有時候在編輯模板的時候又發(fā)現(xiàn)用std::is_same的判斷太過嚴格,還是之前的例子:

#include#include#includetemplatevoid?typeCheck(TYPE?data);

int?_tmain(int?argc,?_TCHAR*?argv[])
{
????int?a?=?1;
????const?int&?b?=?a;
????int&?c?=?a;
????int?d[12];
????const?int&?e?=?d[7];
????typeCheck(a);//int?type
????typeCheck(b);//int?type
????typeCheck(c);//int?type
????typeCheck(d[7]);//int?type
????typeCheck(e);//int?type
????typeCheck(8);//int?type
????system("pause");
????return?0;
}

templatevoid?typeCheck(TYPE?data)
{
????if(std::is_same::value)
????{
????????std::cout<<"int?type"<<std::endl;
????}
????else?if(std::is_same::value)
????{
????????std::cout<<"string?type"<<std::endl;
????}
????else
????{
????????std::cout<<"other?type";
????}
}

輸出:

int?type
int?type
int?type
int?type
int?type
int?type

測試后發(fā)現(xiàn),雖然變量b,c使用引用,但std::is_same還是能識別出來的,但是??!
如果我顯示的指定模板參數(shù)類型時情況有不一樣了:

#include#include#includetemplatevoid?typeCheck(TYPE?data);

int?_tmain(int?argc,?_TCHAR*?argv[])
{
????int?a?=?1;
????const?int&?b?=?a;
????int&?c?=?a;
????int?d[12];

????typeCheck(a);????????//int?type
????typeCheck(b);//other?type
????typeCheck(c);????????//other?type
????typeCheck(d[7]);//other?type
????typeCheck(8);????????????????//int?type
????system("pause");
????return?0;
}

templatevoid?typeCheck(TYPE?data)
{
????if(std::is_same::value)
????{
????????std::cout<<"int?type"<<std::endl;
????}
????else?if(std::is_same::value)
????{
????????std::cout<<"string?type"<<std::endl;
????}
????else
????{
????????std::cout<<"other?type";
????}
}

輸出:

int?type
other?type
other?type
other?type
int?type

瞬間結(jié)果就不一樣了,這很好了解,從上面可知道,std::is_same對int和const intint &const int&等都是區(qū)別對待的,但在寫模板函數(shù)時,經(jīng)常會強制指定常引用進行傳參,以免進行數(shù)據(jù)拷貝,這時候is_same就做出了不相等的判斷,但是有時候其實我們還是希望TYPE和const TYPE& 是能認為是一樣的,這時就需要std::decay進行退化處理

std::decay 退化類型的修飾

std::decay就是對一個類型進行退化處理,他的實現(xiàn)如下:

template<?class?T?>
struct?decay?{
private:
????typedef?typename?std::remove_reference::type?U;
public:
????typedef?typename?std::conditional<?
????????std::is_array::value,
????????typename?std::remove_extent::type*,
????????typename?std::conditional<?
????????????std::is_function::value,
????????????typename?std::add_pointer::type,
????????????typename?std::remove_cv::type
????????>::type
????>::type?type;
};

看著比較抽象,其實就是把各種引用啊什么的修飾去掉,把cosnt int&退化為int,這樣就能通過std::is_same正確識別出加了引用的類型了
上面的例子改為:

#include?"stdafx.h"
#include#include#includetemplatevoid?typeCheck(TYPE?data);

int?_tmain(int?argc,?_TCHAR*?argv[])
{
????int?a?=?1;
????const?int&?b?=?a;
????int&?c?=?a;
????int?d[12];

????typeCheck(a);//int?type
????typeCheck(b);//int?type
????typeCheck(c);//int?type
????typeCheck(d[7]);//int?type
????typeCheck(8);//int?type
????system("pause");
????return?0;
}

templatevoid?typeCheck(TYPE?data)
{
????if(std::is_same<typename?std::decay::type,int>::value)
????{
????????std::cout<<"int?type"<<std::endl;
????}
????else
????{
????????std::cout<<"other?type"<<std::endl;
????}
}

在cppref有個更加詳細的例子:

#include#includetemplatestruct?decay_equiv?:?
????std::is_same<typename?std::decay::type,?U>::type?
{};

int?main()
{
????std::cout?<<?std::boolalpha
??????????????<<?decay_equiv::value?<<?'n'
??????????????<<?decay_equiv::value?<<?'n'
??????????????<<?decay_equiv::value?<<?'n'
??????????????<<?decay_equiv::value?<<?'n'
??????????????<<?decay_equiv::value?<<?'n'
??????????????<<?decay_equiv::value?<<?'n';
}

輸出:

true
true
true
true
true
true

總結(jié): 在模板里可以通過std::is_same判斷模板的類型,從而實現(xiàn)對不同類型的區(qū)別對待 在堆類型要求不是非常嚴格的情況下,可以使用std::decay把類型退化為基本形態(tài),結(jié)合std::is_same用,可以判斷出更多的情況

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉