为什么 C++ 不会在算术溢出时自动抛出异常?

Why doesn't C++ automatically throw an exception on arithmetic overflow?

C++ 标准在某些时候指出:

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...]

我试图理解为什么大多数(所有?)实现选择忽略溢出而不是做一些类似抛出 std::overflow_error 异常的事情。不会产生任何额外的运行时成本吗?如果是这样的话,底层的算术处理硬件就不能免费做那个检查吗?

If that's the case, can't the underlying arithmetic processing hardware be used to do that check for free?

引发异常总是有代价的。但也许某些架构可以保证当 引发异常时,检查是免费的。

但是,C++ 被设计为可以在广泛的体系结构上高效地实现。强制检查整数溢出将违反 C++ 的设计原则,除非所有体系结构都可以在所有不发生溢出的情况下以零成本支持此类检查。事实并非如此。