4 ms·
(Most) things that are undefined behavior are so for a reason. In this case - you don't know if the system you're on even stores numbers as twos complement. An
by TheLoneWolfling 12y ago
(Most) things that are undefined behavior are so for a reason.
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.
- to3m 12y agoI 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://blog.metaobject.com/2014/04/cc-osmartass.html, http://robertoconcerto.blogspot.co.uk/2010/10/strict-aliasing.html http://robertoconcerto.blogspot.co.uk/2010/10/strict-aliasin...
- heinrich5991 12y agoThe good thing is that this allows to optimize out in reasonable places too.