Equivalence of “a @= b” and “a = a @ b”

前端 未结 4 1169
梦如初夏
梦如初夏 2021-02-12 22:15

It\'s often mooted (indeed I think even the standard alludes to it), that a @= b and a = a @ b are equivalent. Here I\'m using @ to stand

4条回答
  •  梦如初夏
    2021-02-12 22:58

    The language standard only defines the behavior of the built-in operators, not the "user defined" overloads. And from a language point of view, there is no built-in operator for std::atomic_int (which is formally a "user defined type"); std::atomic_int is a typedef for std::atomic, which defines a number of operator@= overloads, but no simple @. So for

    std::atomic_int i;
    i ^= 1;
    

    the second line becomes:

    i.operator^=( 1 );
    

    but for:

    std::atomic_int i;
    i = i ^ 1;
    

    the second line becomes:

    i.operator=( i.operator int() ^ 1 );
    

    One could argue that this is part of what is implied by "the left hand argument being evaluated twice, instead of once". More generally, however, the definitions for overloaded operators are whatever the author of the operator wanted: operator+= could (as far as the language is concerned) actually subtract, even when operator+ added. (I've a couple of cases where operator+ actually does operator+=. This is not usually a good idea, and in my case, it only happens with classes specially designed for use with std::accumulate, and documented to only be used in that case.) The standard simply doesn't restrict user defined operators

提交回复
热议问题