Re: [C++] Omitted return statement

From: Sumit Rajan (sumitrajan_at_myrealbox.com)
Date: 04/29/04


Date: Thu, 29 Apr 2004 09:04:31 +0530


"Gary Labowitz" <glabowitz@comcast.net> wrote in message
news:3fqdndbgQoiizQ3dRVn-gg@comcast.com...
> One of my students overloaded the operator<< function for use on a
> UDT. His code did not include a return statement even though the
> header calls for an ostream& as the return type. The program works
> anyway for chained output. How can this be? If it's undefined
> behavior, are we just getting unlucky?
> Using g++ 3.2 on W2K box. (You may need to include cstdlib)

It is undefined behaviour according to 6.6.3/2:
"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."

So I guess that your student was just plain lucky.

Regards,
Sumit.



Relevant Pages

  • Re: [C++] Omitted return statement
    ... In message, Gary Labowitz ... >UDT. ... >header calls for an ostream& as the return type. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: [C++] Omitted return statement
    ... > UDT. ... > header calls for an ostream& as the return type. ... Pure unluck. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: [C++] Omitted return statement
    ... > UDT. ... > header calls for an ostream& as the return type. ... Issue all the warnings demanded by strict ISO C and ISO C++; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: unreferenced local variable
    ... The above header is non-standard. ... StrNam2 was not initialized. ... This is the far more dangerous problem. ...
    (comp.lang.cpp)