第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

c++函數(shù)指針解引用之后為什么不是函數(shù)了

c++函數(shù)指針解引用之后為什么不是函數(shù)了

C++
桃花長(zhǎng)相依 2018-09-03 07:49:47
定義一個(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的類(lèi)型,結(jié)果表明類(lèi)型一致,但第一個(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 { };
查看完整描述

1 回答

?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

decltype(*t1)的結(jié)果不是函數(shù),而是函數(shù)引用,這是因?yàn)?t1返回一個(gè)lvalue,對(duì)于lvalue,decltype返回引用類(lèi)型。
也就是說(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;
}


查看完整回答
反對(duì) 回復(fù) 2018-09-03
  • 1 回答
  • 0 關(guān)注
  • 534 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)