__typeof __()和__typeof()是C語言的特定于編譯器的擴(kuò)展缝呕,因?yàn)闃?biāo)準(zhǔn)C不包含這樣的運(yùn)算符。 標(biāo)準(zhǔn)C要求編譯器使用雙下劃線為語言擴(kuò)展添加前綴(這也是您不應(yīng)該為自己的函數(shù)撞秋,變量等執(zhí)行此操作的原因)
typeof()完全相同耻蛇,但是將下劃線拋出窗口恶座,理解每個(gè)現(xiàn)代編譯器都支持它鲤拿。 (實(shí)際上,現(xiàn)在我想到它歼疮,Visual C ++可能不會(huì)杂抽。它確實(shí)支持decltype(),它通常提供與typeof()相同的行為韩脏。)
所有三個(gè)都意味著相同的東西缩麸,但沒有一個(gè)是標(biāo)準(zhǔn)的C,所以符合標(biāo)準(zhǔn)的編譯器可以選擇使任何意思有所不同赡矢。
#include <iostream>
using namespace std;
#define min(x,y) ({\
typeof(x) _x=x;\
typeof(y) _y=y;\
(void) (&_x==&_y);\
_x<_y?_x:_y;})
int main()
{
int *pvar=NULL;
typeof(*pvar) var=999;
typeof(pvar) vvar=&var;
cout<<var<<endl;
cout<<vvar<<endl;
cout<<min('a','b')<<endl;
cout<<min(2,44)<<endl;
}
999
0x7ffe8a4951cc
a
2
#include <iostream>
using namespace std;
int main()
{
int i=0;
decltype(i) d=2;
decltype((i)) dd=d;
cout<<d<<endl;
cout<<dd<<endl;
}
root@iZwz9d2igx9zyp8enw8g5pZ:/home/leecode# g++ test1.cpp -o test1 -std=c++11
root@iZwz9d2igx9zyp8enw8g5pZ:/home/leecode# ./test1
2
2
decltype((variable))(注意是雙層括號(hào))的結(jié)果永遠(yuǎn)是引用杭朱,而decltype(variable)結(jié)果只有當(dāng)variable本身就是一個(gè)引用時(shí)才是引用
參考:
https://stackoverflow.com/questions/14877415/difference-between-typeof-typeof-and-typeof-objective-c
https://blog.csdn.net/zhanshen2015/article/details/51495273/