4 ms·
It is best not to try to roll your own checked arithmetic. Your compiler already has an implementation, that will likely compile down to "check the overflow fla
by jcranmer 3y ago
It is best not to try to roll your own checked arithmetic. Your compiler already has an implementation, that will likely compile down to "check the overflow flag in hardware", and carries 0 risk of undefined behavior. Why not use it?
("It's not portable" is a reason that will cease to be true shortly--as mentioned, C23 added checked overflow functions.)
- gavinhoward 3y agoBecause I want to stay on C11. > Your compiler already has an implementation That's a big assumption that is not true on all compilers I want to target.
- yjftsjthsd-h 3y ago> ("It's not portable" is a reason that will cease to be true shortly--as mentioned, C23 added checked overflow functions.) So in practice, today, it's not portable and won't be for a while (even when it's released into compilers, it'll take time for those new versions to trickle into the ecosystem), which is a compelling argument against using it.