Bug? 0 is less than -10

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 6 07:55:22 PDT 2015


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.


More information about the Digitalmars-d-learn mailing list