void main returning int - why compiles?
Jonathan M Davis
jmdavisProg at gmx.com
Sat Jan 1 00:32:17 PST 2011
On Friday 31 December 2010 23:37:17 Daren Scot Wilson wrote:
> I'm wondering why the following compiles. I'm using LDC. Perhaps it's a
> bug, or there's some subtlety about D. I have deliberately, out of a
> combination of idleness and desire for mischief, have main() declared as
> returning void, but with a return statement giving an integer.
>
> If the first "half evil" return statement is uncommented, the corruption is
> noticed by the compiler and it writes an error.
>
> 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.)
>
> When I get religion and like good boy declare main() as returing int, it
> compiles in perfectly. When executed, the program returns either number
> according to which return statement is uncommented.
>
>
>
>
> int foo(int x) {
> return x;
> }
>
> void main() {
> // return 333; /* half evil */
> return foo(666); /* total evil */
> }
I don't know what LDC's current state is in terms of being up-to-date with the
latest D. However, it is _not_ legal D to return a value from a void function.
So, this is definitely a bug in LDC.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list