2 ms·
I cannot understand your argument. For release builds, the default compilation options result in unsafe programs, both for Rust and for C++. So with default o
by adrian_b 5y ago
I cannot understand your argument.
For release builds, the default compilation options result in unsafe programs, both for Rust and for C++.
So with default options, all Rust programs are unsafe and all C++ programs are unsafe.
I do not see any difference whatsoever.
It is true that there is one difference in the options required for safe programs.
For Rust you have just one option, to enable overflow checks.
For C++, you must give 2 different options, one to enable overflow checks and one to forbid the optimizations that assume that integer overflow cannot happen.
The extra flexibility of the C++ compilers is actually useful, because there are cases when you are certain that overflow cannot happen and in that cases you want the compiler to do all the applicable optimizations.
The right way to use such options is to always enable the safe options, which will ensure correct behavior for the program in all cases and to disable the checks and/or enable the unsafe optimizations only for the cases when it can be predicted for sure that overflows will not happen.
The failure cases for mistaken wrapping and "mistaken undefined" (the latter meaning that the compiler is allowed to assume that overflow cannot happen) are not different.
If an unexpected overflow happens, that is a certain failure in both cases.
When your program is written to work with modular numbers a.k.a. wrapping numbers, that is a different case and in that case enabling overflow checks will break that program.
Interval numbers and modular numbers are distinct types that cannot be interchanged in a program whenever interval overflow or modular reduction may happen.
- Dylan16807 5y agoWith default options, Rust programs overflow in a limited way most people call 'safe', and C++ programs that would overflow have undefined behavior which is very unsafe. > The failure cases for mistaken wrapping and "mistaken undefined" (the latter meaning that the compiler is allowed to assume that overflow cannot happen) are not different. They are extremely different. You still get a number with overflowing, and most of the time it's the number you want. If you expect wrapping, it will always be the number you want. Undefined behavior can be much more broken, and it will actively defy expectations. No language will force you to write correct code. Safety is related but not a synonym for correct.