4 ms·
Yes, I've heard the same thing. The C and C++ specifications specifically mention that some things are undefined behaivour, that shows that its not something th
by dkersten 8y ago
Yes, I've heard the same thing. The C and C++ specifications specifically mention that some things are undefined behaivour, that shows that its not something that slipped through the cracks (at least in those cases) but rather a conscious decision to leave it undefined. This only makes sense if its done to allow the compiler room to do things like aggressive optimisation.
I only wish that undefined behaviour in C++ were harder to accidentally use, or at least, if the compiler warned me "hey, I'm gonna do X here because that undefined behaviour here allows me to do so, this may not be portable to other compilers and may not be what you wanted, you've been warned!"
- readams 8y agoThe reason that these behaviors are undefined typically is that the compiler cannot prove whether or not a program will execute undefined behavior, and needing to account for it would lead to less efficient code. For example if (x+1 > x) can be simplified to if (true) for signed integers only because signed overflow is undefined. That said, there is UBSan, which is a runtime checker for undefined behavior that can help you to identify if your code is depending on some undefined result.