Why can I use initializer lists on the right-hand side of operator += but not operator+?

后端 未结 3 1623
遇见更好的自我
遇见更好的自我 2021-01-03 20:15

This is a follow-up to an earlier question about why I can\'t use a brace-enclosed initializer as an argument to operator+, which was resolved by looking at this earlier que

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-03 20:39

    It is explained in the answer to this question (which is linked from the question you linked to).

    The language grammar only allows a braced list in certain grammatical contexts, not in place of an arbitrary expression. That list includes the right-hand side of assignment operators, but NOT the right-hand side of operators in general.

    += is an assignment operator, + is not.

    The grammar for assignment expressions is:

      assignment-expression:
         conditional-expression
         logical-or-expression assignment-operator initializer-clause
         throw-expression
      assignment-operator: one of
          = *= *= /= %= += -= >>= <<= &= ^= |=
      

提交回复
热议问题