Is this reasonable?

Dominikus Dittes Scherkl Dominikus.Scherkl at continental-corporation.com
Fri Dec 6 04:22:34 PST 2013


>> The best that could be done would arguably be to simply do the
>> comparison the 'right' way. E.g. static assert(-1 < 0u).
>
> What's the 'right' way? assert(-1 < uint.max) will always fail 
> because
> no matter whether you convert to int or uint, the comparison 
> simply
> cannot be carried out at the machine code level.

But the compiler should be able to do such things at compile time.
If you compare an unsigned variable with a negative literal,
"-1 < x" or "x > -1": the whole expression can be rewritten to 
"true",
"-1 == x" or "-1 > x" or "-1 >= x" etc. can be rewritten to 
"false".
This is not only no performance problem but instead extremely 
efficient!


More information about the Digitalmars-d-learn mailing list