templatetypedef
templatetypedef

Reputation: 372784

What does it mean to pass a parameter of function type in C++?

A while back I found that it's possible to write a C++ function that takes a parameter of function type (not function pointer type!). For example here's a function that takes a callback function that accepts and returns a double:

void MyFunction(double function(double));

My question is what it means to have a variable of function type, since you can't declare one in any other context. Semantically, how is it different from a function pointer or reference to a function? Is there an important difference between function pointers and variables of function type that I should be aware of?

Upvotes: 8

Views: 519

Answers (1)

James McNellis
James McNellis

Reputation: 355049

Just like void f(int x[]) is the same as void f(int* x), the following two are identical:

void MyFunction(double function(double)); 
void MyFunction(double (*function)(double)); 

Or, in official language (C++03 8.3.5/3), when determining the type of a function,

After determining the type of each parameter, any parameter of type "array of T" or "function returning T" is adjusted to be "pointer to T" or "pointer to function returning T," respectively.

Upvotes: 13

Related Questions