Is there actually a reason why overloaded && and || don't short circuit?

前端 未结 9 1751
礼貌的吻别
礼貌的吻别 2020-11-28 21:54

The short circuiting behaviour of the operators && and || is an amazing tool for programmers.

But why do they lose this behaviour w

9条回答
  •  佛祖请我去吃肉
    2020-11-28 22:41

    but the operators for bool have this behaviour, why should it be restricted to this single type?

    I just want to answer this one part. The reason is that the built-in && and || expressions are not implemented with functions as overloaded operators are.

    Having the short-circuiting logic built-in to the compiler's understanding of specific expressions is easy. It's just like any other built-in control flow.

    But operator overloading is implemented with functions instead, which have particular rules, one of which is that all the expressions used as arguments get evaluated before the function is called. Obviously different rules could be defined, but that's a bigger job.

提交回复
热议问题