Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no ||= operator in C/C++? [duplicate]

Tags:

c++

c

operators

Possible Duplicates:
&&= and ||= operators

See title. This question may be expanded to include all logical+assignment operators. To clarify: I am not talking about |= operator.

Another example: &&=

Thanks.

Example:

bool result = false;

// Today
result = result || my_test1();
// Do stuff
result = result || my_test2();

// In my imagination only...
result ||= my_test1();
// Do stuff
result ||= my_test2();
like image 253
kevinarpe Avatar asked Jul 13 '11 08:07

kevinarpe


People also ask

Which operator is not used in C?

The logical NOT operator is represented as the '!' symbol, which is used to reverse the result of any given expression or condition. If the result of an expression is non-zero or true, the result will be reversed as zero or false value.

What does the operation |= do in C?

|= is shorthand for doing an OR operation and assignment. For example,, x |= 3 is equivalent to x = x | 3 . You can also use other operators ( +, -, *, & , etc) in this manner as well.

Can you use || in C?

Logical OR (||) operator in C Logical OR is denoted by double pipe characters (||), it is used to check the combinations of more than one conditions; it is a binary operator – which requires two operands.

What is the difference between && and || in C?

A more detailed explanation on how these work is this: OR ( || ) - If EITHER or BOTH sides of the operator is true, the result will be true. AND ( && ) - If BOTH and ONLY BOTH sides of the operator are true, the result will be true. Otherwise, it will be false.


2 Answers

At a guess, as they would only make sense for bools, which were late to the party

Even if bools had been around at the start these operators would be of limited use, as without side-effects they would be identical to |= and &= with boolean operands, so the only use would be trapping passing a non-bool in by accident.

If the proposed operators are also short-circuiting (not unreasonable as || and && are for in-built types) then you also have an additional justification for them in the presence of side effects.

The only other possible reason I can think of for allowing them would be if it significantly simplified parsing/compiling the language, however that is likely not the case given that they don't make sense for non-bool types.

Ultimately they are not in the language because no one has cared enough to put them in the language, thus we can conclude that none of these justifications is sufficient to warrant the cost of submitting a proposal, getting it into the standard and implementing the feature.

like image 96
jk. Avatar answered Oct 11 '22 20:10

jk.


Because they wouldn't make sense. The definition of x op= y is x = x op y, except that x is only evaluated once. What does this mean with a short circuited operator, which converts each of its operands (if it evaluates them) to bool? The equivalent of ||= might be something like:

if ( !x )
    x = y;

which is certainly different from the interpretation of other op=.

like image 32
James Kanze Avatar answered Oct 11 '22 19:10

James Kanze