Hacker News new | ask | show | jobs
by stop50 38 days ago
im not an c++ expert, but it looks like the unreachable macro of rust. In rust it allows optimizing the generated binary to not care about special paths. If an integer can only be 1-4, then it doesn't make sense to dedicate an code path for 0 or 5+ that will never be called. in an explicit for loop its easy to know for the compiler, but when this information crosses boundaries like function calls then it can make it easier and can reduce the binary size.
2 comments

unreachable! in Rust is panic! with a different name [1]. The unsafe unreachable_unchecked [2] is C++ std::unreachable.

[1] https://doc.rust-lang.org/std/macro.unreachable.html [2] https://doc.rust-lang.org/std/hint/fn.unreachable_unchecked....

To elaborate on the other reply to your comment: In Rust, if you don't use `unsafe` and still manage to trigger UB, then that's a bug in the compiler or the library code you called (which in turn has to have used unsafe somewhere to trigger UB).

So the fact that `unreachable!()` does not require the use of the keyword `unsafe` means that it can't be like C++ `std::unreachable()`.