Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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)

like image 968
user1492625 Avatar asked Dec 15 '22 06:12

user1492625


1 Answers

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()

like image 107
Daniel Frey Avatar answered Dec 17 '22 19:12

Daniel Frey