Syntax of an un-named function pointer in C++

  • A+
Category:Languages

I was looking into most vexing parse, and I stumbled upon something like this:

Foo bar(Baz()); // bar is a function that takes a pointer to a function that returns a Baz and returns a Foo 

This is quite different from the typical syntax of return-type(*name)(parameters). Are the parenthesis present the parenthesis for the parameter list, or are they for the name?

 


Fully explicit form:

Foo bar(Baz f()); 

bar is a function that takes a single parameter f, which is a function (taking no arguments) returning Baz.

Without naming the parameter:

Foo bar(Baz ()); 

The reason bar ends up taking a pointer to a function is that functions cannot be passed by value, so declaring a parameter as a function automatically decays it into a pointer. The above declaration is equivalent to:

Foo bar(Baz (*)());  // or: Foo bar(Baz (*f)());  // with a named parameter 

This is similar to void foo(int [10]) where int [10] also means int * in a parameter list.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: