Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 270
templatetypedef Avatar asked Mar 18 '11 19:03

templatetypedef


1 Answers

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.

like image 111
James McNellis Avatar answered Oct 03 '22 07:10

James McNellis