Tomas Lindquist Olsen wrote:
I'm not really sure what I think about all this. I try to always insert
assertions before operations like this, which makes me think the nicest
solution would be if the compiler errors out if it detects a problematic
expression that is unchecked...
uint diff(uint begin, uint end)
{
return end - begin; // error
}
uint diff(uint begin, uint end)
{
assert(begin <= end);
return end - begin; // ok because of the assert
}
I'm not going to get into how this would be implemented in the compiler,
but it sure would be sweet :)
On the other hand, the CPU can report on integer overflow, so you could
turn that into an exception if the expression doesn't include a cast.