From Jason Turner

[intro.execution]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpwlpyxb6l/{from.md → to.md} +56 -165
tmp/tmpwlpyxb6l/{from.md → to.md} RENAMED
@@ -1,121 +1,12 @@
1
- ## Program execution <a id="intro.execution">[[intro.execution]]</a>
2
 
3
- The semantic descriptions in this International Standard define a
4
- parameterized nondeterministic abstract machine. This International
5
- Standard places no requirement on the structure of conforming
6
- implementations. In particular, they need not copy or emulate the
7
- structure of the abstract machine. Rather, conforming implementations
8
- are required to emulate (only) the observable behavior of the abstract
9
- machine as explained below.[^6]
10
-
11
- Certain aspects and operations of the abstract machine are described in
12
- this International Standard as implementation-defined (for example,
13
- `sizeof(int)`). These constitute the parameters of the abstract machine.
14
- Each implementation shall include documentation describing its
15
- characteristics and behavior in these respects.[^7] Such documentation
16
- shall define the instance of the abstract machine that corresponds to
17
- that implementation (referred to as the “corresponding instance” below).
18
-
19
- Certain other aspects and operations of the abstract machine are
20
- described in this International Standard as unspecified (for example,
21
- evaluation of expressions in a *new-initializer* if the allocation
22
- function fails to allocate memory ([[expr.new]])). Where possible, this
23
- International Standard defines a set of allowable behaviors. These
24
- define the nondeterministic aspects of the abstract machine. An instance
25
- of the abstract machine can thus have more than one possible execution
26
- for a given program and a given input.
27
-
28
- Certain other operations are described in this International Standard as
29
- undefined (for example, the effect of attempting to modify a `const`
30
- object).
31
-
32
- [*Note 1*: This International Standard imposes no requirements on the
33
- behavior of programs that contain undefined behavior. — *end note*]
34
-
35
- A conforming implementation executing a well-formed program shall
36
- produce the same observable behavior as one of the possible executions
37
- of the corresponding instance of the abstract machine with the same
38
- program and the same input. However, if any such execution contains an
39
- undefined operation, this International Standard places no requirement
40
- on the implementation executing that program with that input (not even
41
- with regard to operations preceding the first undefined operation).
42
-
43
- An instance of each object with automatic storage duration (
44
- [[basic.stc.auto]]) is associated with each entry into its block. Such
45
- an object exists and retains its last-stored value during the execution
46
- of the block and while the block is suspended (by a call of a function
47
- or receipt of a signal).
48
-
49
- The least requirements on a conforming implementation are:
50
-
51
- - Accesses through volatile glvalues are evaluated strictly according to
52
- the rules of the abstract machine.
53
- - At program termination, all data written into files shall be identical
54
- to one of the possible results that execution of the program according
55
- to the abstract semantics would have produced.
56
- - The input and output dynamics of interactive devices shall take place
57
- in such a fashion that prompting output is actually delivered before a
58
- program waits for input. What constitutes an interactive device is
59
- *implementation-defined*.
60
-
61
- These collectively are referred to as the *observable behavior* of the
62
- program.
63
-
64
- [*Note 2*: More stringent correspondences between abstract and actual
65
- semantics may be defined by each implementation. — *end note*]
66
-
67
- [*Note 3*:
68
-
69
- Operators can be regrouped according to the usual mathematical rules
70
- only where the operators really are associative or commutative.[^8] For
71
- example, in the following fragment
72
-
73
- ``` cpp
74
- int a, b;
75
- ...
76
- a = a + 32760 + b + 5;
77
- ```
78
-
79
- the expression statement behaves exactly the same as
80
-
81
- ``` cpp
82
- a = (((a + 32760) + b) + 5);
83
- ```
84
-
85
- due to the associativity and precedence of these operators. Thus, the
86
- result of the sum `(a + 32760)` is next added to `b`, and that result is
87
- then added to 5 which results in the value assigned to `a`. On a machine
88
- in which overflows produce an exception and in which the range of values
89
- representable by an `int` is \[`-32768`, `+32767`\], the implementation
90
- cannot rewrite this expression as
91
-
92
- ``` cpp
93
- a = ((a + b) + 32765);
94
- ```
95
-
96
- since if the values for `a` and `b` were, respectively, -32754 and -15,
97
- the sum `a + b` would produce an exception while the original expression
98
- would not; nor can the expression be rewritten either as
99
-
100
- ``` cpp
101
- a = ((a + 32765) + b);
102
- ```
103
-
104
- or
105
-
106
- ``` cpp
107
- a = (a + (b + 32765));
108
- ```
109
-
110
- since the values for `a` and `b` might have been, respectively, 4 and -8
111
- or -17 and 12. However on a machine in which overflows do not produce an
112
- exception and in which the results of overflows are reversible, the
113
- above expression statement can be rewritten by the implementation in any
114
- of the above ways because the same result will occur.
115
-
116
- — *end note*]
117
 
