146 lines
6.0 KiB
Markdown
146 lines
6.0 KiB
Markdown
[expr.assign]
|
||
|
||
# 7 Expressions [[expr]](./#expr)
|
||
|
||
## 7.6 Compound expressions [[expr.compound]](expr.compound#expr.assign)
|
||
|
||
### 7.6.19 Assignment and compound assignment operators [expr.assign]
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L8067)
|
||
|
||
The assignment operator (=) and the compound assignment
|
||
operators all group right-to-left[.](#1.sentence-1)
|
||
|
||
All
|
||
require a modifiable lvalue as their left operand; their result is an lvalue
|
||
of the type of the left operand, referring to the left operand[.](#1.sentence-2)
|
||
|
||
The result in all cases is a bit-field if
|
||
the left operand is a bit-field[.](#1.sentence-3)
|
||
|
||
In all cases, the assignment is
|
||
sequenced after thevalue computation of the right and left operands,
|
||
and before the
|
||
value computation of the assignment expression[.](#1.sentence-4)
|
||
|
||
The right operand is sequenced before the left operand[.](#1.sentence-5)
|
||
|
||
With
|
||
respect to an indeterminately-sequenced function call, the operation of
|
||
a compound assignment is a single evaluation[.](#1.sentence-6)
|
||
|
||
[*Note [1](#note-1)*:
|
||
|
||
Therefore, a function call cannot intervene between the
|
||
lvalue-to-rvalue conversion and the side effect associated with any
|
||
single compound assignment operator[.](#1.sentence-7)
|
||
|
||
â *end note*]
|
||
|
||
[assignment-expression:](#nt:assignment-expression "7.6.19 Assignment and compound assignment operators [expr.assign]")
|
||
[*conditional-expression*](expr.cond#nt:conditional-expression "7.6.16 Conditional operator [expr.cond]")
|
||
[*yield-expression*](expr.yield#nt:yield-expression "7.6.17 Yielding a value [expr.yield]")
|
||
[*throw-expression*](expr.throw#nt:throw-expression "7.6.18 Throwing an exception [expr.throw]")
|
||
[*logical-or-expression*](expr.log.or#nt:logical-or-expression "7.6.15 Logical OR operator [expr.log.or]") [*assignment-operator*](#nt:assignment-operator "7.6.19 Assignment and compound assignment operators [expr.assign]") [*initializer-clause*](dcl.init.general#nt:initializer-clause "9.5.1 General [dcl.init.general]")
|
||
|
||
[assignment-operator:](#nt:assignment-operator "7.6.19 Assignment and compound assignment operators [expr.assign]") one of
|
||
= *= /= %= += -= >>= <<= &= ^= |=
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L8115)
|
||
|
||
In simple assignment (=),
|
||
let V be the result of the right operand;
|
||
the object referred to by the left operand is
|
||
modified ([[defns.access]](defns.access "3.1 access")) by replacing its value
|
||
with V or,
|
||
if the object is of integer type,
|
||
with the value congruent ([[basic.fundamental]](basic.fundamental "6.9.2 Fundamental types")) to V[.](#2.sentence-1)
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L8124)
|
||
|
||
If the right operand is an expression, it is implicitly[converted](conv "7.3 Standard conversions [conv]") to the cv-unqualified type of the left
|
||
operand[.](#3.sentence-1)
|
||
|
||
[4](#4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L8130)
|
||
|
||
When the left operand of an assignment operator
|
||
is a bit-field that cannot represent the value of the expression, the
|
||
resulting value of the bit-field isimplementation-defined[.](#4.sentence-1)
|
||
|
||
[5](#5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L8137)
|
||
|
||
An assignment whose left operand is of
|
||
a volatile-qualified type is deprecated ([[depr.volatile.type]](depr.volatile.type "D.4 Deprecated volatile types"))
|
||
unless the (possibly parenthesized) assignment is a discarded-value expression or
|
||
an [unevaluated operand](expr.context#def:unevaluated_operand "7.2.3 Context dependence [expr.context]")[.](#5.sentence-1)
|
||
|
||
[6](#6)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L8143)
|
||
|
||
The behavior of an expression of the form E1 *op*= E2 is equivalent to E1 = E1 *op* E2 except
|
||
that E1 is evaluated only once[.](#6.sentence-1)
|
||
|
||
[*Note [2](#note-2)*:
|
||
|
||
The object designated by E1 is accessed twice[.](#6.sentence-2)
|
||
|
||
â *end note*]
|
||
|
||
For += and -=,E1 shall either have arithmetic type or be a pointer to a
|
||
possibly cv-qualified completely-defined object type[.](#6.sentence-3)
|
||
|
||
In all other
|
||
cases, E1 shall have arithmetic type[.](#6.sentence-4)
|
||
|
||
[7](#7)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L8155)
|
||
|
||
If the value being stored in an object is read via another object that
|
||
overlaps in any way the storage of the first object, then the overlap shall be
|
||
exact and the two objects shall have the same type, otherwise the behavior is
|
||
undefined[.](#7.sentence-1)
|
||
|
||
[*Note [3](#note-3)*:
|
||
|
||
This restriction applies to the relationship
|
||
between the left and right sides of the assignment operation; it is not a
|
||
statement about how the target of the assignment can be aliased in general[.](#7.sentence-2)
|
||
|
||
See [[basic.lval]](basic.lval "7.2.1 Value category")[.](#7.sentence-3)
|
||
|
||
â *end note*]
|
||
|
||
[8](#8)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L8167)
|
||
|
||
A [*braced-init-list*](dcl.init.general#nt:braced-init-list "9.5.1 General [dcl.init.general]") B may appear on the right-hand side of
|
||
|
||
- [(8.1)](#8.1)
|
||
|
||
an assignment to a scalar of type T, in which case B shall have at most a single element[.](#8.1.sentence-1)
|
||
The meaning of x = B is x = t,
|
||
where t is an invented temporary variable
|
||
declared and initialized as T t = B[.](#8.1.sentence-2)
|
||
|
||
- [(8.2)](#8.2)
|
||
|
||
an assignment to an object of class type, in which case B is passed as the argument to the assignment operator function selected by
|
||
overload resolution ([[over.assign]](over.assign "12.4.3.2 Simple assignment"), [[over.match]](over.match "12.2 Overload resolution"))[.](#8.2.sentence-1)
|
||
|
||
[*Example [1](#example-1)*: complex<double> z;
|
||
z = { 1,2 }; // meaning z.operator=({1,2}) z += { 1, 2 }; // meaning z.operator+=({1,2})int a, b;
|
||
a = b = { 1 }; // meaning a=b=1; a = { 1 } = b; // syntax error â *end example*]
|