Liste des Groupes | Revenir à cl c |
On 2024-11-22, Bart <bc@freeuk.com> wrote:You should only use "unreachable()" in places where it is /never/ actually reached - thus it is perfectly safe if you use it correctly. (I'm not sure of any features of any language that are safe to use /incorrectly/.)You also seem proud that in this example:Unreachable assertions are actually a bad trade if all you are looking
>
int F(int n) {
if (n==1) return 10;
if (n==2) return 20;
}
>
You can use 'unreachable()', a new C feature, to silence compiler
messages about running into the end of the function, something I
considered a complete hack.
for is to suppress a diagnostic. Because the behavior is undefined
if the unreachable is actually reached.
That's literally the semantic definition! "unreachable()" means,Yes. So that's fine, as long as execution never reaches it. That's the whole point - you are telling the compiler that this thing cannot happen. Compilers optimise all the time on the basis of what they know can and cannot happen - this just lets the programmer specify it.
roughly, "remove all definition of behavior from this spot in the
program".
Whereas falling off the end of an int-returning function onlyAll true - but so what?
becomes undefined if the caller obtains the return value,
and of course in the case of a void function, it's well-defined.
You are better off with:Asserts - or other temporary checks resulting in stopping the program with a useful message - can be very helpful in debugging. If you are not entirely sure that code execution can never reach a particular point, then either don't use "unreachable()" there, or if you prefer, put a conditional check there. "asset" is not magic - you can do the same thing yourself :
assert(0 && "should not be reached");
return 0;
if asserts are turned off with NDEBUG, the function does something that
is locally safe, and offers the possibility of avoiding a disaster.
The only valid reason for using unreachable is optimization: you're"unreachable()" is not unsafe unless you are using it incorrectly. /Everything/ is unsafe if you are using it incorrectly.
introducing something unsafe in order to get better machine code. When
the compiler is informed that the behavior is always undefined when some
code is reached, it can just delete that code and everything dominated
by it (reachable only through it).
The above function does not need a function return sequence to be
emitted for the fall-through case that is not expected to occur,
if the situation truly does not occur. Then if it does occur, hell
will break loose since control will fall through to whatever bytes
follow the abrupt end of the function.
Les messages affichés proviennent d'usenet.