forcing evaluation in if()
Patrick Schluter
Patrick.Schluter at bbox.fr
Mon Sep 13 13:25:46 UTC 2021
On Sunday, 12 September 2021 at 09:08:12 UTC, Elmar wrote:
> 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) { ... }
> ```
\<source>(14): Error: `num != 0` must be surrounded by
parentheses when next to operator `&`
& ^ and | have higher prio than comparison ops. => parenthesis
required.
> 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