I think in most cases, people writing code to check for overflow do know whether the system they're on stores integers as twos complement, and they'll have a pretty good idea what will happen when overflow occurs. A portable approach for this sort of thing would be very useful, but in the mean time, perhaps we could settle for compilers that don't strip out reasonable code?
I know, I know - I'm asking a lot.
My favourite two links on the subject: http://blog.metaobject.com/2014/04/cc-osmartass.html, http://robertoconcerto.blogspot.co.uk/2010/10/strict-aliasin...
Re:Fast integer overflow detection (2012)
by heinrich5991 on Sunday December 14, 2014 @05:18PM
(#8749774)
The good thing is that this allows to optimize out in reasonable places too.
In this case - you don't know if the system you're on even stores numbers as twos complement. And if it doesn't, it may not have the same behavior. So even the unoptimized version may fail.