Sujet : Re: "A diagram of C23 basic types"
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 04. Apr 2025, 03:43:42
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vsnh11$28q4m$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 03.04.2025 13:07,
Muttley@DastardlyHQ.org wrote:
On Thu, 3 Apr 2025 11:41:31 +0200
David Brown <david.brown@hesbynett.no> wibbled:
[ "unreachable()" is now standard. ]
I can't tell you what Scott uses it for, but I have used gcc's
__builtin_unreachable() a fair number of times in my coding. I use it
to inform both the compiler and human readers that a path is unreachable:
What for? The compiler doesn't care and a human reader would probably
prefer a meaningful comment if its not obvious. If you're worried about the
code accidently going there use an assert.
switch (x) {
case 1 : ...
case 2 : ...
case 3 : ...
default : __builtin_unreachable();
}
>
I can also use it to inform the compiler about data :
>
if ((x < 0) || (x > 10)) __builtin_unreachable();
// x must be 1 .. 10
And that'll do what? You want the compiler to compile in a hidden value check?
I also don't see a point here; myself I'd write some sort of assertion
in such cases, depending on the application case either just temporary
for tests or a static one with sensible handling of the case.
Good use of __builtin_unreachable() can result in smaller and faster
code, and possibly improved static error checking. It is related to the
Sorry, don't see how. If you think a piece of code is unreachable then don't
put it in in the first place!
Let me give that another spin...
In cases like above 'switch' code I have the habit to (often) provide
a default branch that contains a fprintf(stderr, "Internal error: ..."
or a similar logging command and some form of exit or trap/catch code.
I want some safety for the cases where in the _evolving_ program bugs
sneak in by an oversight.[*]
Personally I don't care about a compiler who is clever enough to warn
me, say, about a lacking default branch but not clever enough to notice
that it's intentionally, cannot be reached (say, in context of enums).
I can understand that it might be of use for others, though. (There's
certainly some demand if it's now standard.)
I'm uninformed about __builtin_unreachable(), I don't know whether it
can be overloaded, user-defined, or anything. If that's not the case
I'd anyway write my own "Internal error: unexpected ..." function to
use that in all such cases for error detection and tracking of bugs.
Janis
[*] This habit is actually a very old one and most probably resulting
from an early observation with one of my first Simula programs coded
on a mainframe that told me: "Internal error! Please contact the NCC
in Oslo." - BTW; a nice suggestion, but useless since back these days
there was no Email available to me and the NCC was in another country.