定義一個(gè)函數(shù)和指向該函數(shù)的指針,然后判斷函數(shù)指針解引用之后是否為函數(shù)void test1() {}int main(){ void(*t1)() = test1; std::cout << std::is_function<decltype(*t1)>::value << std::endl; std::cout << std::is_function<decltype(test1)>::value << std::endl; std::cout << (typeid(decltype(test1)).hash_code() == typeid(decltype(*t1)).hash_code()) << std::endl;
return 0;
}最后輸出為:011直接比較*t1和test1的類型,結(jié)果表明類型一致,但第一個(gè)輸出為什么為0已知道使用函數(shù)名調(diào)用函數(shù)時(shí)會(huì)被轉(zhuǎn)化為函數(shù)函數(shù)指針想不明白這里為什么不對(duì)是模板匹配參數(shù)的規(guī)則造成的嗎?is_function的部分實(shí)現(xiàn):template<typename> struct is_function
: public false_type { }; template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes...)>
: public true_type { };
2 回答

波斯汪
TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊
decltype(*t1)的結(jié)果不是函數(shù),而是函數(shù)引用,這是因?yàn)?t1返回一個(gè)lvalue,對(duì)于lvalue,decltype返回引用類型。
也就是說(shuō),不是
void()
而是
void (&) ()
由于是引用,is_function自然不能生效。使用remove_reference去除這個(gè)引用即可。
#include <iostream>#include <type_traits>#include <typeinfo>void test1() {}typedef void TEST();int main(){ TEST* t1 = test1; std::cout << std::is_reference<decltype(*t1)>::value << std::endl; //1 std::cout << std::is_function<std::remove_reference<decltype(*t1)>::type>::value << std::endl; // 1 return 0; }

慕的地10843
TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
decltype(*t1)的結(jié)果不是函數(shù),而是函數(shù)引用,這是因?yàn)?t1返回一個(gè)lvalue,對(duì)于lvalue,decltype返回引用類型。
也就是說(shuō),不是
void()
而是
void (&) ()
由于是引用,is_function自然不能生效。使用remove_reference去除這個(gè)引用即可。
#include <iostream>#include <type_traits>#include <typeinfo>void test1() {}typedef void TEST();int main(){ TEST* t1 = test1; std::cout << std::is_reference<decltype(*t1)>::value << std::endl; //1 std::cout << std::is_function<std::remove_reference<decltype(*t1)>::type>::value << std::endl; // 1 return 0; }
- 2 回答
- 0 關(guān)注
- 1045 瀏覽
添加回答
舉報(bào)
0/150
提交
取消