On 12.12.2013 11:11, Juha Nieminen wrote:
> Mr Flibble <
fli...@i42.co.uk> a.k.a. Leigh Johnson wrote:
>> It is impossible in the general case
>> for a compiler to emit a diagnostic for missing return statements which
>> is why it is undefined behaviour instead.
>
[snip]
>
> What you probably meant was that it's in general impossible to prove
> that the execution of a subroutine will ever reach its last 'return'
> statement (if it contains previous such statements inside conditionals)
Rather, I think Leigh referred to the case where a non-void function
exits by execution reaching the end of the function body, so that the
return value is dynamically unspecified, which is Undefined Behavior.
The general case includes such code as
auto f()
-> Foo
{ if( fermatsLastTheoremIsTrue() ) { return Foo( 42 ); } }
which a compiler might find difficult to reason about.
The main problem -- for me, who is all I can speak for -- is that in
some cases the compiler *can* prove that execution will never reach a
return statement at the end. Then first without that statement it might
complain that a non-void function, like e.g. throw_exception (I just
call it "fail"), doesn't have a return statement, and then after such a
statement is added the compiler, too smart for the programmer's own
good, complains that the return statement will never be reached...
That's what C++11 "noreturn" attribute is about.
However, as I understand it and vaguely remember it, Visual C++ will
never get C++11 attribute support, but it does have a non-standard
extension (called "declspec") for declaring the same.
I just wrap this, and some other such features, in macros, like
CPPX_NORETURN, defined by a header file that's included via different
include paths for different compilers.
Cheers,
- Alf