Home >Backend Development >C++ >Is `widget w(gadget(), doodad());` a Variable Declaration in C ?

Is `widget w(gadget(), doodad());` a Variable Declaration in C ?

Linda Hamilton
Linda HamiltonOriginal
2024-11-19 07:53:02931browse

Is `widget w(gadget(), doodad());` a Variable Declaration in C  ?

C 's Most Vexing Parse Resolved

The issue arises with code like the following:

widget w(gadget(), doodad());  // Pitfall: Not a Variable Declaration

One might assume this code is declaring a variable named w of type widget. However, this is not the case.

In C , arguments of type array decay into pointers to the first element, and arguments of type function decay into a function pointer. This means the signature of the function being declared is:

widget w(gadget(*)(), doodad(*)());

This function takes two arguments: one is a pointer to a function taking no arguments and returning a gadget, and the other is a pointer to a function taking no arguments and returning a doodad. The function itself returns a widget.

Even more confusing cases arise when extra parentheses are added to function arguments, as in:

widget w(gadget(x));

This looks like it should declare a variable named x of type gadget, but it actually declares a function that takes a first 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 in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn