forcing evaluation in if()
xs0
xs0 at xs0.com
Fri Nov 17 05:24:12 PST 2006
Mariano wrote:
> == Quote from xs0 (xs0 at xs0.com)'s article
>>> One question about && and || optimization:
>>>
>>> Is there some way to specify that I need to evaluate the 2 sides of the
>>> comparer?
>>>
>>> (something like &&& or ||| )
>>>
>>> note: "if( And( left() , right() ) )" or similar is not a solution :-)
>> & and | should do the trick :)
>> xs0
>
> Well, the '&' doesn't always work, considering the ambibalence between integers and booleans.
> For instance:
>
> cmp(a,b) & cmp(b,c)
>
> will be considered false even if the 3 strings are different, if they return values (for example) 1 and 2; 1 & 2 is 0
> (01 & 10). Same thing with XOR. Nevertheless, the OR would work, and you could use !(!a | !b) instead if &.
Yup. I just assumed the operands were bools in the first place..
Other workarounds include
cast(bool)a & cast(bool)b
!!a & !!b
a?b?1:0:b?0:0 // just kidding
Though, I think that if I wanted to always execute both sides, I'd put
that execution outside the if expression, it makes it far more obvious
what's going on.
auto a = something();
auto b = somethingElse();
if (a && b) {
...
}
xs0
More information about the Digitalmars-d
mailing list