5 ms·
In D an unreachable branch can be indicated with: assert(0); which is used frequently in D. This is a bonus from assert() being a builtin to D rather than
by WalterBright 4y ago
In D an unreachable branch can be indicated with:
assert(0);
which is used frequently in D. This is a bonus from assert() being a builtin to D rather than a macro.
- a1369209993 4y agoThe problem with the C++23 std::unreachable is that it invokes undefined behaviour. Calling abort (or panic, or whatever D's assert boils down to when the condition fails), would be a prefectly reasonable way to define unreachable. (That is, for example, basically how I define it in my own code:) #define unreachable die("unreachable code reached")
- MaxBarraclough 4y agoAs the article states, they're doing different things. It's not a runtime check. The point of making it undefined behaviour is that it enables the programmer to provide information to the compiler to enable better optimisation. The compiler is permitted to assume the programmer is correct about the path being unreachable, and may infer accordingly.