Treating the abusive unsigned syndrome

Christopher Wright dhasenan at gmail.com
Wed Nov 26 19:25:11 PST 2008


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.



More information about the Digitalmars-d mailing list