Search code examples
c++c++11decltype

Can C++11 decltype be used to create a typedef for function pointer from an existing function?


Given

struct A { 
    int foo(double a, std::string& b) const;
};

I can create a member function pointer like this:

typedef int (A::*PFN_FOO)(double, std::string&) const;

Easy enough, except that PFN_FOO needs to be updated if A::foo's signature changes. Since C++11 introduces decltype, could it be used to automatically deduce the signature and create the typedef?


Solution

  • Yes, of course:

    typedef decltype(&A::foo) PFN_FOO;

    You can also define type alias via using keyword (Thanks to Matthieu M.):

    using PFN_FOO = decltype(&A::foo);