On Tue, 29 Sep 2009 15:43:55 +0400, Jason House
<jason.james.ho...@gmail.com> wrote:
bearophile Wrote:
Don:
> In the docs for "return statement" in statement.html it says:
> ---
> ReturnStatement:
> return;
> return Expression ;
>
> Expression is allowed even if the function specifies a void return
type.
> The Expression will be evaluated, but nothing will be returned.
I agree that making the language a little more strict in such regards
is better.
> This makes sense for things we definitely want to allow, such as:
> void foo() { return bar(); }
> where bar() is another void function.
If bar() is a void function then "return bar();" looks like a bug. Why
do you want to allow such semantically wrong situation?
Bye,
bearophile
I believe the argument is to allow templated code to gracefully handle a
void return type without special cases. It doesn't quite work since void
is not a valid type for intermediate variables (leads to increased code
duplication)
Unfortunately, you still have to special-case void type in a lot of cases.
Here are just 2 examples:
RetType doSomething(Args, RetType)(Args args, RetType retType)
{
RetType result = callSomeDelegate(args); // doesn't work for 'void'
addLogEntry("bla-bla-bla");
return result;
}
class Test(T)
{
T foo(T t) { ... }
T bar() { ... }
T test()
{
return bar(foo()); // works for all types but 'void'
}
}