by Diggsey · 4y ago · 0 comments
Very few other languages have a similar rule (eg. Rust explicitly does not have this rule)
One chink in this rule is interior mutability. This is why it is better to think of `&T` as a shared reference than an immutable reference. For example in the following code Rust can't assume that `a.get() == 5`. (Even if the second argument is changed to i8)
pub fn test(a: &std::cell::Cell<u8>, b: &std::cell::Cell<u8>) -> u8 {
a.set(5);
b.set(7);
a.get()
}
https://rust.godbolt.org/z/e1c6Kx4ebCommenting out the write to b does allow Rust to hardcode the return value as 5.
That's not true, in C++ a byte is guaranteed to be able to hold a UTF-8 code unit.
https://timsong-cpp.github.io/cppwp/n4868/intro.memory#1.sen...
Alternatively, it implies that there's alot of broken code out there. So much broken code that they've accidentally found safety in numbers, and compilers are unlikely to change a coincidental behavior upon which they wrongly relied. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66110