Re:Named Booleans prevent C++ bugs and save you time
by maccard on Saturday February 18, 2023 @01:37PM
(#34849651)
It's the same idea, that the && short circuits.
bool dirty = ...;
bool success = doExpensiveCalculation(...);
vs if (dirty && doExpensiveCalculation)
{
Re:Named Booleans prevent C++ bugs and save you time
by saghm on Saturday February 18, 2023 @02:03PM
(#34849934)
Not necessarily; in first case, the stuff after the `&&` will definitively not be run. In the second case, it depends on how the code is optimized, and there's no guarantee that things moved to intermediate variables won't still be evaluated even if they're never used. Since the compiler presumably can't know the value of the boolean at runtime, it would need to reorder things to avoid doing the unnecessary work, and I wouldn't think it's something that you could just assume.
But I don't understand your "inefficient" point. At the end of the day, everything gets turned into SSA. So expression-per-line and nested-expressions generates the same code, right?