The result of an assignment expression is not an lvalue.
The
result of an assignment expression is an lvalue.
All assignment operators have the same precedence and have right-to-left associativity.
The simple assignment operator has the following form:
lvalue = expr
The operator stores the value of the right operand expr in the object designated by the left operand lvalue.
The left operand must be a modifiable lvalue. The type of an assignment operation is the type of the left operand.
If the left operand is not a class type or a vector type, the right operand is implicitly converted to the type of the left operand. This converted type will not be qualified by const or volatile.
If the left operand is a class type, that type must be complete. The copy assignment operator of the left operand will be called.
If the left operand is an object of reference type, the compiler will assign the value of the right operand to the object denoted by the reference.
The
assignment operator has been extended to permit operands of vector
type. Both sides of an assignment expression must be of the same vector
type.
The compound assignment operators consist of a binary operator and the simple assignment operator. They perform the operation of the binary operator on both operands and store the result of that operation into the left operand, which must be a modifiable lvalue.
The following table shows the operand types of compound assignment expressions:
| Operator | Left operand | Right operand |
|---|---|---|
| += or -= | Arithmetic | Arithmetic |
| += or -= | Pointer | Integral type |
| *=, /=, and %= | Arithmetic | Arithmetic |
| <<=, >>=, &=, ‸=, and |= | Integral type | Integral type |
a *= b + cis equivalent to
a = a * (b + c)and not
a = a * b + c
The following table lists the compound assignment operators and shows an expression using each operator:
| Operator | Example | Equivalent expression |
|---|---|---|
| += | index += 2 | index = index + 2 |
| -= | *(pointer++) -= 1 | *pointer = *(pointer++) - 1 |
| *= | bonus *= increase | bonus = bonus * increase |
| /= | time /= hours | time = time / hours |
| %= | allowance %= 1000 | allowance = allowance % 1000 |
| <<= | result <<= num | result = result << num |
| >>= | form >>= 1 | form = form >> 1 |
| &= | mask &= 2 | mask = mask & 2 |
| ‸= | test ‸= pre_test | test = test ‸ pre_test |
| |= | flag |= ON | flag = flag | ON |
Although the equivalent expression column shows the left operands (from the example column) twice, it is in effect evaluated only once.
In addition to the table of operand
types, an expression is implicitly converted to the cv-unqualified
type of the left operand if it is not of class type. However, if the
left operand is of class type, the class becomes complete, and assignment
to objects of the class behaves as a copy assignment operation. Compound
expressions and conditional expressions are lvalues in C++, which
allows them to be a left operand in a compound assignment expression.
When GNU C language features
have been enabled, compound expressions and conditional expressions
are allowed as lvalues, provided that their operands are lvalues.
The following compound assignment of the compound expression (a,
b) is legal under GNU C, provided that expression b,
or more generally, the last expression in the sequence, is an lvalue: (a,b) += 5 /* Under GNU C, this is equivalent to a, (b += 5) */