From Jason Turner

[basic.exec]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmptyjqvlkh/{from.md → to.md} +166 -552
tmp/tmptyjqvlkh/{from.md → to.md} RENAMED
@@ -64,16 +64,18 @@ or *initializer* E are
64
  A *full-expression* is
65
 
66
  - an unevaluated operand [[expr.context]],
67
  - a *constant-expression* [[expr.const]],
68
  - an immediate invocation [[expr.const]],
69
- - an *init-declarator* [[dcl.decl]] or a *mem-initializer*
 
70
  [[class.base.init]], including the constituent expressions of the
71
  initializer,
72
  - an invocation of a destructor generated at the end of the lifetime of
73
  an object other than a temporary object [[class.temporary]] whose
74
- lifetime has not been extended, or
 
75
  - an expression that is not a subexpression of another expression and
76
  that is not otherwise part of a full-expression.
77
 
78
  If a language construct is defined to produce an implicit call of a
79
  function, a use of the language construct is considered to be an
@@ -100,11 +102,11 @@ S s1(1); // full-expression comprises call of S::S(int)
100
  void f() {
101
  S s2 = 2; // full-expression comprises call of S::S(int)
102
  if (S(3).v()) // full-expression includes lvalue-to-rvalue and int to bool conversions,
103
  // performed before temporary is deleted at end of full-expression
104
  { }
105
- bool b = noexcept(S()); // exception specification of destructor of S considered for noexcept
106
 
107
  // full-expression is destruction of s2 at end of block
108
  }
109
  struct B {
110
  B(S = S(0));
@@ -121,19 +123,20 @@ full-expression. For example, subexpressions involved in evaluating
121
  default arguments [[dcl.fct.default]] are considered to be created in
122
  the expression that calls the function, not the expression that defines
123
  the default argument. — *end note*]
124
 
125
  Reading an object designated by a `volatile` glvalue [[basic.lval]],
126
- modifying an object, calling a library I/O function, or calling a
127
- function that does any of those operations are all *side effects*, which
128
- are changes in the state of the execution environment. *Evaluation* of
129
- an expression (or a subexpression) in general includes both value
 
130
  computations (including determining the identity of an object for
131
  glvalue evaluation and fetching a value previously assigned to an object
132
  for prvalue evaluation) and initiation of side effects. When a call to a
133
  library I/O function returns or an access through a volatile glvalue is
134
- evaluated the side effect is considered complete, even though some
135
  external actions implied by the call (such as the I/O itself) or by the
136
  `volatile` access may not have completed yet.
137
 
138
  *Sequenced before* is an asymmetric, transitive, pair-wise relation
139
  between evaluations executed by a single thread [[intro.multithread]],
@@ -158,29 +161,43 @@ every value computation and every side effect associated with the
158
  expression *X* is sequenced before every value computation and every
159
  side effect associated with the expression *Y*.
160
 
161
  Every value computation and side effect associated with a
162
  full-expression is sequenced before every value computation and side
163
- effect associated with the next full-expression to be evaluated.[^22]
164
 
165
  Except where noted, evaluations of operands of individual operators and
166
  of subexpressions of individual expressions are unsequenced.
167
 
168
  [*Note 5*: In an expression that is evaluated more than once during the
169
  execution of a program, unsequenced and indeterminately sequenced
170
  evaluations of its subexpressions need not be performed consistently in
171
  different evaluations. — *end note*]
172
 
173
  The value computations of the operands of an operator are sequenced
174
- before the value computation of the result of the operator. If a side
175
- effect on a memory location [[intro.memory]] is unsequenced relative to
176
- either another side effect on the same memory location or a value
177
- computation using the value of any object in the same memory location,
178
- and they are not potentially concurrent [[intro.multithread]], the
179
- behavior is undefined.
180
-
181
- [*Note 6*: The next subclause imposes similar, but more complex
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
  restrictions on potentially concurrent computations. — *end note*]
183
 
184
  [*Example 3*:
185
 
186
  ``` cpp
@@ -188,24 +205,35 @@ void g(int i) {
188
  i = 7, i++, i++; // i becomes 9
189
 
190
  i = i++ + 1; // the value of i is incremented
191
  i = i++ + i; // undefined behavior
192
  i = i + 1; // the value of i is incremented
 
 
 
193
  }
194
  ```
195
 
196
  — *end example*]
197
 
198
- When invoking a function (whether or not the function is inline), every
199
- argument expression and the postfix expression designating the called
200
- function are sequenced before every expression or statement in the body
201
- of the called function. For each function invocation or evaluation of an
202
- *await-expression* *F*, each evaluation that does not occur within *F*
203
- but is evaluated on the same thread and as part of the same signal
204
- handler (if any) is either sequenced before all evaluations that occur
205
- within *F* or sequenced after all evaluations that occur within
206
- *F*;[^23]
 
 
 
 
 
 
 
 
207
 
208
  if *F* invokes or resumes a coroutine [[expr.await]], only evaluations
209
  subsequent to the previous suspension (if any) and prior to the next
210
  suspension (if any) are considered to occur within *F*.
211
 
@@ -224,14 +252,19 @@ regardless of the syntax of the expression that calls the function.
224
 
225
  If a signal handler is executed as a result of a call to the
226
  `std::raise` function, then the execution of the handler is sequenced
227
  after the invocation of the `std::raise` function and before its return.
228
 
229
- [*Note 7*: When a signal is received for another reason, the execution
230
  of the signal handler is usually unsequenced with respect to the rest of
231
  the program. — *end note*]
232
 
 
 
 
 
 
233
  ### Multi-threaded executions and data races <a id="intro.multithread">[[intro.multithread]]</a>
234
 
235
  #### General <a id="intro.multithread.general">[[intro.multithread.general]]</a>
236
 
237
  A *thread of execution* (also known as a *thread*) is a single flow of
@@ -241,12 +274,12 @@ subsequently executed by the thread.
241
 
242
  [*Note 1*: When one thread creates another, the initial call to the
243
  top-level function of the new thread is executed by the new thread, not
244
  by the creating thread. — *end note*]
245
 
246
- Every thread in a program can potentially access every object and
247
- function in a program.[^24]
248
 
249
  Under a hosted implementation, a C++ program can have more than one
250
  thread running concurrently. The execution of each thread proceeds as
251
  defined by the remainder of this document. The execution of the entire
252
  program consists of an execution of all of its threads.
@@ -274,27 +307,37 @@ below.
274
  Much of this subclause is motivated by the desire to support atomic
275
  operations with explicit and detailed visibility constraints. However,
276
  it also implicitly supports a simpler view for more restricted
277
  programs. — *end note*]
278
 
279
- Two expression evaluations *conflict* if one of them modifies a memory
280
- location [[intro.memory]] and the other one reads or modifies the same
281
- memory location.
 
 
 
 
 
 
 
 
 
 
282
 
283
  The library defines a number of atomic operations [[atomics]] and
284
  operations on mutexes [[thread]] that are specially identified as
285
  synchronization operations. These operations play a special role in
286
  making assignments in one thread visible to another. A synchronization
287
- operation on one or more memory locations is either a consume operation,
288
- an acquire operation, a release operation, or both an acquire and
289
- release operation. A synchronization operation without an associated
290
- memory location is a fence and can be either an acquire fence, a release
291
- fence, or both an acquire and release fence. In addition, there are
292
- relaxed atomic operations, which are not synchronization operations, and
293
- atomic read-modify-write operations, which have special characteristics.
294
 
295
- [*Note 2*: For example, a call that acquires a mutex will perform an
296
  acquire operation on the locations comprising the mutex.
297
  Correspondingly, a call that releases the same mutex will perform a
298
  release operation on those same locations. Informally, performing a
299
  release operation on A forces prior side effects on other memory
300
  locations to become visible to other threads that later perform a
@@ -303,11 +346,11 @@ not synchronization operations even though, like synchronization
303
  operations, they cannot contribute to data races. — *end note*]
304
 
305
  All modifications to a particular atomic object M occur in some
306
  particular total order, called the *modification order* of M.
307
 
308
- [*Note 3*: There is a separate order for each atomic object. There is
309
  no requirement that these can be combined into a single total order for
310
  all objects. In general this will be impossible since different threads
311
  can observe modifications to different objects in inconsistent
312
  orders. — *end note*]
313
 
@@ -319,181 +362,107 @@ subsequent operation is an atomic read-modify-write operation.
319
  Certain library calls *synchronize with* other library calls performed
