Sujet : Re: else ladders practice
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 22. Nov 2024, 19:10:50
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20241122095231.679@kylheku.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-11-22, Bart <
bc@freeuk.com> wrote:
You also seem proud that in this example:
>
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.
Unreachable assertions are actually a bad trade if all you are looking
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,
roughly, "remove all definition of behavior from this spot in the
program".
Whereas falling off the end of an int-returning function only
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:
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
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.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca