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?
The decltype type specifier yields the type of a specified expression. The decltype type specifier, together with the auto keyword, is useful primarily to developers who write template libraries. Use auto and decltype to declare a template function whose return type depends on the types of its template arguments.
decltype returnsIf what we pass to decltype is the name of a variable (e.g. decltype(x) above) or function or denotes a member of an object ( decltype x.i ), then the result is the type of whatever this refers to. As the example of decltype(y) above shows, this includes reference, const and volatile specifiers.
We declare the function pointer, i.e., void (*ptr)(char*). The statement ptr=printname means that we are assigning the address of printname() function to ptr. Now, we can call the printname() function by using the statement ptr(s).
We can use typedef to simplify the usage of function pointers. Imagine we have some functions, all having the same signature, that use their argument to print out something in different ways: Now we can use a typedef to create a named function pointer type called printer:
Although using typedef names for pointer to function types makes life easier, it can also lead to confusion for others who will maintain your code later on, so use with caution and proper documentation. See also Function Pointers.
To use it we create a variable of the created type and assign it a pointer to one of the functions in question: Then to call the function pointed to by the function pointer variable: Thus the typedef allows a simpler syntax when dealing with function pointers.
Now we can use a typedef to create a named function pointer type called printer: typedef void (*printer_t) (int); This creates a type, named printer_t for a pointer to a function that takes a single int argument and returns nothing, which matches the signature of the functions we have above.
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);
One issue: you may only deduce the type of a variable if this variable is unambiguous.
The main issue with functions is that overloads mean that their names alone are insufficient to identify them. Therefore using decltype
fails should you ever introduce an overload of foo
in A
.
struct A { void foo() const; void foo(int) const; }; using PFN_FOO = decltype(A::foo);
source.cpp:6:36: error: decltype cannot resolve address of overloaded function
Not sure you'll be gaining much thus...
On the other hand, you can actually use an alias and check that alias is right:
struct A { void foo() const; void foo(int) const; }; using PFN_FOO = void (A::*)(int) const; static_assert(std::is_same<PFN_FOO, decltype(static_cast<PFN_FOO>(&A::foo))>::value, "Ooops, need to update signature of PFN_FOO!");
Note: not sure this is the best way to test, basically all you need is the static_cast
part, I just wanted to stash an error message alongside. You would probably need something like SFINAE to get better messages though.
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