开发者

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

开发者 https://www.devze.com 2023-03-20 09:45 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicates: &&= and ||= operators
This question already has answers here: Closed 11 years ago.

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();


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.


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=.


No good logical reason... just historical pressures for features.

Logical operators aren't normally available as single CPU opcodes, so they're not as cheap or fundamental as their bitwise counterparts. A logical XOR would be nice too, perhaps ^^, then ^^=.

Still, a proliferation of operators makes it increasingly painful to create drop-in user-defined types with builtin-style semantics, and there's little real-world gain. I'd still like to see them - these operators do exist in some other languages, like Ruby, and I've found them convenient and expressive.


Because their modus operandi is widely different.

Look at the signatures:

T operator+(T,T);
T& T::operator+=(T);

T operator*(T,T);
T& T::operator*=(T);

bool operator&&(T,T);
???

Compounds operators exist for operators that return the same type of argument, the && and || operators return a boolean (or at least, are expected to), and therefore it does not make sense to create a compound version, except perhaps for booleans only.

0

精彩评论

暂无评论...
验证码 换一张
取 消