TITLE: return and void (Newsgroups: comp.lang.c++,comp.std.c++, 8 Oct 98) NARAN: sbnaran@KILL.uiuc.edu wrote: > > > Is something like this possible? I've seen in done in one of the > examples in Stroustrup III. It's very convenient. > > void f() { /*stuff*/ } > void g() { /*stuff*/ return f(); /*stuff*/ } HYSLOP: jim.hyslop@leitch.com First off, if Stroustrup uses it, that's a pretty strong indicatation that it's allowable syntax. However, even Stroustrup is human, and can make mistakes (did I actually say that :-O ) Anyway, MSVC5 gives an error, complaining "g: 'void' function returning a value". But we all know that one compiler's opinion does not necessarily indicate the truth, so let's see what the Standard says... [pause while I dig through the Standard] Section 3.9.1 (Fundamental Types) states "The void type has an empty set of values.... Any expression can be explicitly converted to type cv void (5.2).... An expression of type void shall be used only as ... the expression in a return statement (6.6.3) for a function with the return type void." OK, that suggests to me that "return void;" would be valid syntax. Let's see what MSVC says... nope, "error: type 'void' unexpected." How about "return (void) f();"... nope, back to the original error. Let's look at 6.6.3 to see if it gives any clues. Ah, here we go - clause 3: "A return statement with an expression of type 'cv void' can be used only in functions with a return type of cv void; the expression is evaluated just before the function returns to its caller". Well, as I understand it, "f()" is an expression of type 'cv void', so "return f();" should be valid syntax. OK, now what do you real experts say to this? Is my analysis correct? CLAMAGE: stephen.clamage@sun.com (Steve Clamage) It's a recent addition to the standard. It was formerly forbidden. The reason for the change was not to write code like the above, but to make it easier to write templates. Without the relaxed rule, you would have to write a special version of templates for instantiation on type void.