Why is std :: is_function evaluating to false when using the dereference function pointer?

I am trying to use std::is_function to determine if a variable is a function pointer.

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

output

 PFivE 0 PFivE 0 FivE 1 FivE 0 

Can someone explain with understanding why the last std::is_function evaluates to false?

(Code checked under g ++ 4.7, g ++ 4.8 and clang ++ 3.2)

+4
source share
1 answer

This is because decltype(*pmain) provides a reference to the type of function for which std::function false , as intended. Try:

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

BTW: ISO C ++ prohibits accepting address :: main ()

+12
source

Source: https://habr.com/ru/post/1500352/


All Articles