定义一个函数和指向该函数的指针,然后判断函数指针解引用之后是否为函数
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 { };
decltype(*t1)的结果不是函数,而是函数引用,这是因为*t1返回一个lvalue,对于lvalue,decltype返回引用类型。
也就是说,不是
而是
由于是引用,is_function自然不能生效。使用remove_reference去除这个引用即可。