How do I typedef a function pointer with the C++11 using syntax?
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.
How do I typedef a method pointer with the C++11 using syntax?
using MethodPtr = void (A::*)();
Quite similar to a function pointer, in fact.
how to understand using typedef to define a function pointer?
Let's start by talking about declaration syntax in general (I'll be using C terminology, although C++ is largely similar). In both C and C++, a declaration contains a sequence of one or more declaration specifiers followed by a comma-separated list of zero or more declarators.
Declaration specifiers include type specifiers (int
, double
, char
, unsigned
, etc.), type qualifiers (const
, volatile
, etc.), storage class specifiers (static
, register
, typedef
, etc.), struct
and union
specifiers, and a few other things we won't get into here.
Declarators include the name of the thing being declared, along with information about that thing's pointer-ness, array-ness, or function-ness (in C++ you also have reference-ness).
When you declare a function, such as
void foo( int, double );
void
is the declaration specifier (type specifier), and foo( int, double )
is the declarator. The type of foo
is fully specified by the combination of the declaration specifier and declarator:
foo -- foo
foo( ) -- is a function taking
foo( ) -- unnamed parameter
foo( int ) -- is an int
foo( int, ) -- unnamed parameter
foo( int, double ) -- is a double
void foo( int, double ) -- returning void
In plain English, the type of foo
is "function taking an int
and double
parameter and returning void
."
You can declare pointers to functions as well:
fptr -- fptr
(*fptr) -- is a pointer to
(*fptr)( ) -- function taking
(*fptr)( ) -- unnamed parameter
(*fptr)( int ) -- is an int
(*fptr)( int, ) -- unnamed parameter
(*fptr)( int, double ) -- is a double
void (*fptr)( int, double ) -- returning void
Again, the sole declaration specifier is void
, and the declarator is (*fptr)( int, double )
.
For syntactic purposes, typedef
is grouped with the storage class specifiers (static
, auto
, register
), but it doesn't behave like other storage class specifiers - instead of affecting the storage or visibility of the thing being declared, it makes the identifier in the declarator an alias for the type. If we stick typedef
on the front of the above declaration:
typedef void (*fptr)( int, double );
then it reads as
fptr -- fptr
typedef fptr -- IS AN ALIAS FOR THE TYPE
typedef (*fptr) -- pointer to
typedef (*fptr)( ) -- function taking
typedef (*fptr)( ) -- unnamed parameter
typedef (*fptr)( int ) -- is an int
typedef (*fptr)( int, ) -- unnamed parameter
typedef (*fptr)( int, double ) -- is a double
typedef void (*fptr)( int, double ) -- returning void
IOW, fptr
is an alias (typedef
name) for the type "pointer to function taking an int
and double
parameter and returning void
", and you can use it to declare pointer objects of that type:
fptr fp1, fp2;
You can do the same thing with other pointer types1:
typedef int *intp; // intp is an alias for the type "pointer to int";
typedef double (*arr)[10]; // arr is an alias for the type "pointer to 10-element array of double"
Declarators can get pretty complex. You can have pointers to functions:
T (*ptr)();
pointers to arrays:
T (*ptr)[N];
arrays of pointers to functions:
T (*ptr[N])();
functions returning pointers to arrays:
T (*foo())[N];
arrays of pointers to functions returning pointers to arrays:
T (*(*arr[N])())[M];
and on and on and on, and sticking typedef
in front of any of them will work:
typedef T (*(*arr[N])())[M];
means arr
is an alias for the type "N-element array of pointers to functions returning pointers to M-element arrays of T
".
- As a rule, you do not want to hide pointers to scalar types behind typedefs unless you can guarantee that the programmer using that type will never have to be aware of the underlying pointer-ness of that type (i.e., will never have to explicitly dereference it with
*
or try to print its value with%p
or anything like that).
Typedef function pointer?
typedef
is a language construct that associates a name to a type.
You use it the same way you would use the original type, for instance
typedef int myinteger;
typedef char *mystring;
typedef void (*myfunc)();
using them like
myinteger i; // is equivalent to int i;
mystring s; // is the same as char *s;
myfunc f; // compile equally as void (*f)();
As you can see, you could just replace the typedefed name with its definition given above.
The difficulty lies in the pointer to functions syntax and readability in C and C++, and the typedef
can improve the readability of such declarations. However, the syntax is appropriate, since functions - unlike other simpler types - may have a return value and parameters, thus the sometimes lengthy and complex declaration of a pointer to function.
The readability may start to be really tricky with pointers to functions arrays, and some other even more indirect flavors.
To answer your three questions
Why is typedef used?
To ease the reading of the code - especially for pointers to functions, or structure names.The syntax looks odd (in the pointer to function declaration)
That syntax is not obvious to read, at least when beginning. Using atypedef
declaration instead eases the readingIs a function pointer created to store the memory address of a function?
Yes, a function pointer stores the address of a function. This has nothing to do with thetypedef
construct which only ease the writing/reading of a program ; the compiler just expands the typedef definition before compiling the actual code.
Example:
typedef int (*t_somefunc)(int,int);
int product(int u, int v) {
return u*v;
}
t_somefunc afunc = &product;
...
int x2 = (*afunc)(123, 456); // call product() to calculate 123*456
How to define function type with typedef?
The alias name should be placed where a variable name would be if it was a variable declaration.
Here:
typedef int (*order) (void *, void *);
// ^~~~~
Or, if you want a function type rather than a pointer-to-function:
typedef int order(void *, void *);
Trouble understanding a typedef along function pointers
It defines the DllEntryProc
data type as an alias for BOOL, where DllEntryProc is a pointer to a function that takes one HINSTANCE, one DWORD and one LPVOID as arguments and returns a WINAPI BOOL
typedef BOOL (WINAPI *DllEntryProc)(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved);
DllEntryProc
is a new type like int
and you can declare variables of this type just like you can declare variables of type int
.
DllEntryProc somevar;
Now the value that you can assign to somevar
should be of type DllEntryProc
which is a pointer to a function of the said type.
How to create a typedef for function pointers
Your question isn't clear, but I think you might want something like this:
int foo(int i){ return i + 1;}
typedef int (*g)(int); // Declare typedef
g func = &foo; // Define function-pointer variable, and initialise
int hvar = func(3); // Call function through pointer
c++11 typedef function pointer with trailing return type
The trailing return type syntax is new in C++11, and it is valid wherever you can write a function type. auto <function>(<parameters>) -> <result>
is simply a fancy way of writing <result> <function>(<parameters>)
(with the benefit that <result>
can refer to <parameters>
).
This means typedef auto Kernel (int, int) -> int;
is perfectly valid, and means exactly the same thing as typedef int Kernel (int, int);
. That Kernel
typedef can then be be used in declarations of functions, or of pointers to functions.
Related Topics
How to Make Cout Behave as in Binary Mode
What Is the Vtable Layout and Vtable Pointer Location in C++ Objects in Gcc 3.X and 4.X
Why Doesn't C++ Move Construct Rvalue References by Default
Sizeof(Struct) Returns Unexpected Value
C++ Template Parameter Type Inference
Dijkstra Shortest Path with Vertexlist = Lists in Boost Graph
Can You Resize a C++ Array After Initialization
2D Diamond (Isometric) Map Editor - Textures Extended Infinitely
How to Solve -------Undefined Reference to '_Chkstk_Ms'-------On Mingw
How to Install Feature Based on the Property Set in Custom Action
How Performing Multiple Matrix Multiplications in Cuda
Undefined Reference to 'Stdscr' While Using Ncurses
What Are Good Practices Regarding Shared Libraries on Linux
Is Pointer Comparison Undefined or Unspecified Behavior in C++