void main returning int - why compiles?

Simen kjaeraas simen.kjaras at gmail.com
Sat Jan 1 13:08:36 PST 2011


Daren Scot Wilson <darenw at darenscotwilson.com> wrote:

> As shown, the "total evil" return statement gets a value from subroutine  
> foo().  Being somehow so perfect in its evilness, this passes through  
> the compiler without a burp.  The resulting executable returns zero (or  
> my bash shell defaults to zero when receiving nothing.)

This is by design, the feature is made for generic functions. Consider:

ReturnType!Fn wrap( alias Fn )( ParameterTypeTuple!Fn args ) {
     return Fn( args );
}

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 );
     }
}

Clearly this code is worse than the above.

-- 
Simen


More information about the Digitalmars-d-learn mailing list