鍍金池/ 問答/C++/ c++函數(shù)指針解引用之后為什么不是函數(shù)了

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

定義一個(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;
}

最后輸出為:

0
1
1

直接比較*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 { };
回答
編輯回答
空白格

decltype(*t1)的結(jié)果不是函數(shù),而是函數(shù)引用,這是因?yàn)?t1返回一個(gè)lvalue,對(duì)于lvalue,decltype返回引用類型。
也就是說,不是

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;
}
2017年3月16日 10:23
編輯回答
吢涼

因?yàn)橹羔槺緛砭筒皇呛瘮?shù),所謂函數(shù),在編譯的時(shí)候,會(huì)寫入對(duì)應(yīng)的符號(hào)表,而指針把函數(shù)的所有信息都丟了,只是一個(gè)入口地址而已。如果std::is_function判斷指針為一個(gè)函數(shù),那么可以認(rèn)為是一個(gè)bug,從語義還是邏輯上這個(gè)結(jié)果都是正確的。

2018年1月10日 13:41