Where's the constructor?

Joe Buck jbuck@synopsys.com
Wed Feb 11 18:20:00 GMT 1998


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

> Then promote this warning into an error.  I expect a warning to mean
> that some kind of default or best-guess behavior was generated, when the
> programmer might have intended something else.  In this case, the
> compiler does nothing at all.  If it is too complicated an issue to
> construct a valid object, it's better to say the program is in error. 
> Does the standard allow you to call this an error?  Or does it require
> the compiler to generate code, no matter what?

The CD2 draft says (section [stmt.return]):

  A return statement with an expression can be used only in functions
  returning a value; the value of the expression is returned to the caller
  of the function.  If required, the expression is implicitly converted to
  the return type of the function in which it appears.  A return statement
  can involve the construction and copy of a temporary object.  Flowing
  off the end of a function is equivalent to a return with no value; this
  results in undefined behavior in a value-returning function.

As far as I know the final text does not change this.  So the standard
declares the effect of falling off the end undefined (so a call to abort
would be a legal implementation).

However, if a function appears to fall off the end but in fact does not
(because a condition is never true, or a function never returns), it would
be a well-formed program as best I can tell and we have to accept it
(section [intro.compliance]), which means warnings but no errors.  So, for
example,

String foo() {
       exit(1);
}

must be accepted.  I think that

String foo() {
}

could be a hard error, but it's probably better just to always make
warnings.

So why not just make default constructor calls, you say?  Well, they
might be wasted code in some cases (because the end cannot be reached
but g++'s flow analysis is not smart enough to know this), and there
may not be a default constructor.  It's not enough to show simple
code sequences, as the compiler needs rules that are good for every
sequence.










More information about the Gcc mailing list