When the operands of &&
or ||
are of types that declare an applicable user-defined operator
&
or operator
|
, both of the following must be true, where T
is the type in which the selected operator is declared:
T
. In other words, the operator must compute the logical AND
or the logical OR
of two operands of type T
, and must return a result of type T
.T
must contain declarations of operator
true
and operator
false
.A compile-time error occurs if either of these requirements is not satisfied. Otherwise, the &&
or ||
operation is evaluated by combining the user-defined operator
true
or operator
false
with the selected user-defined operator:
x
&&
y
is evaluated as T.false(x)?
x:
T.&(x,
y)
, where T.false(x)
is an invocation of the operator
false
declared in T
, and T.&(x,
y)
is an invocation of the selected operator
&
. In other words, x
is first evaluated and operator
false
is invoked on the result to determine if x
is definitely false. Then, if x
is definitely false, the result of the operation is the value previously computed for x
. Otherwise, y
is evaluated, and the selected operator
&
is invoked on the value previously computed for x
and the value computed for y
to produce the result of the operation.x
||
y
is evaluated as T.true(x)?
x:
T.|(x,
y)
, where T.true(x)
is an invocation of the operator
true
declared in T
, and T.|(x,
y)
is an invocation of the selected operator
|
. In other words, x
is first evaluated and operator
true
is invoked on the result to determine if x
is definitely true. Then, if x
is definitely true, the result of the operation is the value previously computed for x
. Otherwise, y
is evaluated, and the selected operator
|
is invoked on the value previously computed for x
and the value computed for y
to produce the result of the operation.In either of these operations, the expression given by x
is only evaluated once, and the expression given by y
is either not evaluated or evaluated exactly once.
For an example of a type that implements operator
true
and operator
false
, see §11.3.2.