4 ms·
The generate the same assembly in this particular context, but changes to surrounding code could change that. The fact that signed int overflow is UB in C++ is
by oconnor663 5y ago
The generate the same assembly in this particular context, but changes to surrounding code could change that. The fact that signed int overflow is UB in C++ is a major difference.
> C++ is better because the developer may choose to keep the option also in release builds
You can enable overflow checks in release mode if you like:
https://doc.rust-lang.org/cargo/reference/profiles.html https://doc.rust-lang.org/cargo/reference/profiles.html
- adrian_b 5y agoThe fact that integer overflow is undefined in C++ is ugly, but this leaves the solution of this problem to the compiler. The final behavior is decided by the programmer, by using or not using "-fsanitize=signed-integer-overflow" or equivalent options for the compilation. So after the programmer makes a decision, the behavior in C++ is defined and it is either like in debug Rust or like in release Rust, depending on the programmer's decision. While I hate undefined behaviors in C/C++, I prefer to be able to make my own decisions on the behavior of integer overflow, instead of being forced by Rust to ignore overflows in release builds. Edit: Thanks for the Cargo link. If you can change the overflow checking option in the Cargo profile, then there remains absolutely no difference between C++ and Rust regarding integer overflow. The Rust RFC "Integer overflow #560" is totally misleading in this case.
- SAI_Peregrinus 5y agoRust integer overflow is never undefined behavior. It'll either wrap (default release mode) or trap (debug mode, release mode with the flag on). C++ defaults to it being undefined.