> On Thu, Mar 12, 2009 at 4:56 PM, Robert Dewar wrote: >> Nathan Ridge wrote: >> >>> Why does gcc not give an error about this? >>> If I compile with "-Wall", it will give a WARNING saying >>> "control reaches end of non-void function". >>> However, shouldn't it be an ERROR to return nothing >>> from a function that's supposed to return something? >>> Does this not result in undefined behaviour? Why goes gcc allow it? >> >> Because the standard does not make this an error, you can't tell >> if anyone needs a result, perhaps function is always called in >> a void environment. >> >> A warning is all you can get, always pay attention to warnings! > > The standard does make it an error, in that if such a function > (meaning a function that unconditionally falls off the end, when > declared to return a value) is called _at all_ then undefined behavior > results. More general cases can't be established at compile time, of > course. > > To be conservative, a compiler would issue the error only at the call > site, with possibly a warning (as gcc does) for the definition. > > -- James
I get no error when I compile the following code, even though a call to the function does take place: class A {}; A function() { A result; } int main() { function(); return 0; } Regards, Nate. _________________________________________________________________ Share photos with friends on Windows Live Messenger http://go.microsoft.com/?linkid=9650734