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

Possible duplicates:
& = and || = operators

See chapter. This question can be expanded to include all logical operators + assignments. To clarify: I'm 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 ();
+8
c ++ c operators
source share
4 answers

Under the assumption, as they would only make sense to bools who were late for the party

Even if bools were in the beginning, these operators would have limited use, since without side effects they would be identical with |= and &= with boolean operands, so the only use would be a trap that transmits non-bool randomly.

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

The only possible reason I can think of resolving them would be to greatly simplify the parsing / compilation of the language, however this is most likely not the case, since they do not make sense for non-bool types.

Ultimately, they are not in the language, because no one cared to put them in the language, so we can conclude that none of these excuses is sufficient to guarantee the cost of presenting the proposal, introducing it into the standard and exercise function.

+13
source share

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

 if ( !x ) x = y; 

which, of course, differs from the interpretation of other op = .

+8
source share

There is no good logical reason ... just historical pressure on functions.

Logical operators are usually not available as single-processor opcodes, so they are not as cheap or fundamental as their bitwise copies. A logical XOR would also be nice, maybe ^^ , then ^^= .

However, an increase in the number of operators makes it increasingly painful to create drop-down user types with built-in semantics, and there is little real growth. I would still like to see them - these operators exist in some other languages, such as Ruby, and I found them convenient and expressive.

+3
source share

Because their modus operandi is very different.

Look at the captions:

 T operator+(T,T); T& T::operator+=(T); T operator*(T,T); T& T::operator*=(T); bool operator&&(T,T); ??? 

Conjugate operators exist for operators that return the same type of arguments, the operators && and || return a boolean (or at least the expected one), and therefore it makes no sense to create a complex version, except, perhaps, only for booleans.

+3
source share

All Articles