In fact, you cannot even reliably detect overflow after the fact, because overflow in integer integer operations leads to undefined behavior. If the compiler can see that the code path is reached only in case of overflow, it allowed to optimize it completely (since in the case of undefined behavior it can do anything at all). Unsigned types are distinguished by the fact that they determined the characteristics of overflow (they perform arithmetic of the module).
Thus, the only way to detect overflow with signed types is to do an appropriate check in advance, which is quite expensive. It is almost always much more efficient to design such things so that the invariant of your algorithm ensures that there is no overflow.
As for resources for detecting possible overflow before it occurs, see https://stackoverflow.com/a/199413/445525
source share