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 a
iff a
is not set. this relies on the logical OR's short-circuiting capabilities.
The shorter way to write the above would, of course, be
a ||= expr;
but (to my surprise) C does not have logical assignment operators.
So my question is twofold. First, is there a shorter way to write the first statement in standard C (the ternary operator is even worse - a = a ? a : expr
requires me to spell out a
thrice).
Secondly, why aren't there logical assignments in C? The possible reasons I could think of are:
- it makes the grammar harder to parse?
- there is some subtlety in handling short-circuiting for these cases?
- it was considered superfluous (but isn't that an argument against ALL the operator assignments?)
EDIT
Please unlock this question because:
The question it has been linked to (as a alleged duplicate of) HAS NOT BEEN ANSWERED. The (accepted) answer to that question states that
||=
is not present because duplicates the functionality of|=
. That is the wrong answer.|=
does not short-circuit.C and C++ are NOT the same languages. I wish to know why C doesn't have it. In fact, the fact that derived languages like C++ and, particularly, Java (which did not suffer from the problems of legacy code as has been suggested in Edmund's answer) makes the question even more interesting.
EDIT 2
It now seems like my original intent was wrong. In the statement a = a || expr
(where a
is integral and expr
returns an integral value, first both a
and expr
will be implicitly converted to "booleans", and then the "boolean" value will be assigned to a
. This will be incorrect — the integral value will be lost. Thanks, Jens and Edmund.
So for the first part of the question, the correct ways, not alternatives :), to code my intention would be:
if (!a) a = expr;
or
a = a ? a : expr;
they should be optimized the same (I think) though personally I would prefer the first one (because it has one less a
to type).
However, the second part of the question still remains. The arguments that Jens and Edmund about have given about the ambiguity in a ||= expr
apply equally well to a = a || expr
. the assignment case can simply be treated as the normal one:
- convert
a
to boolean - if it is true, the value of the entire expression becomes equal to the boolean value of
a
- otherwise evaluate
expr
, convert result to boolean, assign toa
, and return it
The steps above seem to be the same for both the assignment and normal case.
||=
, etc. This question asks why C doesn't allow the trick of carrying non booleans through a boolean operator.if ( ! a ) a = expr;
which is quite clear and concise. As for tricks, note that addition and multiplication on Booleans produces OR and AND functions, respectively, so as long as you don't need short-circuits or a cast tobool
,+=
and*=
perform like||=
and&&=
respectively.a |= expr
the result ofexpr
or its logical value? What should be the type of thea |= expr
as a whole,int
, the type ofa
or the result of promotinga
andexpr
? I don't think that there is a straight solution to these question, and probably therefore anybody who might have considered this already has quickly abandoned. I personally would go fora = (a ? a : expr)
and let the compiler optimize the assignment.&&=
and||=
operator would be introducing a new semantic concept in the language, the conditional assignment. The ternary aslvalue
is already bad enough in some compilers, don't need to add some more.a ?: b
which meansa ? a : b
but without the extra evaluation