320
  by another thread. For example, an atomic store-release synchronizes
321
  with a load-acquire that takes its value from the store
322
  [[atomics.order]].
323
 
324
- [*Note 4*: Except in the specified cases, reading a later value does
325
  not necessarily ensure visibility as described below. Such a requirement
326
  would sometimes interfere with efficient implementation. — *end note*]
327
 
328
- [*Note 5*: The specifications of the synchronization operations define
329
  when one reads the value written by another. For atomic objects, the
330
  definition is clear. All operations on a given mutex occur in a single
331
  total order. Each mutex acquisition “reads the value written” by the
332
  last mutex release. — *end note*]
333
 
334
- An evaluation A *carries a dependency* to an evaluation B if
335
-
336
- - the value of A is used as an operand of B, unless:
337
- - B is an invocation of any specialization of `std::kill_dependency`
338
- [[atomics.order]], or
339
- - A is the left operand of a built-in logical (`&&`, see 
340
- [[expr.log.and]]) or logical (`||`, see  [[expr.log.or]]) operator,
341
- or
342
- - A is the left operand of a conditional (`?:`, see  [[expr.cond]])
343
- operator, or
344
- - A is the left operand of the built-in comma (`,`) operator
345
- [[expr.comma]];
346
-
347
- or
348
- - A writes a scalar object or bit-field M, B reads the value written by
349
- A from M, and A is sequenced before B, or
350
- - for some evaluation X, A carries a dependency to X, and X carries a
351
- dependency to B.
352
-
353
- [*Note 6*: “Carries a dependency to” is a subset of “is sequenced
354
- before”, and is similarly strictly intra-thread. — *end note*]
355
-
356
- An evaluation A is *dependency-ordered before* an evaluation B if
357
-
358
- - A performs a release operation on an atomic object M, and, in another
359
- thread, B performs a consume operation on M and reads the value
360
- written by A, or
361
- - for some evaluation X, A is dependency-ordered before X and X carries
362
- a dependency to B.
363
-
364
- [*Note 7*: The relation “is dependency-ordered before” is analogous to
365
- “synchronizes with”, but uses release/consume in place of
366
- release/acquire. — *end note*]
367
-
368
- An evaluation A *inter-thread happens before* an evaluation B if
369
-
370
- - A synchronizes with B, or
371
- - A is dependency-ordered before B, or
372
- - for some evaluation X
373
- - A synchronizes with X and X is sequenced before B, or
374
- - A is sequenced before X and X inter-thread happens before B, or
375
- - A inter-thread happens before X and X inter-thread happens before B.
376
-
377
- [*Note 8*: The “inter-thread happens before” relation describes
378
- arbitrary concatenations of “sequenced before”, “synchronizes with” and
379
- “dependency-ordered before” relationships, with two exceptions. The
380
- first exception is that a concatenation is not permitted to end with
381
- “dependency-ordered before” followed by “sequenced before”. The reason
382
- for this limitation is that a consume operation participating in a
383
- “dependency-ordered before” relationship provides ordering only with
384
- respect to operations to which this consume operation actually carries a
385
- dependency. The reason that this limitation applies only to the end of
386
- such a concatenation is that any subsequent release operation will
387
- provide the required ordering for a prior consume operation. The second
388
- exception is that a concatenation is not permitted to consist entirely
389
- of “sequenced before”. The reasons for this limitation are (1) to permit
390
- “inter-thread happens before” to be transitively closed and (2) the
391
- “happens before” relation, defined below, provides for relationships
392
- consisting entirely of “sequenced before”. — *end note*]
393
-
394
  An evaluation A *happens before* an evaluation B (or, equivalently, B
395
- *happens after* A) if:
396
-
397
- - A is sequenced before B, or
398
- - A inter-thread happens before B.
399
-
400
- The implementation shall ensure that no program execution demonstrates a
401
- cycle in the “happens before” relation.
402
-
403
- [*Note 9*: This cycle would otherwise be possible only through the use
404
- of consume operations. — *end note*]
405
-
406
- An evaluation A *simply happens before* an evaluation B if either
407
 
408
  - A is sequenced before B, or
409
  - A synchronizes with B, or
410
- - A simply happens before X and X simply happens before B.
411
 
412
- [*Note 10*: In the absence of consume operations, the happens before
413
- and simply happens before relations are identical. — *end note*]
414
 
415
  An evaluation A *strongly happens before* an evaluation D if, either
416
 
417
  - A is sequenced before D, or
418
  - A synchronizes with D, and both A and D are sequentially consistent
419
  atomic operations [[atomics.order]], or
420
  - there are evaluations B and C such that A is sequenced before B, B
421
- simply happens before C, and C is sequenced before D, or
422
  - there is an evaluation B such that A strongly happens before B, and B
423
  strongly happens before D.
424
 
425
- [*Note 11*: Informally, if A strongly happens before B, then A appears
426
- to be evaluated before B in all contexts. Strongly happens before
427
- excludes consume operations. — *end note*]
428
 
429
  A *visible side effect* A on a scalar object or bit-field M with respect
430
  to a value computation B of M satisfies the conditions:
431
 
432
  - A happens before B and
433
  - there is no other side effect X to M such that A happens before X and
434
  X happens before B.
435
 
436
  The value of a non-atomic scalar object or bit-field M, as determined by
437
- evaluation B, shall be the value stored by the visible side effect A.
438
 
439
- [*Note 12*: If there is ambiguity about which side effect to a
440
  non-atomic object or bit-field is visible, then the behavior is either
441
  unspecified or undefined. — *end note*]
442
 
443
- [*Note 13*: This states that operations on ordinary objects are not
444
  visibly reordered. This is not actually detectable without data races,
445
- but it is necessary to ensure that data races, as defined below, and
446
- with suitable restrictions on the use of atomics, correspond to data
447
- races in a simple interleaved (sequentially consistent)
448
- execution. — *end note*]
449
 
450
- The value of an atomic object M, as determined by evaluation B, shall be
451
- the value stored by some side effect A that modifies M, where B does not
452
- happen before A.
453
 
454
- [*Note 14*: The set of such side effects is also restricted by the rest
455
  of the rules described here, and in particular, by the coherence
456
  requirements below. — *end note*]
457
 
458
  If an operation A that modifies an atomic object M happens before an
459
- operation B that modifies M, then A shall be earlier than B in the
460
  modification order of M.
461
 
462
- [*Note 15*: This requirement is known as write-write
463
  coherence. — *end note*]
464
 
465
  If a value computation A of an atomic object M happens before a value
466
  computation B of M, and A takes its value from a side effect X on M,
467
- then the value computed by B shall either be the value stored by X or
468
- the value stored by a side effect Y on M, where Y follows X in the
469
  modification order of M.
470
 
471
- [*Note 16*: This requirement is known as read-read
472
  coherence. — *end note*]
473
 
474
  If a value computation A of an atomic object M happens before an
475
- operation B that modifies M, then A shall take its value from a side
476
- effect X on M, where X precedes B in the modification order of M.
477
 
478
- [*Note 17*: This requirement is known as read-write
479
  coherence. — *end note*]
480
 
481
  If a side effect X on an atomic object M happens before a value
482
- computation B of M, then the evaluation B shall take its value from X or
483
- from a side effect Y that follows X in the modification order of M.
484
 
485
- [*Note 18*: This requirement is known as write-read
486
  coherence. — *end note*]
487
 
488
- [*Note 19*: The four preceding coherence requirements effectively
489
  disallow compiler reordering of atomic operations to a single object,
490
  even if both operations are relaxed loads. This effectively makes the
491
  cache coherence guarantee provided by most hardware available to C++
492
  atomic operations. — *end note*]
493
 
