The C++ Standard at some point states that:
5 Expressions [expr]
...
If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. [ Note: most existing implementations of C++ ignore integer overflows...]
I'm trying to understand why most(all?) implementations choose to ignore overflows rather than doing something like throwing an std::overflow_error exception. Is it not to incur any additional runtime cost? If that's the case, can't the underlying arithmetic processing hardware be used to do that check for free?
1 Answer 1
If that's the case, can't the underlying arithmetic processing hardware be used to do that check for free?
Raising an exception always has a cost. But perhaps some architectures can guarantee that when an exception is not raised, then the check is free.
However, C++ is designed to be efficiently implementable on a wide range of architectures. It would violate the design principles of C++ to mandate checking for integer overflow, unless all architectures could support such checks with zero cost in all cases where overflow does not occur. This is not the case.
LEAinstruction does not set flags.