118
  A *constituent expression* is defined as follows:
119
 
120
  - The constituent expression of an expression is that expression.
121
  - The constituent expressions of a *braced-init-list* or of a (possibly
@@ -136,40 +27,42 @@ B b = { 5, { 1+1 } };
136
  The constituent expressions of the *initializer* used for the
137
  initialization of `b` are `5` and `1+1`.
138
 
139
  — *end example*]
140
 
141
- The *immediate subexpressions* of an expression `e` are
142
 
143
- - the constituent expressions of `e`’s operands (Clause [[expr]]),
144
- - any function call that `e` implicitly invokes,
145
- - if `e` is a *lambda-expression* ([[expr.prim.lambda]]), the
146
- initialization of the entities captured by copy and the constituent
147
- expressions of the *initializer* of the *init-capture*s,
148
- - if `e` is a function call ([[expr.call]]) or implicitly invokes a
149
- function, the constituent expressions of each default argument (
150
- [[dcl.fct.default]]) used in the call, or
151
- - if `e` creates an aggregate object ([[dcl.init.aggr]]), the
152
- constituent expressions of each default member initializer (
153
- [[class.mem]]) used in the initialization.
154
 
155
- A *subexpression* of an expression `e` is an immediate subexpression of
156
- `e` or a subexpression of an immediate subexpression of `e`.
157
 
158
- [*Note 4*: Expressions appearing in the *compound-statement* of a
159
  *lambda-expression* are not subexpressions of the
160
  *lambda-expression*. — *end note*]
161
 
162
  A *full-expression* is
163
 
164
- - an unevaluated operand (Clause [[expr]]),
165
- - a *constant-expression* ([[expr.const]]),
166
- - an *init-declarator* (Clause [[dcl.decl]]) or a *mem-initializer* (
167
- [[class.base.init]]), including the constituent expressions of the
 
168
  initializer,
169
  - an invocation of a destructor generated at the end of the lifetime of
170
- an object other than a temporary object ([[class.temporary]]), or
 
171
  - an expression that is not a subexpression of another expression and
172
  that is not otherwise part of a full-expression.
173
 
174
  If a language construct is defined to produce an implicit call of a
175
  function, a use of the language construct is considered to be an
@@ -190,19 +83,18 @@ struct S {
190
  ~S() noexcept(false) { }
191
  private:
192
  int I;
193
  };
194
 
195
- S s1(1); // full-expression is call of S::S(int)
196
  void f() {
197
- S s2 = 2; // full-expression is call of S::S(int)
198
- if (S(3).v()) // full-expression includes lvalue-to-rvalue and
199
- // int to bool conversions, performed before
200
- // temporary is deleted at end of full-expression
201
  { }
202
- bool b = noexcept(S()); // exception specification of destructor of S
203
- // considered for noexcept
204
  // full-expression is destruction of s2 at end of block
205
  }
206
  struct B {
207
  B(S = S(0));
208
  };