494
- [*Note 20*: The value observed by a load of an atomic depends on the
495
  “happens before” relation, which depends on the values observed by loads
496
  of atomics. The intended reading is that there must exist an association
497
  of atomic loads with modifications they observe that, together with
498
  suitably chosen modification orders and the “happens before” relation
499
  derived as described above, satisfy the resulting constraints as imposed
@@ -509,67 +478,71 @@ The execution of a program contains a *data race* if it contains two
509
  potentially concurrent conflicting actions, at least one of which is not
510
  atomic, and neither happens before the other, except for the special
511
  case for signal handlers described below. Any such data race results in
512
  undefined behavior.
513
 
514
- [*Note 21*: It can be shown that programs that correctly use mutexes
515
  and `memory_order::seq_cst` operations to prevent all data races and use
516
  no other synchronization operations behave as if the operations executed
517
  by their constituent threads were simply interleaved, with each value
518
  computation of an object being taken from the last side effect on that
519
  object in that interleaving. This is normally referred to as “sequential
520
  consistency”. However, this applies only to data-race-free programs, and
521
  data-race-free programs cannot observe most program transformations that
522
  do not change single-threaded program semantics. In fact, most
523
- single-threaded program transformations continue to be allowed, since
524
- any program that behaves differently as a result has undefined
525
  behavior. — *end note*]
526
 
527
- Two accesses to the same object of type `volatile std::sig_atomic_t` do
528
- not result in a data race if both occur in the same thread, even if one
529
- or more occurs in a signal handler. For each signal handler invocation,
530
- evaluations performed by the thread invoking a signal handler can be
531
- divided into two groups A and B, such that no evaluations in B happen
532
- before evaluations in A, and the evaluations of such
533
- `volatile std::sig_atomic_t` objects take values as though all
534
- evaluations in A happened before the execution of the signal handler and
535
- the execution of the signal handler happened before all evaluations in
536
- B.
537
 
538
- [*Note 22*: Compiler transformations that introduce assignments to a
539
  potentially shared memory location that would not be modified by the
540
  abstract machine are generally precluded by this document, since such an
541
  assignment might overwrite another assignment by a different thread in
542
  cases in which an abstract machine execution would not have encountered
543
  a data race. This includes implementations of data member assignment
544
  that overwrite adjacent members in separate memory locations. Reordering
545
  of atomic loads in cases in which the atomics in question might alias is
546
  also generally precluded, since this could violate the coherence
547
  rules. — *end note*]
548
 
549
- [*Note 23*: Transformations that introduce a speculative read of a
550
- potentially shared memory location might not preserve the semantics of
551
- the C++ program as defined in this document, since they potentially
552
- introduce a data race. However, they are typically valid in the context
553
- of an optimizing compiler that targets a specific machine with
554
- well-defined semantics for data races. They would be invalid for a
555
  hypothetical machine that is not tolerant of races or provides hardware
556
  race detection. — *end note*]
557
 
558
  #### Forward progress <a id="intro.progress">[[intro.progress]]</a>
559
 
560
  The implementation may assume that any thread will eventually do one of
561
  the following:
562
 
563
  - terminate,
 
564
  - make a call to a library I/O function,
565
- - perform an access through a volatile glvalue, or
566
- - perform a synchronization operation or an atomic operation.
 
 
567
 
568
  [*Note 1*: This is intended to allow compiler transformations such as
569
- removal of empty loops, even when termination cannot be
570
- proven. *end note*]
 
571
 
572
  Executions of atomic functions that are either defined to be lock-free
573
  [[atomics.flag]] or indicated as lock-free [[atomics.lockfree]] are
574
  *lock-free executions*.
575
 
@@ -593,13 +566,14 @@ Executions of atomic functions that are either defined to be lock-free
593
 
594
  During the execution of a thread of execution, each of the following is
595
  termed an *execution step*:
596
 
597
  - termination of the thread of execution,
598
- - performing an access through a volatile glvalue, or
599
- - completion of a call to a library I/O function, a synchronization
600
- operation, or an atomic operation.
 
601
 
602
  An invocation of a standard library function that blocks [[defns.block]]
603
  is considered to continuously execute execution steps while waiting for
604
  the condition that it blocks on to be satisfied.
605
 
@@ -621,12 +595,12 @@ concurrent threads that are not blocked in a standard library function
621
 
622
  For a thread of execution providing *concurrent forward progress
623
  guarantees*, the implementation ensures that the thread will eventually
624
  make progress for as long as it has not terminated.
625
 
626
- [*Note 5*: This is required regardless of whether or not other threads
627
- of execution (if any) have been or are making progress. To eventually
628
  fulfill this requirement means that this will happen in an unspecified
629
  but finite amount of time. — *end note*]
630
 
631
  It is *implementation-defined* whether the implementation-created thread
632
  of execution that executes `main` [[basic.start.main]] and the threads
@@ -739,24 +713,24 @@ function parameter is called `argv`, where `argc` shall be the number of
739
  arguments passed to the program from the environment in which the
740
  program is run. If `argc` is nonzero these arguments shall be supplied
741
  in `argv[0]` through `argv[argc - 1]` as pointers to the initial
742
  characters of null-terminated multibyte strings (NTMBSs)
743
  [[multibyte.strings]] and `argv[0]` shall be the pointer to the initial
744
- character of a NTMBS that represents the name used to invoke the program
745
- or `""`. The value of `argc` shall be non-negative. The value of
746
  `argv[argc]` shall be 0.
747
 
748
  *Recommended practice:* Any further (optional) parameters should be
749
  added after `argv`.
750
 
751
- The function `main` shall not be used within a program. The linkage
752
  [[basic.link]] of `main` is *implementation-defined*. A program that
753
  defines `main` as deleted or that declares `main` to be `inline`,
754
  `static`, `constexpr`, or `consteval` is ill-formed. The function `main`
755
  shall not be a coroutine [[dcl.fct.def.coroutine]]. The `main` function
756
- shall not be declared with a *linkage-specification* [[dcl.link]]. A
757
- program that declares
758
 
759
  - a variable `main` that belongs to the global scope, or
760
  - a function `main` that belongs to the global scope and is attached to
761
  a named module, or
762
  - a function template `main` that belongs to the global scope, or
@@ -772,29 +746,29 @@ calling the function `std::exit(int)` [[support.start.term]]) does not
772
  destroy any objects with automatic storage duration [[class.dtor]]. If
773
  `std::exit` is invoked during the destruction of an object with static
774
  or thread storage duration, the program has undefined behavior.
775
 
776
  A `return` statement [[stmt.return]] in `main` has the effect of leaving
777
- the main function (destroying any objects with automatic storage
778
- duration) and calling `std::exit` with the return value as the argument.
779
- If control flows off the end of the *compound-statement* of `main`, the
780
- effect is equivalent to a `return` with operand `0` (see also
781
- [[except.handle]]).
782
 
783
  #### Static initialization <a id="basic.start.static">[[basic.start.static]]</a>
784
 
785
  Variables with static storage duration are initialized as a consequence
786
  of program initiation. Variables with thread storage duration are
787
  initialized as a consequence of thread execution. Within each of these
788
  phases of initiation, initialization occurs as follows.
789
 
790
- *Constant initialization* is performed if a variable or temporary object
791
- with static or thread storage duration is constant-initialized
792
- [[expr.const]]. If constant initialization is not performed, a variable
793
- with static storage duration [[basic.stc.static]] or thread storage
794
- duration [[basic.stc.thread]] is zero-initialized [[dcl.init]].
795
- Together, zero-initialization and constant initialization are called
796
  *static initialization*; all other initialization is
797
  *dynamic initialization*. All static initialization strongly happens
798
  before [[intro.races]] any dynamic initialization.
799
 
800
  [*Note 1*: The dynamic initialization of non-block variables is
@@ -887,11 +861,11 @@ thread storage duration variable.
887
  It is *implementation-defined* whether the dynamic initialization of a
888
  non-block non-inline variable with static storage duration is sequenced
889
  before the first statement of `main` or is deferred. If it is deferred,
890
  it strongly happens before any non-initialization odr-use of any
891
  non-inline function or non-inline variable defined in the same
892
- translation unit as the variable to be initialized.[^25]
893
 
894
  It is *implementation-defined* in which threads and at which points in
895
  the program such deferred dynamic initialization occurs.
896
 
897
  *Recommended practice:* An implementation should choose such points in a
@@ -1027,365 +1001,5 @@ static-storage-duration objects. — *end note*]
1027
 
1028
  Calling the function `std::abort()` declared in `<cstdlib>` terminates
1029
  the program without executing any destructors and without calling the
1030
  functions passed to `std::atexit()` or `std::at_quick_exit()`.
1031
 
