Bug? 0 is less than -10

Laeeth Isharc via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 6 16:21:08 PDT 2015


On Tuesday, 6 October 2015 at 14:55:23 UTC, Adam D. Ruppe wrote:
> On Tuesday, 6 October 2015 at 14:46:56 UTC, tcak wrote:
>> void main(){
>> 	size_t dec = 0;
>
>> How is it generating "true" for (dec <= -10) ? Is there a 
>> special casting or something?
>
> size_t is unsigned, so the -10 is cast to unsigned too for the 
> comparison which yields some huge number.
>
> Comparing signed to unsigned is almost always a mistake... but 
> one D inherited from C.
>
> This is a reason why I prefer to use int instead of size_t 
> where I can but that might require casts and truncation too.

could we have ssize_t defined in phobos somewhere so your code 
ends up being portable ;) (It's trivial to do, obviously).


More information about the Digitalmars-d-learn mailing list