Correct comparison of signed type with unsigned type (and vice versa)

Meta jared771 at gmail.com
Thu Feb 20 14:52:54 PST 2014


On Thursday, 20 February 2014 at 20:52:23 UTC, Xinok wrote:
> The following statement prints false:
>
> writeln(-1 < uint.max);
>
> This came up in another topic recently. I think this is silly 
> and
> an unnecessary source of bugs (it's bitten me before and
> presumably many others as well). I'm making a proposal to add an
> extra check so that comparisons of signed with unsigned types is
> always correct. Simply, if the signed type is negative, it is by
> default less than the unsigned value. The compiler has all the
> information it needs at compile time to add this check where
> necessary. I demonstrate the problem and solution here:
>
> http://dpaste.dzfl.pl/acd819d1a9ea
>
> Others have suggested disallowing comparing a signed type with 
> an
> unsigned type. I think this is a better solution. Yes, it will
> add a small bit of overhead, but I believe it's more important
> for code to be correct than to be fast.
>
> Any takers?

This will silently change the semantics of any C code compiled 
with DMD (even if those semantics were extremely bug prone in the 
first place). Is this a good or bad thing? I can't think of any C 
code that would rely on such behaviour, but I think it'd just be 
safer all-around to make it an error.


More information about the Digitalmars-d mailing list