1032
- <!-- Link reference definitions -->
1033
- [allocator.members]: mem.md#allocator.members
1034
- [allocator.traits.members]: mem.md#allocator.traits.members
1035
- [atomics]: thread.md#atomics
1036
- [atomics.flag]: thread.md#atomics.flag
1037
- [atomics.lockfree]: thread.md#atomics.lockfree
1038
- [atomics.order]: thread.md#atomics.order
1039
- [bad.alloc]: support.md#bad.alloc
1040
- [basic]: #basic
1041
- [basic.align]: #basic.align
1042
- [basic.compound]: #basic.compound
1043
- [basic.def]: #basic.def
1044
- [basic.def.odr]: #basic.def.odr
1045
- [basic.exec]: #basic.exec
1046
- [basic.extended.fp]: #basic.extended.fp
1047
- [basic.fundamental]: #basic.fundamental
1048
- [basic.fundamental.width]: #basic.fundamental.width
1049
- [basic.indet]: #basic.indet
1050
- [basic.life]: #basic.life
1051
- [basic.link]: #basic.link
1052
- [basic.lookup]: #basic.lookup
1053
- [basic.lookup.argdep]: #basic.lookup.argdep
1054
- [basic.lookup.elab]: #basic.lookup.elab
1055
- [basic.lookup.general]: #basic.lookup.general
1056
- [basic.lookup.qual]: #basic.lookup.qual
1057
- [basic.lookup.qual.general]: #basic.lookup.qual.general
1058
- [basic.lookup.udir]: #basic.lookup.udir
1059
- [basic.lookup.unqual]: #basic.lookup.unqual
1060
- [basic.lval]: expr.md#basic.lval
1061
- [basic.memobj]: #basic.memobj
1062
- [basic.namespace]: dcl.md#basic.namespace
1063
- [basic.pre]: #basic.pre
1064
- [basic.scope]: #basic.scope
1065
- [basic.scope.block]: #basic.scope.block
1066
- [basic.scope.class]: #basic.scope.class
1067
- [basic.scope.enum]: #basic.scope.enum
1068
- [basic.scope.lambda]: #basic.scope.lambda
1069
- [basic.scope.namespace]: #basic.scope.namespace
1070
- [basic.scope.param]: #basic.scope.param
1071
- [basic.scope.pdecl]: #basic.scope.pdecl
1072
- [basic.scope.scope]: #basic.scope.scope
1073
- [basic.scope.temp]: #basic.scope.temp
1074
- [basic.start]: #basic.start
1075
- [basic.start.dynamic]: #basic.start.dynamic
1076
- [basic.start.main]: #basic.start.main
1077
- [basic.start.static]: #basic.start.static
1078
- [basic.start.term]: #basic.start.term
1079
- [basic.stc]: #basic.stc
1080
- [basic.stc.auto]: #basic.stc.auto
1081
- [basic.stc.dynamic]: #basic.stc.dynamic
1082
- [basic.stc.dynamic.allocation]: #basic.stc.dynamic.allocation
1083
- [basic.stc.dynamic.deallocation]: #basic.stc.dynamic.deallocation
1084
- [basic.stc.dynamic.general]: #basic.stc.dynamic.general
1085
- [basic.stc.general]: #basic.stc.general
1086
- [basic.stc.inherit]: #basic.stc.inherit
1087
- [basic.stc.static]: #basic.stc.static
1088
- [basic.stc.thread]: #basic.stc.thread
1089
- [basic.type.qualifier]: #basic.type.qualifier
1090
- [basic.type.qualifier.rel]: #basic.type.qualifier.rel
1091
- [basic.types]: #basic.types
1092
- [basic.types.general]: #basic.types.general
1093
- [bit.cast]: utilities.md#bit.cast
1094
- [c.malloc]: mem.md#c.malloc
1095
- [class]: class.md#class
1096
- [class.abstract]: class.md#class.abstract
1097
- [class.access]: class.md#class.access
1098
- [class.access.base]: class.md#class.access.base
1099
- [class.base.init]: class.md#class.base.init
1100
- [class.bit]: class.md#class.bit
1101
- [class.cdtor]: class.md#class.cdtor
1102
- [class.conv.fct]: class.md#class.conv.fct
1103
- [class.copy.assign]: class.md#class.copy.assign
1104
- [class.copy.ctor]: class.md#class.copy.ctor
1105
- [class.copy.elision]: class.md#class.copy.elision
1106
- [class.default.ctor]: class.md#class.default.ctor
1107
- [class.derived]: class.md#class.derived
1108
- [class.dtor]: class.md#class.dtor
1109
- [class.free]: class.md#class.free
1110
- [class.friend]: class.md#class.friend
1111
- [class.mem]: class.md#class.mem
1112
- [class.member.lookup]: #class.member.lookup
1113
- [class.mfct]: class.md#class.mfct
1114
- [class.mfct.non.static]: class.md#class.mfct.non.static
1115
- [class.name]: class.md#class.name
1116
- [class.pre]: class.md#class.pre
1117
- [class.prop]: class.md#class.prop
1118
- [class.qual]: #class.qual
1119
- [class.spaceship]: class.md#class.spaceship
1120
- [class.static]: class.md#class.static
1121
- [class.static.data]: class.md#class.static.data
1122
- [class.temporary]: #class.temporary
1123
- [class.union]: class.md#class.union
1124
- [class.union.anon]: class.md#class.union.anon
1125
- [class.virtual]: class.md#class.virtual
1126
- [conv]: expr.md#conv
1127
- [conv.array]: expr.md#conv.array
1128
- [conv.func]: expr.md#conv.func
1129
- [conv.integral]: expr.md#conv.integral
1130
- [conv.lval]: expr.md#conv.lval
1131
- [conv.mem]: expr.md#conv.mem
1132
- [conv.prom]: expr.md#conv.prom
1133
- [conv.ptr]: expr.md#conv.ptr
1134
- [conv.rank]: #conv.rank
1135
- [conv.rval]: expr.md#conv.rval
1136
- [cpp.predefined]: cpp.md#cpp.predefined
1137
- [cstddef.syn]: support.md#cstddef.syn
1138
- [cstring.syn]: strings.md#cstring.syn
1139
- [dcl.align]: dcl.md#dcl.align
1140
- [dcl.array]: dcl.md#dcl.array
1141
- [dcl.attr]: dcl.md#dcl.attr
1142
- [dcl.attr.nouniqueaddr]: dcl.md#dcl.attr.nouniqueaddr
1143
- [dcl.constexpr]: dcl.md#dcl.constexpr
1144
- [dcl.dcl]: dcl.md#dcl.dcl
1145
- [dcl.decl]: dcl.md#dcl.decl
1146
- [dcl.enum]: dcl.md#dcl.enum
1147
- [dcl.fct]: dcl.md#dcl.fct
1148
- [dcl.fct.def]: dcl.md#dcl.fct.def
1149
- [dcl.fct.def.coroutine]: dcl.md#dcl.fct.def.coroutine
1150
- [dcl.fct.def.general]: dcl.md#dcl.fct.def.general
1151
- [dcl.fct.default]: dcl.md#dcl.fct.default
1152
- [dcl.init]: dcl.md#dcl.init
1153
- [dcl.init.aggr]: dcl.md#dcl.init.aggr
1154
- [dcl.init.list]: dcl.md#dcl.init.list
1155
- [dcl.init.ref]: dcl.md#dcl.init.ref
1156
- [dcl.link]: dcl.md#dcl.link
1157
- [dcl.meaning]: dcl.md#dcl.meaning
1158
- [dcl.mptr]: dcl.md#dcl.mptr
1159
- [dcl.name]: dcl.md#dcl.name
1160
- [dcl.pre]: dcl.md#dcl.pre
1161
- [dcl.ptr]: dcl.md#dcl.ptr
1162
- [dcl.ref]: dcl.md#dcl.ref
1163
- [dcl.spec]: dcl.md#dcl.spec
1164
- [dcl.spec.auto]: dcl.md#dcl.spec.auto
1165
- [dcl.stc]: dcl.md#dcl.stc
1166
- [dcl.struct.bind]: dcl.md#dcl.struct.bind
1167
- [dcl.type.decltype]: dcl.md#dcl.type.decltype
1168
- [dcl.type.elab]: dcl.md#dcl.type.elab
1169
- [dcl.typedef]: dcl.md#dcl.typedef
1170
- [defns.block]: intro.md#defns.block
1171
- [depr.local]: future.md#depr.local
1172
- [depr.static.constexpr]: future.md#depr.static.constexpr
1173
- [diff.cpp11.basic]: compatibility.md#diff.cpp11.basic
1174
- [enum.udecl]: dcl.md#enum.udecl
1175
- [except.handle]: except.md#except.handle
1176
- [except.pre]: except.md#except.pre
1177
- [except.spec]: except.md#except.spec
1178
- [except.terminate]: except.md#except.terminate
1179
- [except.throw]: except.md#except.throw
1180
- [expr.add]: expr.md#expr.add
1181
- [expr.alignof]: expr.md#expr.alignof
1182
- [expr.arith.conv]: expr.md#expr.arith.conv
1183
- [expr.ass]: expr.md#expr.ass
1184
- [expr.await]: expr.md#expr.await
1185
- [expr.call]: expr.md#expr.call
1186
- [expr.cast]: expr.md#expr.cast
1187
- [expr.comma]: expr.md#expr.comma
1188
- [expr.cond]: expr.md#expr.cond
1189
- [expr.const]: expr.md#expr.const
1190
- [expr.const.cast]: expr.md#expr.const.cast
1191
- [expr.context]: expr.md#expr.context
1192
- [expr.delete]: expr.md#expr.delete
1193
- [expr.dynamic.cast]: expr.md#expr.dynamic.cast
1194
- [expr.eq]: expr.md#expr.eq
1195
- [expr.log.and]: expr.md#expr.log.and
1196
- [expr.log.or]: expr.md#expr.log.or
1197
- [expr.mptr.oper]: expr.md#expr.mptr.oper
1198
- [expr.new]: expr.md#expr.new
1199
- [expr.pre]: expr.md#expr.pre
1200
- [expr.prim.id]: expr.md#expr.prim.id
1201
- [expr.prim.id.qual]: expr.md#expr.prim.id.qual
1202
- [expr.prim.id.unqual]: expr.md#expr.prim.id.unqual
1203
- [expr.prim.lambda]: expr.md#expr.prim.lambda
1204
- [expr.prim.lambda.capture]: expr.md#expr.prim.lambda.capture
1205
- [expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
1206
- [expr.prim.this]: expr.md#expr.prim.this
1207
- [expr.prop]: expr.md#expr.prop
1208
- [expr.ref]: expr.md#expr.ref
1209
- [expr.reinterpret.cast]: expr.md#expr.reinterpret.cast
1210
- [expr.rel]: expr.md#expr.rel
1211
- [expr.sizeof]: expr.md#expr.sizeof
1212
- [expr.static.cast]: expr.md#expr.static.cast
1213
- [expr.sub]: expr.md#expr.sub
1214
- [expr.type.conv]: expr.md#expr.type.conv
1215
- [expr.typeid]: expr.md#expr.typeid
1216
- [expr.unary.op]: expr.md#expr.unary.op
1217
- [get.new.handler]: support.md#get.new.handler
1218
- [headers]: library.md#headers
1219
- [intro.execution]: #intro.execution
1220
- [intro.memory]: #intro.memory
1221
- [intro.multithread]: #intro.multithread
1222
- [intro.multithread.general]: #intro.multithread.general
1223
- [intro.object]: #intro.object
1224
- [intro.progress]: #intro.progress
1225
- [intro.races]: #intro.races
1226
- [lex.charset]: lex.md#lex.charset
1227
- [lex.fcon]: lex.md#lex.fcon
1228
- [lex.name]: lex.md#lex.name
1229
- [lex.separate]: lex.md#lex.separate
1230
- [module.context]: module.md#module.context
1231
- [module.global.frag]: module.md#module.global.frag
1232
- [module.interface]: module.md#module.interface
1233
- [module.reach]: module.md#module.reach
1234
- [module.unit]: module.md#module.unit
1235
- [multibyte.strings]: library.md#multibyte.strings
1236
- [namespace.def]: dcl.md#namespace.def
1237
- [namespace.qual]: #namespace.qual
1238
- [namespace.udecl]: dcl.md#namespace.udecl
1239
- [namespace.udir]: dcl.md#namespace.udir
1240
- [namespace.unnamed]: dcl.md#namespace.unnamed
1241
- [new.delete]: support.md#new.delete
1242
- [new.delete.array]: support.md#new.delete.array
1243
- [new.delete.placement]: support.md#new.delete.placement
1244
- [new.delete.single]: support.md#new.delete.single
1245
- [new.handler]: support.md#new.handler
1246
- [new.syn]: support.md#new.syn
1247
- [obj.lifetime]: mem.md#obj.lifetime
1248
- [over]: over.md#over
1249
- [over.literal]: over.md#over.literal
1250
- [over.match]: over.md#over.match
1251
- [over.match.funcs]: over.md#over.match.funcs
1252
- [over.oper]: over.md#over.oper
1253
- [over.over]: over.md#over.over
1254
- [ptr.align]: mem.md#ptr.align
1255
- [ptr.launder]: support.md#ptr.launder
1256
- [replacement.functions]: library.md#replacement.functions
1257
- [special]: class.md#special
1258
- [std.modules]: library.md#std.modules
1259
- [stdfloat.syn]: support.md#stdfloat.syn
1260
- [stmt.block]: stmt.md#stmt.block
1261
- [stmt.dcl]: stmt.md#stmt.dcl
1262
- [stmt.expr]: stmt.md#stmt.expr
1263
- [stmt.if]: stmt.md#stmt.if
1264
- [stmt.iter]: stmt.md#stmt.iter
1265
- [stmt.pre]: stmt.md#stmt.pre
1266
- [stmt.ranged]: stmt.md#stmt.ranged
1267
- [stmt.return]: stmt.md#stmt.return
1268
- [stmt.select]: stmt.md#stmt.select
1269
- [support.dynamic]: support.md#support.dynamic
1270
- [support.runtime]: support.md#support.runtime
1271
- [support.start.term]: support.md#support.start.term
1272
- [support.types]: support.md#support.types
1273
- [temp.concept]: temp.md#temp.concept
1274
- [temp.deduct.guide]: temp.md#temp.deduct.guide
1275
- [temp.dep]: temp.md#temp.dep
1276
- [temp.dep.candidate]: temp.md#temp.dep.candidate
1277
- [temp.dep.constexpr]: temp.md#temp.dep.constexpr
1278
- [temp.dep.type]: temp.md#temp.dep.type
1279
- [temp.expl.spec]: temp.md#temp.expl.spec
1280
- [temp.explicit]: temp.md#temp.explicit
1281
- [temp.friend]: temp.md#temp.friend
1282
- [temp.local]: temp.md#temp.local
1283
- [temp.names]: temp.md#temp.names
1284
- [temp.over]: temp.md#temp.over
1285
- [temp.over.link]: temp.md#temp.over.link
1286
- [temp.param]: temp.md#temp.param
1287
- [temp.point]: temp.md#temp.point
1288
- [temp.pre]: temp.md#temp.pre
1289
- [temp.res]: temp.md#temp.res
1290
- [temp.spec]: temp.md#temp.spec
1291
- [temp.spec.partial]: temp.md#temp.spec.partial
1292
- [temp.type]: temp.md#temp.type
1293
- [term.incomplete.type]: #term.incomplete.type
1294
- [term.odr.use]: #term.odr.use
1295
- [term.unevaluated.operand]: expr.md#term.unevaluated.operand
1296
- [thread]: thread.md#thread
1297
- [thread.jthread.class]: thread.md#thread.jthread.class
1298
- [thread.thread.class]: thread.md#thread.thread.class
1299
- [thread.threads]: thread.md#thread.threads
1300
-
1301
- [^1]: Appearing inside the brace-enclosed *declaration-seq* in a
1302
- *linkage-specification* does not affect whether a declaration is a
1303
- definition.
1304
-
1305
- [^2]: An implementation is not required to call allocation and
1306
- deallocation functions from constructors or destructors; however,
1307
- this is a permissible implementation technique.
1308
-
1309
- [^3]: An implicit object parameter [[over.match.funcs]] is not part of
1310
- the parameter-type-list.
1311
-
1312
- [^4]: Lookups in which function names are ignored include names
1313
- appearing in a *nested-name-specifier*, an
1314
- *elaborated-type-specifier*, or a *base-specifier*.
1315
-
1316
- [^5]: Unicode® is a registered trademark of Unicode, Inc. This
1317
- information is given for the convenience of users of this document
1318
- and does not constitute an endorsement by ISO or IEC of this
1319
- product.
1320
-
1321
- [^6]: The number of bits in a byte is reported by the macro `CHAR_BIT`
1322
- in the header `<climits>`.
1323
-
1324
- [^7]: Under the “as-if” rule an implementation is allowed to store two
1325
- objects at the same machine address or not store an object at all if
1326
- the program cannot observe the difference [[intro.execution]].
1327
-
1328
- [^8]: For example, before the dynamic initialization of an object with
1329
- static storage duration [[basic.start.dynamic]].
1330
-
1331
- [^9]: That is, an object for which a destructor will be called
1332
- implicitly—upon exit from the block for an object with automatic
1333
- storage duration, upon exit from the thread for an object with
1334
- thread storage duration, or upon exit from the program for an object
1335
- with static storage duration.
1336
-
1337
- [^10]: Some implementations might define that copying an invalid pointer
1338
- value causes a system-generated runtime fault.
1339
-
1340
- [^11]: The intent is to have `operator new()` implementable by calling
1341
- `std::malloc()` or `std::calloc()`, so the rules are substantially
1342
- the same. C++ differs from C in requiring a zero request to return a
1343
- non-null pointer.
1344
-
1345
- [^12]: The global `operator delete(void*, std::size_t)` precludes use of
1346
- an allocation function `void operator new(std::size_t, std::size_t)`
1347
- as a placement allocation function [[diff.cpp11.basic]].
1348
-
1349
- [^13]: The same rules apply to initialization of an `initializer_list`
1350
- object [[dcl.init.list]] with its underlying temporary array.
1351
-
1352
- [^14]: By using, for example, the library functions [[headers]]
1353
- `std::memcpy` or `std::memmove`.
1354
-
1355
- [^15]: By using, for example, the library functions [[headers]]
1356
- `std::memcpy` or `std::memmove`.
1357
-
1358
- [^16]: The intent is that the memory model of C++ is compatible with
1359
- that of ISO/IEC 9899 Programming Language C.
1360
-
1361
- [^17]: The size and layout of an instance of an incompletely-defined
1362
- object type is unknown.
1363
-
1364
- [^18]: This is also known as two’s complement representation.
1365
-
1366
- [^19]: Static class members are objects or functions, and pointers to
1367
- them are ordinary pointers to objects or functions.
1368
-
1369
- [^20]: For an object that is not within its lifetime, this is the first
1370
- byte in memory that it will occupy or used to occupy.
1371
-
1372
- [^21]: The same representation and alignment requirements are meant to
1373
- imply interchangeability as arguments to functions, return values
1374
- from functions, and non-static data members of unions.
1375
-
1376
- [^22]: As specified in  [[class.temporary]], after a full-expression is
1377
- evaluated, a sequence of zero or more invocations of destructor
1378
- functions for temporary objects takes place, usually in reverse
1379
- order of the construction of each temporary object.
1380
-
1381
- [^23]: In other words, function executions do not interleave with each
1382
- other.
1383
-
1384
- [^24]: An object with automatic or thread storage duration [[basic.stc]]
1385
- is associated with one specific thread, and can be accessed by a
1386
- different thread only indirectly through a pointer or reference
1387
- [[basic.compound]].
1388
-
1389
- [^25]: A non-block variable with static storage duration having
1390
- initialization with side effects is initialized in this case, even
1391
- if it is not itself odr-used [[term.odr.use]], [[basic.stc.static]].
 
64
  A *full-expression* is
65
 
66
  - an unevaluated operand [[expr.context]],
67
  - a *constant-expression* [[expr.const]],
68
  - an immediate invocation [[expr.const]],
69
+ - an *init-declarator* [[dcl.decl]] (including such introduced by a
70
+ structured binding [[dcl.struct.bind]]) or a *mem-initializer*
71
  [[class.base.init]], including the constituent expressions of the
72
  initializer,
73
  - an invocation of a destructor generated at the end of the lifetime of
74
  an object other than a temporary object [[class.temporary]] whose
75
+ lifetime has not been extended,
76
+ - the predicate of a contract assertion [[basic.contract]], or
77
  - an expression that is not a subexpression of another expression and
78
  that is not otherwise part of a full-expression.
79
 
80
  If a language construct is defined to produce an implicit call of a
81
  function, a use of the language construct is considered to be an
 
102
  void f() {
103
  S s2 = 2; // full-expression comprises call of S::S(int)
104
  if (S(3).v()) // full-expression includes lvalue-to-rvalue and int to bool conversions,
105
  // performed before temporary is deleted at end of full-expression
106
  { }
107
+ bool b = noexcept(S(4)); // exception specification of destructor of S considered for noexcept
108
 
109
  // full-expression is destruction of s2 at end of block
110
  }
111
  struct B {
112
  B(S = S(0));
 
123
  default arguments [[dcl.fct.default]] are considered to be created in
124
  the expression that calls the function, not the expression that defines
125
  the default argument. — *end note*]
126
 
127
  Reading an object designated by a `volatile` glvalue [[basic.lval]],
128
+ modifying an object, producing an injected declaration [[expr.const]],
129
+ calling a library I/O function, or calling a function that does any of
130
+ those operations are all *side effects*, which are changes in the state
131
+ of the execution or translation environment. *Evaluation* of an
132
+ expression (or a subexpression) in general includes both value
133
  computations (including determining the identity of an object for
134
  glvalue evaluation and fetching a value previously assigned to an object
135
  for prvalue evaluation) and initiation of side effects. When a call to a
136
  library I/O function returns or an access through a volatile glvalue is
137
+ evaluated, the side effect is considered complete, even though some
138
  external actions implied by the call (such as the I/O itself) or by the
139
  `volatile` access may not have completed yet.
140
 
141
  *Sequenced before* is an asymmetric, transitive, pair-wise relation
142
  between evaluations executed by a single thread [[intro.multithread]],
 
161
  expression *X* is sequenced before every value computation and every
162
  side effect associated with the expression *Y*.
163
 
164
  Every value computation and side effect associated with a
165
  full-expression is sequenced before every value computation and side
166
+ effect associated with the next full-expression to be evaluated.[^21]
167
 
168
  Except where noted, evaluations of operands of individual operators and
169
  of subexpressions of individual expressions are unsequenced.
170
 
171
  [*Note 5*: In an expression that is evaluated more than once during the
172
  execution of a program, unsequenced and indeterminately sequenced
173
  evaluations of its subexpressions need not be performed consistently in
174
  different evaluations. — *end note*]
175
 
176
  The value computations of the operands of an operator are sequenced
177
+ before the value computation of the result of the operator. The behavior
178
+ is undefined if
179
+
180
+ - a side effect on a memory location [[intro.memory]] or
181
+ - starting or ending the lifetime of an object in a memory location
182
+
183
+ is unsequenced relative to
184
+
185
+ - another side effect on the same memory location,
186
+ - starting or ending the lifetime of an object occupying storage that
187
+ overlaps with the memory location, or
188
+ - a value computation using the value of any object in the same memory
189
+ location,
190
+
191
+ and the two evaluations are not potentially concurrent
192
+ [[intro.multithread]].
193
+
194
+ [*Note 6*: Starting the lifetime of an object in a memory location can
195
+ end the lifetime of objects in other memory locations
196
+ [[basic.life]]. — *end note*]
197
+
198
+ [*Note 7*: The next subclause imposes similar, but more complex
199
  restrictions on potentially concurrent computations. — *end note*]
200
 
201
  [*Example 3*:
202
 
203
  ``` cpp
 
205
  i = 7, i++, i++; // i becomes 9
206
 
207
  i = i++ + 1; // the value of i is incremented
208
  i = i++ + i; // undefined behavior
209
  i = i + 1; // the value of i is incremented
210
+
211
+ union U { int x, y; } u;
212
+ (u.x = 1, 0) + (u.y = 2, 0); // undefined behavior
213
  }
214
  ```
215
 
216
  — *end example*]
217
 
218
+ When invoking a function *f* (whether or not the function is inline),
219
+ every argument expression and the postfix expression designating *f* are
220
+ sequenced before every precondition assertion of *f*
221
+ [[dcl.contract.func]], which in turn are sequenced before every
222
+ expression or statement in the body of *f*, which in turn are sequenced
223
+ before every postcondition assertion of *f*.
224
+
225
+ For each
226
+
227
+ - function invocation,
228
+ - evaluation of an *await-expression* [[expr.await]], or
229
+ - evaluation of a *throw-expression* [[expr.throw]]
230
+
231
+ *F*, each evaluation that does not occur within *F* but is evaluated on
232
+ the same thread and as part of the same signal handler (if any) is
233
+ either sequenced before all evaluations that occur within *F* or
234
+ sequenced after all evaluations that occur within *F*;[^22]
235
 
236
  if *F* invokes or resumes a coroutine [[expr.await]], only evaluations
237
  subsequent to the previous suspension (if any) and prior to the next
238
  suspension (if any) are considered to occur within *F*.
239
 
 
252
 
253
  If a signal handler is executed as a result of a call to the
254
  `std::raise` function, then the execution of the handler is sequenced
255
  after the invocation of the `std::raise` function and before its return.
256
 
257
+ [*Note 8*: When a signal is received for another reason, the execution
258
  of the signal handler is usually unsequenced with respect to the rest of
259
  the program. — *end note*]
260
 
261
+ During the evaluation of an expression as a core constant expression
262
+ [[expr.const]], evaluations of operands of individual operators and of
263
+ subexpressions of individual expressions that are otherwise either
264
+ unsequenced or indeterminately sequenced are evaluated in lexical order.
265
+
266
  ### Multi-threaded executions and data races <a id="intro.multithread">[[intro.multithread]]</a>
267
 
268
  #### General <a id="intro.multithread.general">[[intro.multithread.general]]</a>
269
 
270
  A *thread of execution* (also known as a *thread*) is a single flow of
 
274
 
275
  [*Note 1*: When one thread creates another, the initial call to the
276
  top-level function of the new thread is executed by the new thread, not
277
  by the creating thread. — *end note*]
278
 
279
+ Every thread in a program can potentially use every object and function
280
+ in a program.[^23]
281
 
282
  Under a hosted implementation, a C++ program can have more than one
283
  thread running concurrently. The execution of each thread proceeds as
284
  defined by the remainder of this document. The execution of the entire
285
  program consists of an execution of all of its threads.
 
307
  Much of this subclause is motivated by the desire to support atomic
308
  operations with explicit and detailed visibility constraints. However,
309
  it also implicitly supports a simpler view for more restricted
310
  programs. — *end note*]
