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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 21 June 2013 15:40, Andy Falanga (afalanga) wrote:
>>
>> Who said it's not legal C++?
>
> I inferred that.  Obviously, I shouldn't have.  David said that it was legal C and so I made the jump into 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:
>
> My understanding was in error.  That is why I thought that I should get a warning message from the C++ compiler (without the extra step of an option to the compiler).
>
>>
>> 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.
>
> True, but that does seem to be rather poor design (I know you're giving me an example).

Of course it's a trivial example, I assumed you could extrapolate to
something more realistic:

bool g()
{
  if (something())
    return something_else();
  abort();
}

If the C library doesn't mark abort() as noreturn then the function
above has a missing return, but can't be reached, so it would be wrong
to reject the code.


>  Probably, the language supports this because of some need which I've not run into and am unaware of its merits.

The language standard is not in the business of criticising your
design, code like the above exists, it is valid according to the
current standard, giving an error would be non-conforming, end of
story.

If you decide want to reject such code then use -Werror=return-type.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux