Pointers to functions

Pointers to functions

A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions.

The type of a pointer to a function is based on both the return type and parameter types of the function.

A declaration of a pointer to a function must have the pointer name in parentheses. Function parameters have precedence over pointers in declarations, so parentheses are required to alter the precedence and declare a pointer to a function. Without them, the compiler interprets the declaration as a function that returns a pointer to a specified return type. For example:
int *f(int a);       /* function f returning an int*                        */
int (*g)(int a);     /* pointer g to a function returning an int            */

In the first declaration, f is interpreted as a function that takes an int as argument, and returns a pointer to an int. In the second declaration, g is interpreted as a pointer to a function that takes an int argument and that returns an int.

C++11
You can use a trailing return type in the declaration or definition of a pointer to a function. For example:
auto(*fp)()->int;
In this example, fp is a pointer to a function that returns int. You can rewrite the declaration of fp without using a trailing return type as int (*fp)(void). For more information on trailing return type, see Trailing return type (C++11).
C++11

References to functions

A reference to a function is an alias or an alternative name for a function. You must initialize all references to functions after they are defined. Once defined, a reference to a function cannot be reassigned. You can use references to call functions and to pass functions as arguments to other functions. For example:
int g();

// f is a reference to a function that has no parameters and returns int.
int bar(int(&f)()){
   
   // call function f that is passed as an argument.
   return f();
}

int x = bar(g);
C++11
You can also use a trailing return type in the declaration or definition of a reference to a function. In the following example, fp is a reference to a function that returns int. For more information on trailing return type, see Trailing return type (C++11).
auto(&fp)()->int;
C++11