The defining property of a short circuiting operator is that it doesn't need to evaluate the right side if the left side already determines the result. For or
if the left side is true
the result will be true in any case and it's unnecessary to evaluate the right side. For and
it's the same except that if the left side is false
the result will be false.
You need to overload both |
and true
to get ||
. And &
and false
to get &&
.
a||b
corresponds to something like op_true(a)?a:(a|b)
. So if the true operator returns true it does not need to evaluate the expression of b.
a&&b
corresponds to something like op_false(a)?a:(a&b)
. So if the false operator returns true it does not need to evaluate the expression of b.
Overloading the short circuiting operators is useful when creating a custom boolean type, such as nullable bools(See DBBool
)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…