311
 
312
+ Two expression evaluations *conflict* if one of them
313
+
314
+ - modifies [[defns.access]] a memory location [[intro.memory]] or
315
+ - starts or ends the lifetime of an object in a memory location
316
+
317
+ and the other one
318
+
319
+ - reads or modifies the same memory location or
320
+ - starts or ends the lifetime of an object occupying storage that
321
+ overlaps with the memory location.
322
+
323
+ [*Note 2*: A modification can still conflict even if it does not alter
324
+ the value of any bits. — *end note*]
325
 
326
  The library defines a number of atomic operations [[atomics]] and
327
  operations on mutexes [[thread]] that are specially identified as
328
  synchronization operations. These operations play a special role in
329
  making assignments in one thread visible to another. A synchronization
330
+ operation on one or more memory locations is either an acquire
331
+ operation, a release operation, or both an acquire and release
332
+ operation. A synchronization operation without an associated memory
333
+ location is a fence and can be either an acquire fence, a release fence,
334
+ or both an acquire and release fence. In addition, there are relaxed
335
+ atomic operations, which are not synchronization operations, and atomic
336
+ read-modify-write operations, which have special characteristics.
337
 
338
+ [*Note 3*: For example, a call that acquires a mutex will perform an
339
  acquire operation on the locations comprising the mutex.
340
  Correspondingly, a call that releases the same mutex will perform a
341
  release operation on those same locations. Informally, performing a
342
  release operation on A forces prior side effects on other memory
343
  locations to become visible to other threads that later perform a
 
346
  operations, they cannot contribute to data races. — *end note*]
