This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Compiler didn't error on function with no return statement


On 21 June 2013 15:11, Andy Falanga (afalanga) wrote:
>>
>> <http://stackoverflow.com/questions/5655181/how-to-turn-on-gcc-
>> warnings-for-a-forgotten-return-statement>
>>
>> Failing to provide a return value is legal C, but undefined behaviour.
>> So gcc has to accept it.
>>
>> You only get warnings if you enable them.  Normally, you will want your
>> code to be warning-free when compiled with "-Wall".  Many people use "-
>> Wextra" as well - I personally enable many of the other warning flags
>> as well.
>>
>
> David,
>
> Thanks for the explanation.  I figured it had to be something like this.  It's rather humorous that it's legal but undefined.  It would seem that anything "legal" must, by requirement, have a definition.  One thing I don't quite understand about this behavior of gcc, as I understand the function of gcc it calls the appropriate compiler based on file name suffix.  Since this isn't legal C++, why isn't a warning or error generated by the C++ compiler of gcc?  Am I misunderstanding something about gcc's operation?

Who said it's not legal C++?

And you *do* get a warning with -Wreturn-type, but you don't get an
error because that would reject legal programs:

void f();

bool g()
{
  f();
}

Is this an error?  What if f() never returns?

In C++11 we could mark f() with [[noreturn]] (like GCC's
__attribute((noreturn))) but just because a function isn't marked
noreturn doesn't mean it does return.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]