From what I find on the net, signed integer overflow is undefined in c++ because we simply don't have only one representation for signed integers. But I can't understand why that doesn't make it implementation-defined where each implementation can itself provides a well-defined behavior for signed overflow based on its representation of signed integers.
Asked
Active
Viewed 101 times
0

Amir Valizadeh
- 79
- 6
-
1afaik the difference would just be that every implementation would need to specifiy it, while now it already can. So practiaclly not much gain – 463035818_is_not_an_ai Feb 25 '23 at 12:39
-
related https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior – 463035818_is_not_an_ai Feb 25 '23 at 12:43
-
more related https://stackoverflow.com/questions/16188263/is-signed-integer-overflow-still-undefined-behavior-in-c – 463035818_is_not_an_ai Feb 25 '23 at 12:44
-
3I suppose it boils down to enabling optimizations which would be less straightforward with implementation defined. See eg this answer https://stackoverflow.com/a/16188924/4117728 – 463035818_is_not_an_ai Feb 25 '23 at 12:45
-
4C++20 requires signed integers have 2s complement representation in the C++ abstract machine. Regardless, signed integer overflow is still also defined to be **undefined behavior**, which (I believe) is because to avoid the (otherwise) adverse optimization impact. – Eljay Feb 25 '23 at 12:55
-
3Pragmatic view: undefined means that arithmetic has the same problems everywhere; implementation-defined means that you need to consider every implementation every time you do any arithmetic. Nobody wants to maintain several conditional versions of basic arithmetic, so the end result would just be that everyone avoids overflow as if it were undefined anyway. – molbdnilo Feb 25 '23 at 13:02