|
|
|
|
|
by xphos
56 days ago
|
|
I'd say the alternative is the rust approach. of debug mode always doing checked div and checked add ops. this prevents a large class of issues which you can find in debug mode. In release mode those UB checks are dropped. You of course can specify the type of add you want i.e saturating or wrapping etc. But yeah it makes things slower but the alterative is the code potentially doesn't work. Linux is funny in this regard which i think is why Linus allows rust and not c++ because potentially not working is a big deal in a kernel. C++ doesn't address this issue and of course C does not C++'s potential UB space is so much bigger because of alliasing issues and object patterns exposing more chances for UB. Regardless you are right there is a cost is it worth paying hard to say |
|
Checked divs are present in release mode too. Omitting them would be UB.
Other checks (e.g. for additions) are omitted in release mode, but they were not preventing UB in the first place because they guard a wrapping addition, not an unchecked one (the kind that's UB in C).