|
![](/i/fill.gif) |
Am 11.05.2011 22:17, schrieb Darren New:
> On 5/11/2011 12:57, Warp wrote:
>> compiles and runs just fine.
>
> Huh. I wonder if that's just a GCC thing or whether that's actually in
> the standard. I just *actually* tried it with C# and I get "since XYZ
> returns void, the return statement may not be followed by an expression."
The C language standard is pretty clear about this; quoting from
ISO/IEC 9899:TC2:
"6.8.6.4 The return statement
Constraints
1 A return statement with an expression shall not appear in a function
whose return type is void. [...]"
The C++ standard, however, explicitly allows for such a construction;
quoting from some 2005 working draft:
"6.6.3 The return statement [stmt.return]
[...]
3 A return statement with an expression of type “cv void” can be used
only in functions with a return type of cv void; [...]."
("cv" being shorthand for "an arbitrary set of cv-qualifiers, i.e., one
of {const }, {volatile }, {const, volatile}, or the empty set")
Not of Java though; quoting from "The Java(TM) Language Specification,
Third Edition":
"8.4.7 Method Body
[...]
If a method is declared void, then its body must not contain any return
statement (§14.17) that has an Expression."
So it appears that C++ is about the only member of the C-style language
family allowing for such a construct per specification.
Post a reply to this message
|
![](/i/fill.gif) |