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

ponce contact at gam3sfrommars.fr
Fri Feb 21 02:45:49 PST 2014


On Thursday, 20 February 2014 at 20:52:23 UTC, Xinok wrote:
> The following statement prints false:
>
> writeln(-1 < uint.max);

I don't see this as a bug, this is exactly what I expect from a 
language with intact C integer semantics.

> 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.

That subtly breaks C compatiblity.

> Others have suggested disallowing comparing a signed type with 
> an
> unsigned type. I think this is a better solution.

Currently in C the unsigned vs signed operations all follow the 
same rules. If you do this, would you also disallow unsigned vs 
signed addition, subtraction, divide?
I feel this would make porting C code much longer, and it's 
already quite a bit of work.



More information about the Digitalmars-d mailing list