@@ -210,18 +102,18 @@ struct B {
210
  // including the destruction of temporaries
211
  ```
212
 
213
  — *end example*]
214
 
215
- [*Note 5*: The evaluation of a full-expression can include the
216
  evaluation of subexpressions that are not lexically part of the
217
  full-expression. For example, subexpressions involved in evaluating
218
- default arguments ([[dcl.fct.default]]) are considered to be created in
219
  the expression that calls the function, not the expression that defines
220
  the default argument. — *end note*]
221
 
222
- Reading an object designated by a `volatile` glvalue ([[basic.lval]]),
223
  modifying an object, calling a library I/O function, or calling a
224
  function that does any of those operations are all *side effects*, which
225
  are changes in the state of the execution environment. *Evaluation* of
226
  an expression (or a subexpression) in general includes both value
227
  computations (including determining the identity of an object for
@@ -231,64 +123,63 @@ library I/O function returns or an access through a volatile glvalue is
231
  evaluated the side effect is considered complete, even though some
232
  external actions implied by the call (such as the I/O itself) or by the
233
  `volatile` access may not have completed yet.
234
 
235
  *Sequenced before* is an asymmetric, transitive, pair-wise relation
236
- between evaluations executed by a single thread (
237
- [[intro.multithread]]), which induces a partial order among those
238
- evaluations. Given any two evaluations *A* and *B*, if *A* is sequenced
239
- before *B* (or, equivalently, *B* is *sequenced after* *A*), then the
240
- execution of *A* shall precede the execution of *B*. If *A* is not
241
- sequenced before *B* and *B* is not sequenced before *A*, then *A* and
242
- *B* are *unsequenced*.
243
 
244
- [*Note 6*: The execution of unsequenced evaluations can
245
  overlap. — *end note*]
246
 
247
  Evaluations *A* and *B* are *indeterminately sequenced* when either *A*
248
  is sequenced before *B* or *B* is sequenced before *A*, but it is
249
  unspecified which.
250
 
251
- [*Note 7*: Indeterminately sequenced evaluations cannot overlap, but
252
  either could be executed first. — *end note*]
253
 
254
  An expression *X* is said to be sequenced before an expression *Y* if
255
  every value computation and every side effect associated with the
256
  expression *X* is sequenced before every value computation and every
257
  side effect associated with the expression *Y*.
258
 
259
  Every value computation and side effect associated with a
260
  full-expression is sequenced before every value computation and side
261
- effect associated with the next full-expression to be evaluated.[^9]
262
 
263
  Except where noted, evaluations of operands of individual operators and
264
  of subexpressions of individual expressions are unsequenced.
265
 
266
- [*Note 8*: In an expression that is evaluated more than once during the
267
  execution of a program, unsequenced and indeterminately sequenced
268
  evaluations of its subexpressions need not be performed consistently in
269
  different evaluations. — *end note*]
270
 
271
  The value computations of the operands of an operator are sequenced
272
  before the value computation of the result of the operator. If a side
273
- effect on a memory location ([[intro.memory]]) is unsequenced relative
274
- to either another side effect on the same memory location or a value
275
  computation using the value of any object in the same memory location,
276
- and they are not potentially concurrent ([[intro.multithread]]), the
277
  behavior is undefined.
278
 
279
- [*Note 9*: The next section imposes similar, but more complex
280
  restrictions on potentially concurrent computations. — *end note*]
281
 
282
  [*Example 3*:
283
 
284
  ``` cpp
285
  void g(int i) {
286
  i = 7, i++, i++; // i becomes 9
287
 
288
  i = i++ + 1; // the value of i is incremented
289
- i = i++ + i; // the behavior is undefined
290
  i = i + 1; // the value of i is incremented
291
  }
292
  ```
293
 
294
  — *end example*]
@@ -299,21 +190,21 @@ expression, or with the postfix expression designating the called
299
  function, is sequenced before execution of every expression or statement
300
  in the body of the called function. For each function invocation *F*,
301
  for every evaluation *A* that occurs within *F* and every evaluation *B*
302
  that does not occur within *F* but is evaluated on the same thread and
303
  as part of the same signal handler (if any), either *A* is sequenced
304
- before *B* or *B* is sequenced before *A*.[^10]
305
 
306
- [*Note 10*: If *A* and *B* would not otherwise be sequenced then they
307
  are indeterminately sequenced. — *end note*]
308
 
309
  Several contexts in C++ cause evaluation of a function call, even though
310
  no corresponding function call syntax appears in the translation unit.
311
 
312
  [*Example 4*: Evaluation of a *new-expression* invokes one or more
313
  allocation and constructor functions; see  [[expr.new]]. For another
314
- example, invocation of a conversion function ([[class.conv.fct]]) can
315
  arise in contexts in which no function call syntax
316
  appears. — *end example*]
317
 
318
  The sequencing constraints on the execution of the called function (as
319
  described above) are features of the function calls as evaluated,
@@ -321,9 +212,9 @@ whatever the syntax of the expression that calls the function might be.
321
 
322
  If a signal handler is executed as a result of a call to the
323
  `std::raise` function, then the execution of the handler is sequenced
324
  after the invocation of the `std::raise` function and before its return.
325
 
326
- [*Note 11*: When a signal is received for another reason, the execution
327
  of the signal handler is usually unsequenced with respect to the rest of
328
  the program. — *end note*]
329
 
 
1
+ ### Sequential execution <a id="intro.execution">[[intro.execution]]</a>
2
 
3
+ An instance of each object with automatic storage duration
4
+ [[basic.stc.auto]] is associated with each entry into its block. Such an
5
+ object exists and retains its last-stored value during the execution of
6
+ the block and while the block is suspended (by a call of a function,
7
+ suspension of a coroutine [[expr.await]], or receipt of a signal).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  A *constituent expression* is defined as follows:
10
 
11
  - The constituent expression of an expression is that expression.
12
  - The constituent expressions of a *braced-init-list* or of a (possibly
 
27
  The constituent expressions of the *initializer* used for the
28
  initialization of `b` are `5` and `1+1`.
29
 
30
  — *end example*]
31
 
32
+ The *immediate subexpressions* of an expression E are
33
 
34
+ - the constituent expressions of E’s operands [[expr.prop]],
35
+ - any function call that E implicitly invokes,
36
+ - if E is a *lambda-expression* [[expr.prim.lambda]], the initialization
37
+ of the entities captured by copy and the constituent expressions of
38
+ the *initializer* of the *init-capture*s,
39
+ - if E is a function call [[expr.call]] or implicitly invokes a
40
+ function, the constituent expressions of each default argument
41
+ [[dcl.fct.default]] used in the call, or
42
+ - if E creates an aggregate object [[dcl.init.aggr]], the constituent
43
+ expressions of each default member initializer [[class.mem]] used in
44
+ the initialization.
45
 
46
+ A *subexpression* of an expression E is an immediate subexpression of E
47
+ or a subexpression of an immediate subexpression of E.
48
 
49
+ [*Note 1*: Expressions appearing in the *compound-statement* of a
50
  *lambda-expression* are not subexpressions of the
51
  *lambda-expression*. — *end note*]
52
 
53
  A *full-expression* is
54
 
55
+ - an unevaluated operand [[expr.prop]],
56
+ - a *constant-expression* [[expr.const]],
57
+ - an immediate invocation [[expr.const]],
58
+ - an *init-declarator* [[dcl.decl]] or a *mem-initializer*
59
+ [[class.base.init]], including the constituent expressions of the
60
  initializer,
61
  - an invocation of a destructor generated at the end of the lifetime of
62
+ an object other than a temporary object [[class.temporary]] whose
63
+ lifetime has not been extended, or
64
  - an expression that is not a subexpression of another expression and
65
  that is not otherwise part of a full-expression.
66
 
67
  If a language construct is defined to produce an implicit call of a
68
  function, a use of the language construct is considered to be an
 
83
  ~S() noexcept(false) { }
84
  private:
85
  int I;
86
  };
87
 
88
+ S s1(1); // full-expression comprises call of S::S(int)
89
  void f() {
90
+ S s2 = 2; // full-expression comprises call of S::S(int)
91
+ if (S(3).v()) // full-expression includes lvalue-to-rvalue and int to bool conversions,
92
+ // performed before temporary is deleted at end of full-expression
 
93
  { }
94
+ bool b = noexcept(S()); // exception specification of destructor of S considered for noexcept
95
+
96
  // full-expression is destruction of s2 at end of block
97
  }
98
  struct B {
99
  B(S = S(0));
100
  };
 
102
  // including the destruction of temporaries
103
  ```
104
 
105
  — *end example*]
