Taken directly from http://herbsutter.com/2013/05/09/gotw-1-solution/
While widget w();
is clear for me, I have no idea how can the below code be a function declaration?
// same problem (gadget and doodad are types)
//
widget w( gadget(), doodad() ); // pitfall: not a variable declaration
How is this possible?
In a function declaration, arguments of type array decay into pointers to the first element, arguments of type function decay into a function pointer, so the signature would be:
That is, a function that takes as the first argument a pointer to a function taking no arguments and returning
gadget
, that takes as second argument a pointer to a function taking no arguments and returning adoodad
and that the function itself returns awidget
There are even more interesting or confusing cases, like:
How could that be interpreted as a function declaration? I mean,
x
is a variable, right? Well, when declaring a variable you can add extra parenthesis, sogadget x;
andgadget (x);
both declare the same variablex
. The same applies to function arguments so the code above looks like a declaration of a function that takes a first argument namedx
of typegadget
and returns awidget
...It's function that gets two functions, that returns
gadget
anddoodad
and either of them gets no arguments.Example that compiles fine.
http://ideone.com/YjZK9Y