347
 
348
  All modifications to a particular atomic object M occur in some
349
  particular total order, called the *modification order* of M.
350
 
351
+ [*Note 4*: There is a separate order for each atomic object. There is
352
  no requirement that these can be combined into a single total order for
353
  all objects. In general this will be impossible since different threads
354
  can observe modifications to different objects in inconsistent
355
  orders. — *end note*]
356
 
 
362
  Certain library calls *synchronize with* other library calls performed
363
  by another thread. For example, an atomic store-release synchronizes
364
  with a load-acquire that takes its value from the store
365
  [[atomics.order]].
366
 
367
+ [*Note 5*: Except in the specified cases, reading a later value does
368
  not necessarily ensure visibility as described below. Such a requirement
369
  would sometimes interfere with efficient implementation. — *end note*]
370
 
371
+ [*Note 6*: The specifications of the synchronization operations define
372
  when one reads the value written by another. For atomic objects, the
373
  definition is clear. All operations on a given mutex occur in a single
374
  total order. Each mutex acquisition “reads the value written” by the
375
  last mutex release. — *end note*]
376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
  An evaluation A *happens before* an evaluation B (or, equivalently, B
378
+ happens after A) if either
 
 
 
 
 
 
 
 
 
 
 
379
 
380
  - A is sequenced before B, or
