Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short circuit of overloaded operator && and || in C++17

I read in http://en.cppreference.com/w/cpp/language/operators:

The boolean logic operators, operator && and operator ||

Unlike the built-in versions, the overloads do not sequence their left operand before the right one, and (until C++17) cannot implement short-circuit evaluation.

(My emphasis).

Couldn't find any resource or code example for C++17 supporting short-circuit for operator&& and operator||. Is it related to C++17 parameter pack fold expression? tried to play with it but couldn't create short circuit behavior for overloaded operator && and || with C++17 fold expression.

Code:

class A {
    bool val;
public:
    A(bool b) : val(b) { cout << "A born as " << boolalpha << val << endl;}
    template<typename ...Args>
    bool operator&&(Args&&... args) {
        return (val && ... && args.val);
    }    
};

int main() {
    cout << boolalpha;
    cout << ( A{false} && A{true} ) << endl;
    cout << ( A{true} && A{false} ) << endl;
    cout << ( A{false} && A{false} ) << endl;
}

Output:

A born as true
A born as false
false
A born as false
A born as true
false
A born as false
A born as false
false

http://coliru.stacked-crooked.com/a/f0b5325899c2fe6b

Note: the sequence of left to right is also not happening in current gcc version, compiled with C++17 flag.

like image 778
Amir Kirsh Avatar asked Dec 19 '16 16:12

Amir Kirsh


People also ask

What is short circuit operator in C++?

Short-circuit evaluation denotes the semantics of some Boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression: for instance, when the first argument of the AND function evaluates to false, ...

Which are short circuit operators?

In Java logical operators, if the evaluation of a logical expression exits in between before complete evaluation, then it is known as Short-circuit. A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned.

How do short circuited operators work?

Short-circuit is a tricky method for evaluating logical operators AND and OR. In this method, the whole expression can be evaluated to true or false without evaluating all sub expressions. While debugging it looks like below, In the above program both operands Condition1() and Condition2() are evaluated.

What is short circuit in programming?

Short-Circuit Evaluation: Short-circuiting is a programming concept in which the compiler skips the execution or evaluation of some sub-expressions in a logical expression. The compiler stops evaluating the further sub-expressions as soon as the value of the expression is determined.


1 Answers

That statement is not about short-circuit evaluation. It's about the order of evaluating the operands.

Pre-C++17, the order for evaluating the operands of overloaded && and || was compiler-defined. C++17 defines an explicit order of evaluation of left-to-right for && and ||, whether they are overloaded or not.

Short circuit evaluation still only applies to the built-in operators.

Note that on the actual page you cited, the part that is highlighted is what is applied to a specific version. That part is about the sequencing order, not the part about short-circuit evaluation.

like image 166
Nicol Bolas Avatar answered Sep 27 '22 22:09

Nicol Bolas