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

Daniel Gehriger picture Daniel Gehriger · Oct 26, 2012 · Viewed 19.6k times · Source

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?

Answer

Rost picture Rost · Oct 26, 2012

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