381
  - A synchronizes with B, or
382
+ - A happens before X and X happens before B.
383
 
384
+ [*Note 7*: An evaluation does not happen before itself. *end note*]
 
385
 
386
  An evaluation A *strongly happens before* an evaluation D if, either
387
 
388
  - A is sequenced before D, or
389
  - A synchronizes with D, and both A and D are sequentially consistent
390
  atomic operations [[atomics.order]], or
391
  - there are evaluations B and C such that A is sequenced before B, B
392
+ happens before C, and C is sequenced before D, or
393
  - there is an evaluation B such that A strongly happens before B, and B
394
  strongly happens before D.
395
 
396
+ [*Note 8*: Informally, if A strongly happens before B, then A appears
397
+ to be evaluated before B in all contexts. *end note*]
 
398
 
399
  A *visible side effect* A on a scalar object or bit-field M with respect
400
  to a value computation B of M satisfies the conditions:
401
 
402
  - A happens before B and
403
  - there is no other side effect X to M such that A happens before X and
404
  X happens before B.
405
 
406
  The value of a non-atomic scalar object or bit-field M, as determined by
407
+ evaluation B, is the value stored by the visible side effect A.
408
 
409
+ [*Note 9*: If there is ambiguity about which side effect to a
410
  non-atomic object or bit-field is visible, then the behavior is either
411
  unspecified or undefined. — *end note*]
412
 
413
+ [*Note 10*: This states that operations on ordinary objects are not
414
  visibly reordered. This is not actually detectable without data races,
415
+ but is needed to ensure that data races, as defined below, and with
416
+ suitable restrictions on the use of atomics, correspond to data races in
417
+ a simple interleaved (sequentially consistent) execution. — *end note*]
 
418
 
419
+ The value of an atomic object M, as determined by evaluation B, is the
420
+ value stored by some unspecified side effect A that modifies M, where B
421
+ does not happen before A.
422
 
423
+ [*Note 11*: The set of such side effects is also restricted by the rest
424
  of the rules described here, and in particular, by the coherence
425
  requirements below. — *end note*]
426
 
427
  If an operation A that modifies an atomic object M happens before an
428
+ operation B that modifies M, then A is earlier than B in the
429
  modification order of M.
430
 
431
+ [*Note 12*: This requirement is known as write-write
432
  coherence. — *end note*]
433
 
434
  If a value computation A of an atomic object M happens before a value
435
  computation B of M, and A takes its value from a side effect X on M,
436
+ then the value computed by B is either the value stored by X or the
437
+ value stored by a side effect Y on M, where Y follows X in the
438
  modification order of M.
439
 
440
+ [*Note 13*: This requirement is known as read-read
441
  coherence. — *end note*]
442
 
443
  If a value computation A of an atomic object M happens before an
444
+ operation B that modifies M, then A takes its value from a side effect X
445
+ on M, where X precedes B in the modification order of M.
446
 
447
+ [*Note 14*: This requirement is known as read-write
448
  coherence. — *end note*]
449
 
450
  If a side effect X on an atomic object M happens before a value
451
+ computation B of M, then the evaluation B takes its value from X or from
452
+ a side effect Y that follows X in the modification order of M.
453
 
454
+ [*Note 15*: This requirement is known as write-read
455
  coherence. — *end note*]
456
 
457
+ [*Note 16*: The four preceding coherence requirements effectively
458
  disallow compiler reordering of atomic operations to a single object,
459
  even if both operations are relaxed loads. This effectively makes the
460
  cache coherence guarantee provided by most hardware available to C++
461
  atomic operations. — *end note*]
462
 
