I had the need to code a statement of the form
a = a || expr;
where expr
should be evaluated and the result be assigned to <
Because the return type of operators ||
and &&
is not the same as type of their left argument.
The return type of ||
and &&
is always int
1, while the left argument may be any integral, floating point or pointer type. The operands also don't have to be of the same type. Therefore defining x ||= y
as x = x || y
and x &&= y
as x = x && y
as would be consistent with other augmented assignments would not be able to store the result in the argument for most types.
You could come up with other definitions, e.g. x ||= y
as if(!x) x = y
and x &&= y
as if(!y) x = y
, but that would not be exactly obvious and it is not that useful, so it was not included.
1In C++ it is bool
.