Home >Backend Development >C++ >Is `widget w(gadget(), doodad());` a Variable Declaration or a Function Declaration in C ?
C 's Most Vexing Parse: Unveiling the Mystery of Function Declarations
While understanding the syntax of widget w(); is straightforward, many programmers struggle to comprehend how the following code fragment represents a function declaration:
widget w( gadget(), doodad() ); // pitfall: not a variable declaration
To unravel this mystery, it's crucial to delve into the decay rules of C . Within function declarations, arguments of array types degenerate into pointers pointing to the first element, while arguments of function types convert into function pointers.
Applying this principle, the aforementioned code transmutes into the following signature:
widget w( gadget(*)(), doodad(*)() );
This indicates a function that accepts two arguments: a pointer to a function that takes no arguments and returns a gadget type, and a pointer to a function that also takes zero arguments and returns a doodad type. The function itself returns a widget.
The complexities don't end there. Consider this perplexing example:
widget w(gadget(x));
How can this possibly be a function declaration given that x is a variable?
In C , it's permissible to include additional parentheses when declaring variables, such as gadget x; and gadget (x);. This applies to function arguments as well, allowing the above code to be interpreted as a function declaration that takes one argument named x of type gadget and returns a widget.
The above is the detailed content of Is `widget w(gadget(), doodad());` a Variable Declaration or a Function Declaration in C ?. For more information, please follow other related articles on the PHP Chinese website!