void main returning int - why compiles?

bearophile bearophileHUGS at lycos.com
Sat Jan 1 13:47:00 PST 2011


Simen kjaeraas:

> One would expect that to work. If void functions did not allow returning
> the results of functions, the above function would have had to be changed
> to something like this:
> 
> ReturnType!Fn wrap( alias Fn )( ParameterTypeTuple!Fn args ) {
>      static if ( is( typeof( return ) == void ) ) {
>          Fn( args );
>      } else {
>          return Fn( args );
>      }
> }

I think you are talking about the foo() case. But the OP is talking about the bar() case, that I think it's a compiler bug:

void foo() {}
int bar() { return 1; }
void main()   {
    //return 0; // error
    //return foo(); // OK, foo returns void
    return bar(); // error, bar returns int
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list