Operator overloading, structs

Yigal Chripun yigal100 at gmail.com
Thu Jun 4 12:34:40 PDT 2009


Andrei Alexandrescu wrote:
> Yigal Chripun wrote:
>> Andrei Alexandrescu wrote:
>>> Yigal Chripun wrote:
>>>> your abstraction inversion example doesn't apply here. The problem I 
>>>> see is the narrowing implicit cast, i.e. int values behave like 
>>>> booleans. I have no problem with the reverse which is what your 
>>>> example is about.
>>>
>>> An int does not convert to bool implicitly. An int can be tested with 
>>> "if", which is a different thing.
>>>
>>> Andrei
>>
>> that is an implicit cast.
> 
> No. An implicit cast is this:
> 
> int a;
> bool b = a; // doesn't compile
> 
> or this:
> 
> void fun(bool);
> fun(5); // doesn't compile
> 
> You are mistakenly presupposing that if() takes a bool. In reality if() 
> accepts a bool, an integral, a floating-point type, a pointer, an array, 
> or a class reference.
> 
> 
> Andrei
> 

I'm not debating terminology with you nor am I presupposing that if() 
currently takes a bool, I know it takes other types as well.
what I am saying is that "if" needs to be fixed such that it _will_ take 
only bool. the the C idiom of:

   int a = ...;
   if (a) {...}

is a bad pattern. it is even more problematic with floats.
it _should_ be written always as:
if (a == 0) { .. }

zero is not false. in fact zero can be very positive: zero errors, zero 
  cache misses, etc.



More information about the Digitalmars-d mailing list