简体   繁体   English

在使用解除引用的函数指针时,为什么std :: is_function计算为false?

[英]Why does std::is_function evaluate to false when using on a dereferenced function pointer?

I am trying to use std::is_function to determine if a variable is a function pointer. 我试图使用std::is_function来确定变量是否是函数指针。

When running the following code 运行以下代码时

#include <iostream>
#include <typeinfo>

using namespace std;

int main() {
    typedef int(*functionpointer)();

    functionpointer pmain = main;

    cout << typeid(functionpointer).name() << " "<< is_function<functionpointer>::value << endl;
    cout << typeid(decltype(pmain)).name() << " " << is_function<decltype(pmain)>::value << endl;

    cout << typeid(decltype(main)).name() << " " << is_function<decltype(main)>::value << endl;
    cout << typeid(decltype(*pmain)).name() << " " << is_function<decltype(*pmain)>::value << endl;

    return 0;
}

the output is 输出是

PFivE 0
PFivE 0
FivE 1
FivE 0

Can anybody with insight explain why the last expression of std::is_function evaluates to false? 任何有洞察力的人std::is_function解释为什么std::is_function的最后一个表达式求值为false吗?

(Code tested under g++4.7, g++4.8 and clang++3.2) (在g ++ 4.7,g ++ 4.8和clang ++ 3.2下测试的代码)

That is because decltype(*pmain) yield a reference to a function type, for which std::function is false as intended. 这是因为decltype(*pmain)产生对函数类型的引用 ,对于该函数类型, std::functionfalse Try: 尝试:

is_function<remove_reference<decltype(*pmain)>::type>::value

BTW: ISO C++ forbids to take the address of ::main() BTW:ISO C ++禁止获取:: main()的地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM