You said it shouldn't be an error according to the standard.
If the compiler can detect at compile-time that the code has
undefined behavior, does the standard require the compiler to
compile it regardless? Does "compiler outputs an error" not
fall into the domain of acceptable implementations of
undefined behavior?
Undefined behavior is, well, undefined. I'm pretty sure, in
fact, that it can have retroactive effects: data you wrote to
disk (and flushed) before the undefined behavior occurs may not
be there. And I'm pretty sure that the compiler is allowed to
refuse to compile the code. (It's always allowed to output an
error message, even for perfectly legal code.)
The problem here is that the undefined behavior only occurs *if*
the function is called. So the compiler must compile the code
unless it can prove that the code will in fact be executed, for
all possible input. When using separate compilation, it
generally means that a compiler error cannot occur until
linking, when the compiler has access to the entire program.
In the case of Sun CC, at least, it is very definitly an error
in the compiler; I have code something like:
MyClass
f( int i )
{
switch ( i ) {
case 0 :
// with a return at each case...
default :
programStatus.setError( ProgramStatus::fatal )
<< "Error message" ;
}
}
Sun CC fails to compile this (I think---I'm at home, and not on
my Sparc at the moment, but if it compiles this, then it fails
with something similar). In my framework, however, calling
ProgramStatus::setError with an error severity (the argument) of
fatal guarantees that you won't return. (In fact, the
destructor of the returned stream wrapper will ensure that you
don't go any further---either calling exit or abort, or raising
an exception.)
Now, given the complexities of the statement (dependent on the
argument and a user defined callback, code in a different
translation unit, etc.), I don't think it reasonable for the
compiler to detect that I can't possibly fall off the end of the
function. But if can't prove that I might; in fact, I can't.
So the compiler should accept the code.