c++函数指针解引用之后为什么不是函数了

新手上路,请多包涵

定义一个函数和指向该函数的指针,然后判断函数指针解引用之后是否为函数

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的类型,结果表明类型一致,但第一个输出为什么为0
已知道使用函数名调用函数时会被转化为函数函数指针
想不明白这里为什么不对
是模板匹配参数的规则造成的吗?is_function的部分实现:

template<typename>
    struct is_function
    : public false_type { };

  template<typename _Res, typename... _ArgTypes>
    struct is_function<_Res(_ArgTypes...)>
    : public true_type { };
阅读 3.1k
2 个回答

decltype(*t1)的结果不是函数,而是函数引用,这是因为*t1返回一个lvalue,对于lvalue,decltype返回引用类型。
也就是说,不是

void()

而是

void (&) ()

由于是引用,is_function自然不能生效。使用remove_reference去除这个引用即可。

#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;
}

因为指针本来就不是函数,所谓函数,在编译的时候,会写入对应的符号表,而指针把函数的所有信息都丢了,只是一个入口地址而已。如果std::is_function判断指针为一个函数,那么可以认为是一个bug,从语义还是逻辑上这个结果都是正确的。

推荐问题