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;
}
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?
(Code tested under g++4.7, g++4.8 and clang++3.2)
That is because decltype(*pmain)
yield a reference to a function type, for which std::function
is false
as intended. Try:
is_function<remove_reference<decltype(*pmain)>::type>::value
BTW: ISO C++ forbids to take the address of ::main()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With