Where's the constructor?

Joe Buck jbuck@synopsys.com
Wed Feb 11 10:25:00 GMT 1998


> Here's an interesting situation:
> 
> I have a function:
> 
> String
> foo()
> {
> 	String bar = "la";
> }

This is a broken function.

> A compilation of this function produces a warning since there is no
> return statement and the function is non-void.

Correct.  In this particular case (no return statement at all), an
error might be appropriate.

> This is fine  if the default constructor for the returned
> string were called, but this does not seem to be the case.
> As a result, the contents of the returned String will be garbage.

Correct.

> Surely in this case the default constructor for String should be called
> in the absence of the return statement?

If we were to make a change to the C++ front end, I would prefer
generating a call to abort() for functions of this type if they fall off
the end (or for cases where we cannot tell whether we can fall off the
end).  The behavior is undefined, and it's better to alert the programmer
that s/he has written broken code.




More information about the Gcc mailing list