Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


The 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")


As 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: