Why is there no ^^ operator in C/C++?

前端 未结 7 1928
孤街浪徒
孤街浪徒 2020-12-16 13:31

& has &&. | has ||. Why doesn\'t ^ have ^^?

I understand that it wouldn\'t

7条回答
  •  借酒劲吻你
    2020-12-16 14:21

    Dennis Ritchie answers

    There are both historical and practical reasons why there is no ^^ operator.

    The practical is: there's not much use for the operator. The main point of && and || is to take advantage of their short-circuit evaluation not only for efficiency reasons, but more often for expressiveness and correctness.
    [...]
    By contrast, an ^^ operator would always force evaluation of both arms of the expression, so there's no efficiency gain. Furthermore, situations in which ^^ is really called for are pretty rare, though examples can be created. These situations get rarer and stranger as you stack up the operator--

    if (cond1() ^^ cond2() ^^ cond3() ^^ ...) ...
    

    does the consequent exactly when an odd number of the condx()s are true. By contrast, the && and || analogs remain fairly plausible and useful.

提交回复
热议问题