106
 
107
+ [*Note 2*: The evaluation of a full-expression can include the
108
  evaluation of subexpressions that are not lexically part of the
109
  full-expression. For example, subexpressions involved in evaluating
110
+ default arguments [[dcl.fct.default]] are considered to be created in
111
  the expression that calls the function, not the expression that defines
112
  the default argument. — *end note*]
113
 
114
+ Reading an object designated by a `volatile` glvalue [[basic.lval]],
115
  modifying an object, calling a library I/O function, or calling a
116
  function that does any of those operations are all *side effects*, which
117
  are changes in the state of the execution environment. *Evaluation* of
118
  an expression (or a subexpression) in general includes both value
119
  computations (including determining the identity of an object for
 
123
  evaluated the side effect is considered complete, even though some
124
  external actions implied by the call (such as the I/O itself) or by the
125
  `volatile` access may not have completed yet.
126
 
127
  *Sequenced before* is an asymmetric, transitive, pair-wise relation
128
+ between evaluations executed by a single thread [[intro.multithread]],
129
+ which induces a partial order among those evaluations. Given any two
130
+ evaluations *A* and *B*, if *A* is sequenced before *B* (or,
131
+ equivalently, *B* is *sequenced after* *A*), then the execution of *A*
132
+ shall precede the execution of *B*. If *A* is not sequenced before *B*
133
+ and *B* is not sequenced before *A*, then *A* and *B* are *unsequenced*.
 
134
 
135
+ [*Note 3*: The execution of unsequenced evaluations can
136
  overlap. — *end note*]
137
 
138
  Evaluations *A* and *B* are *indeterminately sequenced* when either *A*
139
  is sequenced before *B* or *B* is sequenced before *A*, but it is
140
  unspecified which.
141
 
142
+ [*Note 4*: Indeterminately sequenced evaluations cannot overlap, but
143
  either could be executed first. — *end note*]
144
 
145
  An expression *X* is said to be sequenced before an expression *Y* if
146
  every value computation and every side effect associated with the
147
  expression *X* is sequenced before every value computation and every
148
  side effect associated with the expression *Y*.
149
 
150
  Every value computation and side effect associated with a
151
  full-expression is sequenced before every value computation and side
152
+ effect associated with the next full-expression to be evaluated.[^26]
153
 
154
  Except where noted, evaluations of operands of individual operators and
155
  of subexpressions of individual expressions are unsequenced.
156
 
157
+ [*Note 5*: In an expression that is evaluated more than once during the
158
  execution of a program, unsequenced and indeterminately sequenced
159
  evaluations of its subexpressions need not be performed consistently in
160
  different evaluations. — *end note*]
161
 
162
  The value computations of the operands of an operator are sequenced
163
  before the value computation of the result of the operator. If a side
164
+ effect on a memory location [[intro.memory]] is unsequenced relative to
165
+ either another side effect on the same memory location or a value
166
  computation using the value of any object in the same memory location,
167
+ and they are not potentially concurrent [[intro.multithread]], the
168
  behavior is undefined.
169
 
170
+ [*Note 6*: The next subclause imposes similar, but more complex
171
  restrictions on potentially concurrent computations. — *end note*]
172
 
173
  [*Example 3*:
174
 
175
  ``` cpp
176
  void g(int i) {
177
  i = 7, i++, i++; // i becomes 9
178
 
179
  i = i++ + 1; // the value of i is incremented
180
+ i = i++ + i; // undefined behavior
181
  i = i + 1; // the value of i is incremented
182
  }
183
  ```
184
 
185
  — *end example*]
 
190
  function, is sequenced before execution of every expression or statement
191
  in the body of the called function. For each function invocation *F*,
192
  for every evaluation *A* that occurs within *F* and every evaluation *B*
193
  that does not occur within *F* but is evaluated on the same thread and
194
  as part of the same signal handler (if any), either *A* is sequenced
195
+ before *B* or *B* is sequenced before *A*.[^27]
196
 
197
+ [*Note 7*: If *A* and *B* would not otherwise be sequenced then they
198
  are indeterminately sequenced. — *end note*]
199
 
200
  Several contexts in C++ cause evaluation of a function call, even though
201
  no corresponding function call syntax appears in the translation unit.
202
 
203
  [*Example 4*: Evaluation of a *new-expression* invokes one or more
204
  allocation and constructor functions; see  [[expr.new]]. For another
205
+ example, invocation of a conversion function [[class.conv.fct]] can
206
  arise in contexts in which no function call syntax
207
  appears. — *end example*]
208
 
209
  The sequencing constraints on the execution of the called function (as
210
  described above) are features of the function calls as evaluated,
 
212
 
213
  If a signal handler is executed as a result of a call to the
214
  `std::raise` function, then the execution of the handler is sequenced
215
  after the invocation of the `std::raise` function and before its return.
216
 
217
+ [*Note 8*: When a signal is received for another reason, the execution
218
  of the signal handler is usually unsequenced with respect to the rest of
219
  the program. — *end note*]
220