0

I am trying to understand the rational behind defining a signed overflow in C and C++ as undefined behavior. Presumably, this is to allow optimizations not otherwise possible. It is not clear to me, however, what those optimizations are.

I know that there is a C++20 proposal that would make signed integers in C++ more defined. At the time of this writing, however, this proposal also leaves a signed integer overflow as undefined.

Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57
  • 3
    AFAIK, signed overflow differs in different CPUs, so it's left undefined. Anyway: https://stackoverflow.com/questions/18195715/why-is-unsigned-integer-overflow-defined-behavior-but-signed-integer-overflow-is – Nikos C. Jun 08 '19 at 03:41
  • 1
    [How undefined signed overflow enables optimizations in GCC](https://kristerw.blogspot.com/2016/02/how-undefined-signed-overflow-enables.html). – Cornstalks Jun 08 '19 at 03:43
  • The author of said proposal gave a [CppCon talk](https://www.youtube.com/watch?v=JhUxIVf1qok) that covered this as far as I remember. – chris Jun 08 '19 at 03:46
  • @NikosC. no that's not it. If you read the proposal, it seeks to define signed integers as 2's complement, and was accepted. Still, despite defining signed integers as 2's complement, overflows are still not defined. – Shachar Shemesh Jun 08 '19 at 06:43
  • @Cornstalks if you submit that as an answer, I'll accept it. – Shachar Shemesh Jun 08 '19 at 06:45

0 Answers0