A typedef, or a function-type alias, helps to define pointers to executable code within memory. Simply put, a typedef can be used as a pointer that references a function.
Function Pointer Syntaxvoid (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.
You can declare any type with typedef , including pointer, function, and array types. You can declare a typedef name for a pointer to a structure or union type before you define the structure or union type, as long as the definition has the same visibility as the declaration.
It has a similar syntax, except you remove the identifier from the pointer:
using FunctionPtr = void (*)();
Here is an Example
If you want to "take away the uglyness", try what Xeo suggested:
#include <type_traits>
using FunctionPtr = std::add_pointer<void()>::type;
And here is another demo.
The "ugliness" can also be taken away if you avoid typedef-ing a pointer:
void f() {}
using Function_t = void();
Function_t* ptr = f;
ptr();
http://ideone.com/e1XuYc
You want a type-id
, which is essentially exactly the same as a declaration except you delete the declarator-id
. The declarator-id
is usually an identifier, and the name you are declaring in the equivilant declaration.
For example:
int x
The declarator-id
is x
so just remove it:
int
Likewise:
int x[10]
Remove the x
:
int[10]
For your example:
void (*FunctionPtr)()
Here the declarator-id
is FunctionPtr
. so just remove it to get the type-id
:
void (*)()
This works because given a type-id
you can always determine uniquely where the identifier would go to create a declaration. From 8.1.1 in the standard:
It is possible to identify uniquely the location in the [type-id] where the identifier would appear if the construction were a [declaration]. The named type is then the same as the type of the hypothetical identifier.
How about this syntax for clarity? (Note double parenthesis)
void func();
using FunctionPtr = decltype((func));
Another approach might using auto return type with trailing return type.
using FunctionPtr = auto (*)(int*) -> void;
This has the arguable advantage of being able to tell something is a function ptr when the alias begins with "auto(*)" and it's not obfuscated by the identifier names.
Compare
typedef someStructureWithAWeirdName& (FunctionPtr*)(type1*, type2**, type3<type4&>);
with
using FunctionPtr = auto (*)(type1*, type2**, type3<type4&>) -> someStructureWithAWeirdName&;
Disclaimer: I took this from Bean Deane's "Easing into Modern C++" talk
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