C++ 为什么在解引用函数指针上使用std::is_函数时计算为false?
我试图使用C++ 为什么在解引用函数指针上使用std::is_函数时计算为false?,c++,c++11,function-pointers,typetraits,C++,C++11,Function Pointers,Typetraits,我试图使用std::is_function来确定变量是否是函数指针 当运行以下代码时 #include <iostream> #include <typeinfo> using namespace std; int main() { typedef int(*functionpointer)(); functionpointer pmain = main; cout << typeid(functionpointer).name
std::is_function
来确定变量是否是函数指针
当运行以下代码时
#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;
}
有见识的人能解释一下为什么std::is_function
的最后一个表达式的计算结果为false吗
(在g++4.7、g++4.8和clang++3.2下测试的代码)这是因为
decltype(*pmain)
产生了对函数类型的引用,对于该函数类型,std::function
是false
。尝试:
is_function<remove_reference<decltype(*pmain)>::type>::value
是函数::值
is_function<remove_reference<decltype(*pmain)>::type>::value