463
+ [*Note 17*: The value observed by a load of an atomic depends on the
464
  “happens before” relation, which depends on the values observed by loads
465
  of atomics. The intended reading is that there must exist an association
466
  of atomic loads with modifications they observe that, together with
467
  suitably chosen modification orders and the “happens before” relation
468
  derived as described above, satisfy the resulting constraints as imposed
 
478
  potentially concurrent conflicting actions, at least one of which is not
479
  atomic, and neither happens before the other, except for the special
480
  case for signal handlers described below. Any such data race results in
481
  undefined behavior.
482
 
483
+ [*Note 18*: It can be shown that programs that correctly use mutexes
484
  and `memory_order::seq_cst` operations to prevent all data races and use
485
  no other synchronization operations behave as if the operations executed
486
  by their constituent threads were simply interleaved, with each value
487
  computation of an object being taken from the last side effect on that
488
  object in that interleaving. This is normally referred to as “sequential
489
  consistency”. However, this applies only to data-race-free programs, and
490
  data-race-free programs cannot observe most program transformations that
491
  do not change single-threaded program semantics. In fact, most
492
+ single-threaded program transformations remain possible, since any
493
+ program that behaves differently as a result has undefined
494
  behavior. — *end note*]
495
 
496
+ Two accesses to the same non-bit-field object of type
497
+ `volatile std::sig_atomic_t` do not result in a data race if both occur
498
+ in the same thread, even if one or more occurs in a signal handler. For
499
+ each signal handler invocation, evaluations performed by the thread
500
+ invoking a signal handler can be divided into two groups A and B, such
501
+ that no evaluations in B happen before evaluations in A, and the
502
+ evaluations of such `volatile std::sig_atomic_t` objects take values as
503
+ though all evaluations in A happened before the execution of the signal
504
+ handler and the execution of the signal handler happened before all
505
+ evaluations in B.
506
 
507
+ [*Note 19*: Compiler transformations that introduce assignments to a
508
  potentially shared memory location that would not be modified by the
509
  abstract machine are generally precluded by this document, since such an
510
  assignment might overwrite another assignment by a different thread in
511
  cases in which an abstract machine execution would not have encountered
512
  a data race. This includes implementations of data member assignment
513
  that overwrite adjacent members in separate memory locations. Reordering
514
  of atomic loads in cases in which the atomics in question might alias is
515
  also generally precluded, since this could violate the coherence
516
  rules. — *end note*]
517
 
518
+ [*Note 20*: It is possible that transformations that introduce a
519
+ speculative read of a potentially shared memory location do not preserve
520
+ the semantics of the C++ program as defined in this document, since they
521
+ potentially introduce a data race. However, they are typically valid in
522
+ the context of an optimizing compiler that targets a specific machine
523
+ with well-defined semantics for data races. They would be invalid for a
524
  hypothetical machine that is not tolerant of races or provides hardware
525
  race detection. — *end note*]
526
 
527
  #### Forward progress <a id="intro.progress">[[intro.progress]]</a>
528
 
529
  The implementation may assume that any thread will eventually do one of
530
  the following:
531
 
532
  - terminate,
533
+ - invoke the function `std::this_thread::yield` [[thread.thread.this]],
534
  - make a call to a library I/O function,
535
+ - perform an access through a volatile glvalue,
536
+ - perform an atomic or synchronization operation other than an atomic
537
+ modify-write operation [[atomics.order]], or
538
+ - continue execution of a trivial infinite loop [[stmt.iter.general]].
539
 
540
  [*Note 1*: This is intended to allow compiler transformations such as
541
+ removal, merging, and reordering of empty loops, even when termination
542
+ cannot be proven. An affordance is made for trivial infinite loops,
543
+ which cannot be removed nor reordered. — *end note*]
544
 
545
  Executions of atomic functions that are either defined to be lock-free
546
  [[atomics.flag]] or indicated as lock-free [[atomics.lockfree]] are
547
  *lock-free executions*.
548
 
 
566
 
567
  During the execution of a thread of execution, each of the following is
568
  termed an *execution step*:
569
 
570
  - termination of the thread of execution,
571
+ - performing an access through a volatile glvalue,
572
+ - completion of a call to a library I/O function, or
573
+ - completion of an atomic or synchronization operation other than an
574
+ atomic modify-write operation [[atomics.order]].
575
 
576
  An invocation of a standard library function that blocks [[defns.block]]
577
  is considered to continuously execute execution steps while waiting for
578
  the condition that it blocks on to be satisfied.
579
 
 
595
 
596
  For a thread of execution providing *concurrent forward progress
597
  guarantees*, the implementation ensures that the thread will eventually
598
  make progress for as long as it has not terminated.
599
 
600
+ [*Note 5*: This applies regardless of whether or not other threads of
601
+ execution (if any) have been or are making progress. To eventually
602
  fulfill this requirement means that this will happen in an unspecified
603
  but finite amount of time. — *end note*]
604
 
605
  It is *implementation-defined* whether the implementation-created thread
606
  of execution that executes `main` [[basic.start.main]] and the threads
 
713
  arguments passed to the program from the environment in which the
714
  program is run. If `argc` is nonzero these arguments shall be supplied
715
  in `argv[0]` through `argv[argc - 1]` as pointers to the initial
716
  characters of null-terminated multibyte strings (NTMBSs)
717
  [[multibyte.strings]] and `argv[0]` shall be the pointer to the initial
718
+ character of an NTMBS that represents the name used to invoke the
719
+ program or `""`. The value of `argc` shall be non-negative. The value of
720
  `argv[argc]` shall be 0.
721
 
722
  *Recommended practice:* Any further (optional) parameters should be
723
  added after `argv`.
724
 
725
+ The function `main` shall not be named by an expression. The linkage
726
  [[basic.link]] of `main` is *implementation-defined*. A program that
727
  defines `main` as deleted or that declares `main` to be `inline`,
728
  `static`, `constexpr`, or `consteval` is ill-formed. The function `main`
729
  shall not be a coroutine [[dcl.fct.def.coroutine]]. The `main` function
730
+ shall not be declared with a *linkage-specification* [[dcl.link]] other
731
+ than `"C++"`. A program that declares
732
 
733
  - a variable `main` that belongs to the global scope, or
734
  - a function `main` that belongs to the global scope and is attached to
735
  a named module, or
736
  - a function template `main` that belongs to the global scope, or
 
746
  destroy any objects with automatic storage duration [[class.dtor]]. If
747
  `std::exit` is invoked during the destruction of an object with static
748
  or thread storage duration, the program has undefined behavior.
749
 
750
  A `return` statement [[stmt.return]] in `main` has the effect of leaving
751
+ the `main` function (destroying any objects with automatic storage
752
+ duration and evaluating any postcondition assertions of `main`) and
753
+ calling `std::exit` with the return value as the argument. If control
754
+ flows off the end of the *compound-statement* of `main`, the effect is
755
+ equivalent to a `return` with operand `0` (see also [[except.handle]]).
756
 
757
  #### Static initialization <a id="basic.start.static">[[basic.start.static]]</a>
758
 
759
  Variables with static storage duration are initialized as a consequence
760
  of program initiation. Variables with thread storage duration are
761
  initialized as a consequence of thread execution. Within each of these
762
  phases of initiation, initialization occurs as follows.
763
 
764
+ *Constant initialization* is performed if a variable with static or
765
+ thread storage duration is constant-initialized [[expr.const]]. If
766
+ constant initialization is not performed, a variable with static storage
767
+ duration [[basic.stc.static]] or thread storage duration
768
+ [[basic.stc.thread]] is zero-initialized [[dcl.init]]. Together,
769
+ zero-initialization and constant initialization are called
770
  *static initialization*; all other initialization is
771
  *dynamic initialization*. All static initialization strongly happens
772
  before [[intro.races]] any dynamic initialization.
773
 
774
  [*Note 1*: The dynamic initialization of non-block variables is
 
861
  It is *implementation-defined* whether the dynamic initialization of a
862
  non-block non-inline variable with static storage duration is sequenced
863
  before the first statement of `main` or is deferred. If it is deferred,
864
  it strongly happens before any non-initialization odr-use of any
865
  non-inline function or non-inline variable defined in the same
866
+ translation unit as the variable to be initialized.[^24]
867
 
868
  It is *implementation-defined* in which threads and at which points in
869
  the program such deferred dynamic initialization occurs.
870
 
871
  *Recommended practice:* An implementation should choose such points in a
 
1001
 
1002
  Calling the function `std::abort()` declared in `<cstdlib>` terminates
1003
  the program without executing any destructors and without calling the
1004
  functions passed to `std::atexit()` or `std::at_quick_exit()`.
1005