C++11的模板類型判斷——std::is_same和std::decay
C++11的模板類型判斷——std::is_same和std::decay
問(wèn)題提出:有一個(gè)模板函數(shù),函數(shù)在處理int型和double型時(shí)需要進(jìn)行特殊的處理,那么怎么在編譯期知道傳入的參數(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)的方法,上面的例子具體用到了其中兩個(gè)結(jié)構(gòu):
std::is_same 判斷類型是否一致
位于頭文件中
這個(gè)結(jié)構(gòu)體作用很簡(jiǎn)單,就是兩個(gè)一樣的類型會(huì)返回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
}通過(guò)std::is_same即可判斷兩個(gè)類型是否一樣,特別在模板里面,在不清楚模板的參數(shù)時(shí),此功能可以對(duì)一些特定的參數(shù)類型進(jìn)行特殊的處理。
這里說(shuō)個(gè)題外話,大家是否通過(guò)std::is_same發(fā)現(xiàn),char既不是unsigned char也不是signed char,char就是char,這和int是signed int的縮寫(xiě)是不一樣的,char的表達(dá)范圍可能等同于signed char,也可能等同于unsigned char,取決于編譯器,一般是等同于signed char,但這個(gè)僅僅是范圍等同,就像32位上int和long范圍是一樣的,但不是同一個(gè)類型。
因?yàn)橛猛静煌?,char用于表達(dá)字符,理論上不應(yīng)該關(guān)心其正負(fù)的實(shí)現(xiàn),而signed char 和 unsigned char 用于表達(dá)數(shù)值,或可移植的char。
回到正文,std::is_same可以判斷兩種類似是否一樣,那么用在模板里就是利器了,本位一開(kāi)始提到的那個(gè)問(wèn)題就可以這樣寫(xiě):
#includetemplatetypeCheck(TYPE?data)
{
????if(std::is_same::value)
????{
????????std::cout<<"int?type";
????????//do?something?int?
????}
????else
????{
????????//.........
????}
}視乎很美好,再看一個(gè)示例:
//?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的判斷是很嚴(yán)格的
但是有時(shí)候在編輯模板的時(shí)候又發(fā)現(xiàn)用std::is_same的判斷太過(guò)嚴(yán)格,還是之前的例子:
#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
測(cè)試后發(fā)現(xiàn),雖然變量b,c使用引用,但std::is_same還是能識(shí)別出來(lái)的,但是!!
如果我顯示的指定模板參數(shù)類型時(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對(duì)int和const intint &const int&等都是區(qū)別對(duì)待的,但在寫(xiě)模板函數(shù)時(shí),經(jīng)常會(huì)強(qiáng)制指定常引用進(jìn)行傳參,以免進(jìn)行數(shù)據(jù)拷貝,這時(shí)候is_same就做出了不相等的判斷,但是有時(shí)候其實(shí)我們還是希望TYPE和const TYPE& 是能認(rèn)為是一樣的,這時(shí)就需要std::decay進(jìn)行退化處理
std::decay 退化類型的修飾
std::decay就是對(duì)一個(gè)類型進(jìn)行退化處理,他的實(shí)現(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;
};看著比較抽象,其實(shí)就是把各種引用啊什么的修飾去掉,把cosnt int&退化為int,這樣就能通過(guò)std::is_same正確識(shí)別出加了引用的類型了
上面的例子改為:
#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有個(gè)更加詳細(xì)的例子:
#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é): 在模板里可以通過(guò)std::is_same判斷模板的類型,從而實(shí)現(xiàn)對(duì)不同類型的區(qū)別對(duì)待 在堆類型要求不是非常嚴(yán)格的情況下,可以使用std::decay把類型退化為基本形態(tài),結(jié)合std::is_same用,可以判斷出更多的情況





