Can we drop casts from float to bool?

Marianne Gagnon auria.mg at gmail.com
Mon Mar 22 13:46:27 PDT 2010


Don Wrote:

> This issue is inspired by bug 3918.
> 
> float f;
> if (f) ...
> 
> This currently compiles, but if the condition is true, what can you 
> conclude about f?
> Obviously it's not zero, but can it be NaN?
> If it's just translated to:  if (f!=0) ..., then f could be NaN.
> Likewise, if (!f) ... is NOT triggered if f is NaN.
> 
> I find that rather unintuitive, and I can't easily invent a case where 
> it is useful.
> 
> As bug 3918 shows, this conversion has never worked properly. Most 
> existing code that makes use of it is probably broken.
> 
> `cast(bool)f` has the same problem.  I don't think it should compile. 
> And compared to `f != 0`, it's very unclear.
> 
> This conversion seems to be confusing, bug prone, and not useful. Can we 
> just get rid if it?

I never used such feature in thousands of lines of code. I think it can safely go away (and I'd welcome that change too, /me never liked implicit conversion from numeric types to bool anyway)



More information about the Digitalmars-d mailing list