4 ms·
As others have pointed out, compilers are already free to do that if they wish. GCC has a flag to trap on signed integer overflow, [0] I believe Clang does too.
by MaxBarraclough 3y ago
As others have pointed out, compilers are already free to do that if they wish. GCC has a flag to trap on signed integer overflow, [0] I believe Clang does too.
Another option is to use a library like SafeInt [1] or Boost Safe Numerics [2] which offer drop-in replacement types like safe<int>.
If you want to use raw unchecked integer types in release builds, you could do that using a typedef like my_int, in combination with a #if.
[0] https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html , the -ftrapv flag
[1] https://github.com/dcleblanc/SafeInt https://github.com/dcleblanc/SafeInt
[2] https://www.boost.org/doc/libs/release/libs/safe_numerics/doc/html/index.html https://www.boost.org/doc/libs/release/libs/safe_numerics/do...