[ILUG] gcc optimisation weirdness?

Kenn Humborg kenn at bluetree.ie
Thu May 11 11:31:41 IST 2000


> I'd be very interested if anyone could test the following code
> for me using gcc. 
> // ===================================
> // AFAIK this shouldn't compile but VC++ thinks this 
> // is just peachy. 
> 
> struct PreludeToFailure(
>     PreludeToFailure( int stuff ) : _internalStuff( stuff ) { }
> 
>     int _internalStuff;
> }
> 
> main ()
> {
> 
>  // do some stuff here
> // ...
>  return 0;
> 
> }
> 
> // ===================================
> 
> This compiles under VC++ event with the strict ANSI compliance
> turned on ( -Za option for the MSVC compiler )
> despite ANSI C requiring   an int return type from main.
> The program crashes - no shit - but still compiles as
> the struct takes an int in the constructor. 

Defining types in the return value of a function is forbidden
by the C++ standard.  This months Windows Developers Journal
covers exactly this issue in its Bug++ Of The Month column.

There are a couple of rules being broken, so the compiler
should complain:

o  main() not returning int.
o  main() not taking (int, char **)
o  type defined in function return type specifier

I haven't tried your snippet with GCC, but I'd bet that it will
complain.  If you try to do this in C under GCC:

int func(struct unknown *p);

GCC says that the scope of struct unknown is limited to the 
argument list of func().  So they probably have checks for scopes
that are limited because the type is only defined as a return value.

Later,
Kenn







More information about the ILUG mailing list