forcing evaluation in if()

Elmar chrehme at gmx.de
Sun Sep 12 09:08:12 UTC 2021


On Friday, 17 November 2006 at 13:24:01 UTC, xs0 wrote:
> Mariano wrote:
>> 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 :-)
>>
>
> ...
>
> Other workarounds include
>
> cast(bool)a & cast(bool)b
> !!a & !!b
> a?b?1:0:b?0:0 // just kidding
>
> ...
>
> auto a = something();
> auto b = somethingElse();
> if (a && b) {
>    ...
> }
>

There is another way which I choose for bitwise-operable types:

```
if (a != 0 & b != 0) { ... }   // no short-circuit behaviour
if (a != 0 ^ b != 0) { ... }
```

For me it's better to read than `cast(bool)a & cast(bool)b` but 
it has same semantics.
This *could* probably generate better performing code than `!!a & 
!!b`.



More information about the Digitalmars-d mailing list