From Jason Turner

[basic.memobj]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpnfr6j_yf/{from.md → to.md} +377 -265
tmp/tmpnfr6j_yf/{from.md → to.md} RENAMED
@@ -3,27 +3,23 @@
3
  ### Memory model <a id="intro.memory">[[intro.memory]]</a>
4
 
5
  The fundamental storage unit in the C++ memory model is the *byte*. A
6
  byte is at least large enough to contain the ordinary literal encoding
7
  of any element of the basic literal character set [[lex.charset]] and
8
- the eight-bit code units of the Unicode[^5]
 
9
 
10
- UTF-8 encoding form and is composed of a contiguous sequence of
11
- bits,[^6]
12
-
13
- the number of which is *implementation-defined*. The least significant
14
- bit is called the *low-order bit*; the most significant bit is called
15
- the *high-order bit*. The memory available to a C++ program consists of
16
- one or more sequences of contiguous bytes. Every byte has a unique
17
- address.
18
 
19
  [*Note 1*: The representation of types is described in 
20
  [[basic.types.general]]. — *end note*]
21
 
22
- A *memory location* is either an object of scalar type that is not a
23
- bit-field or a maximal sequence of adjacent bit-fields all having
24
- nonzero width.
25
 
26
  [*Note 2*: Various features of the language, such as references and
27
  virtual functions, might involve additional memory locations that are
28
  not accessible to programs but are managed by the
29
  implementation. — *end note*]
@@ -94,21 +90,21 @@ Objects can contain other objects, called *subobjects*. A subobject can
94
  be a *member subobject* [[class.mem]], a *base class subobject*
95
  [[class.derived]], or an array element. An object that is not a
96
  subobject of any other object is called a *complete object*. If an
97
  object is created in storage associated with a member subobject or array
98
  element *e* (which may or may not be within its lifetime), the created
99
- object is a subobject of *e*’s containing object if:
100
 
101
  - the lifetime of *e*’s containing object has begun and not ended, and
102
  - the storage for the new object exactly overlays the storage location
103
  associated with *e*, and
104
  - the new object is of the same type as *e* (ignoring cv-qualification).
105
 
106
  If a complete object is created [[expr.new]] in storage associated with
107
  another object *e* of type “array of N `unsigned char`” or of type
108
  “array of N `std::byte`” [[cstddef.syn]], that array *provides storage*
109
- for the created object if:
110
 
111
  - the lifetime of *e* has begun and not ended, and
112
  - the storage for the new object fits entirely within *e*, and
113
  - there is no array object that satisfies these constraints nested
114
  within *e*.
@@ -118,10 +114,12 @@ another object, the lifetime of that object ends because its storage was
118
  reused [[basic.life]]. — *end note*]
119
 
120
  [*Example 1*:
121
 
122
  ``` cpp
 
 
123
  template<typename ...T>
124
  struct AlignedUnion {
125
  alignas(T...) unsigned char data[max(sizeof(T)...)];
126
  };
127
  int f() {
@@ -132,20 +130,20 @@ int f() {
132
  return *c + *d; // OK
133
  }
134
 
135
  struct A { unsigned char a[32]; };
136
  struct B { unsigned char b[16]; };
137
- A a;
138
  B *b = new (a.a + 8) B; // a.a provides storage for *b
139
  int *p = new (b->b + 4) int; // b->b provides storage for *p
140
  // a.a does not provide storage for *p (directly),
141
  // but *p is nested within a (see below)
142
  ```
143
 
144
  — *end example*]
145
 
146
- An object *a* is *nested within* another object *b* if:
147
 
148
  - *a* is a subobject of *b*, or
149
  - *b* provides storage for *a*, or
150
  - there exists an object *c* where *a* is nested within *c*, and *c* is
151
  nested within *b*.
@@ -186,23 +184,45 @@ the circumstances under which the object has zero size are
186
  object with nonzero size shall occupy one or more bytes of storage,
187
  including every byte that is occupied in full or in part by any of its
188
  subobjects. An object of trivially copyable or standard-layout type
189
  [[basic.types.general]] shall occupy contiguous bytes of storage.
190
 
 
 
 
 
 
 
 
 
191
  Unless an object is a bit-field or a subobject of zero size, the address
192
  of that object is the address of the first byte it occupies. Two objects
193
  with overlapping lifetimes that are not bit-fields may have the same
194
- address if one is nested within the other, or if at least one is a
195
- subobject of zero size and they are of different types; otherwise, they
196
- have distinct addresses and occupy disjoint bytes of storage.[^7]
 
 
 
 
 
 
197
 
198
  [*Example 2*:
199
 
200
  ``` cpp
201
  static const char test1 = 'x';
202
  static const char test2 = 'x';
203
  const bool b = &test1 != &test2; // always true
 
 
 
 
 
 
 
 
204
  ```
205
 
206
  — *end example*]
207
 
208
  The address of a non-bit-field subobject of zero size is the address of
@@ -211,16 +231,16 @@ subobject.
211
 
212
  Some operations are described as *implicitly creating objects* within a
213
  specified region of storage. For each operation that is specified as
214
  implicitly creating objects, that operation implicitly creates and
215
  starts the lifetime of zero or more objects of implicit-lifetime types
216
- [[basic.types.general]] in its specified region of storage if doing so
217
- would result in the program having defined behavior. If no such set of
218
- objects would give the program defined behavior, the behavior of the
219
- program is undefined. If multiple such sets of objects would give the
220
- program defined behavior, it is unspecified which such set of objects is
221
- created.
222
 
223
  [*Note 4*: Such operations do not start the lifetimes of subobjects of
224
  such objects that are not themselves of implicit-lifetime
225
  types. — *end note*]
226
 
@@ -253,32 +273,123 @@ X *make_x() {
253
  }
254
  ```
255
 
256
  — *end example*]
257
 
258
- An operation that begins the lifetime of an array of `unsigned char` or
259
- `std::byte` implicitly creates objects within the region of storage
260
- occupied by the array.
261
 
262
  [*Note 5*: The array object provides storage for these
263
  objects. — *end note*]
264
 
265
- Any implicit or explicit invocation of a function named `operator new`
266
- or `operator new[]` implicitly creates objects in the returned region of
267
- storage and returns a pointer to a suitable created object.
 
268
 
269
  [*Note 6*: Some functions in the C++ standard library implicitly create
270
  objects
271
- [[obj.lifetime]], [[allocator.traits.members]], [[c.malloc]], [[cstring.syn]], [[bit.cast]]. — *end note*]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
 
273
  ### Lifetime <a id="basic.life">[[basic.life]]</a>
274
 
 
 
 
275
  The *lifetime* of an object or reference is a runtime property of the
276
  object or reference. A variable is said to have *vacuous initialization*
277
- if it is default-initialized and, if it is of class type or a (possibly
278
- multidimensional) array thereof, that class type has a trivial default
279
- constructor. The lifetime of an object of type `T` begins when:
 
 
280
 
281
  - storage with the proper alignment and size for type `T` is obtained,
282
  and
283
  - its initialization (if any) is complete (including vacuous
284
  initialization) [[dcl.init]],
@@ -293,10 +404,29 @@ except as described in [[allocator.members]]. The lifetime of an object
293
  - if `T` is a non-class type, the object is destroyed, or
294
  - if `T` is a class type, the destructor call starts, or
295
  - the storage which the object occupies is released, or is reused by an
296
  object that is not nested within *o* [[intro.object]].
297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
298
  The lifetime of a reference begins when its initialization is complete.
299
  The lifetime of a reference ends as if it were a scalar object requiring
300
  storage.
301
 
302
  [*Note 1*: [[class.base.init]] describes the lifetime of base and
@@ -325,22 +455,22 @@ In this case, the destructor is not implicitly invoked.
325
 
326
  [*Note 4*: The correct behavior of a program often depends on the
327
  destructor being invoked for each object of class type. — *end note*]
328
 
329
  Before the lifetime of an object has started but after the storage which
330
- the object will occupy has been allocated[^8]
331
 
332
- or, after the lifetime of an object has ended and before the storage
333
  which the object occupied is reused or released, any pointer that
334
  represents the address of the storage location where the object will be
335
  or was located may be used but only in limited ways. For an object under
336
  construction or destruction, see  [[class.cdtor]]. Otherwise, such a
337
  pointer refers to allocated storage [[basic.stc.dynamic.allocation]],
338
  and using the pointer as if the pointer were of type `void*` is
339
  well-defined. Indirection through such a pointer is permitted but the
340
  resulting lvalue may only be used in limited ways, as described below.
341
- The program has undefined behavior if:
342
 
343
  - the pointer is used as the operand of a *delete-expression*,
344
  - the pointer is used to access a non-static data member or call a
345
  non-static member function of the object, or
346
  - the pointer is implicitly converted [[conv.ptr]] to a pointer to a
@@ -350,11 +480,11 @@ The program has undefined behavior if:
350
  cv `void`, or to pointer to cv `void` and subsequently to pointer to
351
  cv `char`, cv `unsigned char`, or cv `std::byte` [[cstddef.syn]], or
352
  - the pointer is used as the operand of a `dynamic_cast`
353
  [[expr.dynamic.cast]].
354
 
355
- [*Example 1*:
356
 
357
  ``` cpp
358
  #include <cstdlib>
359
 
360
  struct B {
@@ -383,36 +513,32 @@ void g() {
383
  ```
384
 
385
  — *end example*]
386
 
387
  Similarly, before the lifetime of an object has started but after the
388
- storage which the object will occupy has been allocated or, after the
389
  lifetime of an object has ended and before the storage which the object
390
  occupied is reused or released, any glvalue that refers to the original
391
  object may be used but only in limited ways. For an object under
392
  construction or destruction, see  [[class.cdtor]]. Otherwise, such a
393
  glvalue refers to allocated storage [[basic.stc.dynamic.allocation]],
394
  and using the properties of the glvalue that do not depend on its value
395
- is well-defined. The program has undefined behavior if:
396
 
397
  - the glvalue is used to access the object, or
398
  - the glvalue is used to call a non-static member function of the
399
  object, or
400
  - the glvalue is bound to a reference to a virtual base class
401
  [[dcl.init.ref]], or
402
  - the glvalue is used as the operand of a `dynamic_cast`
403
  [[expr.dynamic.cast]] or as the operand of `typeid`.
404
 
405
- If, after the lifetime of an object has ended and before the storage
406
- which the object occupied is reused or released, a new object is created
407
- at the storage location which the original object occupied, a pointer
408
- that pointed to the original object, a reference that referred to the
409
- original object, or the name of the original object will automatically
410
- refer to the new object and, once the lifetime of the new object has
411
- started, can be used to manipulate the new object, if the original
412
- object is transparently replaceable (see below) by the new object. An
413
- object o₁ is *transparently replaceable* by an object o₂ if:
414
 
415
  - the storage that o₂ occupies exactly overlays the storage that o₁
416
  occupied, and
417
  - o₁ and o₂ are of the same type (ignoring the top-level cv-qualifiers),
418
  and
@@ -421,11 +547,20 @@ object o₁ is *transparently replaceable* by an object o₂ if:
421
  [[intro.object]], and
422
  - either o₁ and o₂ are both complete objects, or o₁ and o₂ are direct
423
  subobjects of objects p₁ and p₂, respectively, and p₁ is transparently
424
  replaceable by p₂.
425
 
426
- [*Example 2*:
 
 
 
 
 
 
 
 
 
427
 
428
  ``` cpp
429
  struct C {
430
  int i;
431
  void f();
@@ -447,25 +582,25 @@ c1 = c2; // well-defined
447
  c1.f(); // well-defined; c1 refers to a new object of type C
448
  ```
449
 
450
  — *end example*]
451
 
452
- [*Note 5*: If these conditions are not met, a pointer to the new object
453
  can be obtained from a pointer that represents the address of its
454
  storage by calling `std::launder` [[ptr.launder]]. — *end note*]
455
 
456
  If a program ends the lifetime of an object of type `T` with static
457
  [[basic.stc.static]], thread [[basic.stc.thread]], or automatic
458
  [[basic.stc.auto]] storage duration and if `T` has a non-trivial
459
- destructor,[^9]
460
 
461
  and another object of the original type does not occupy that same
462
  storage location when the implicit destructor call takes place, the
463
  behavior of the program is undefined. This is true even if the block is
464
  exited with an exception.
465
 
466
- [*Example 3*:
467
 
468
  ``` cpp
469
  class T { };
470
  struct B {
471
  ~B();
@@ -482,11 +617,11 @@ void h() {
482
  Creating a new object within the storage that a const, complete object
483
  with static, thread, or automatic storage duration occupies, or within
484
  the storage that such a const object used to occupy before its lifetime
485
  ended, results in undefined behavior.
486
 
487
- [*Example 4*:
488
 
489
  ``` cpp
490
  struct B {
491
  B();
492
  ~B();
@@ -500,67 +635,114 @@ void h() {
500
  }
501
  ```
502
 
503
  — *end example*]
504
 
505
- In this subclause, “before” and “after” refer to the “happens before”
506
- relation [[intro.multithread]].
507
-
508
- [*Note 6*: Therefore, undefined behavior results if an object that is
509
- being constructed in one thread is referenced from another thread
510
- without adequate synchronization. — *end note*]
511
-
512
- ### Indeterminate values <a id="basic.indet">[[basic.indet]]</a>
513
 
514
  When storage for an object with automatic or dynamic storage duration is
515
- obtained, the object has an *indeterminate value*, and if no
516
- initialization is performed for the object, that object retains an
517
- indeterminate value until that value is replaced [[expr.ass]].
518
 
519
- [*Note 1*: Objects with static or thread storage duration are
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
520
  zero-initialized, see  [[basic.start.static]]. — *end note*]
521
 
522
- If an indeterminate value is produced by an evaluation, the behavior is
523
- undefined except in the following cases:
 
 
524
 
525
- - If an indeterminate value of unsigned ordinary character type
526
- [[basic.fundamental]] or `std::byte` type [[cstddef.syn]] is produced
527
- by the evaluation of:
528
  - the second or third operand of a conditional expression
529
  [[expr.cond]],
530
  - the right operand of a comma expression [[expr.comma]],
531
  - the operand of a cast or conversion
532
  [[conv.integral]], [[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]
533
  to an unsigned ordinary character type or `std::byte` type
534
  [[cstddef.syn]], or
535
  - a discarded-value expression [[expr.context]],
536
 
537
- then the result of the operation is an indeterminate value.
538
- - If an indeterminate value of unsigned ordinary character type or
539
- `std::byte` type is produced by the evaluation of the right operand of
540
- a simple assignment operator [[expr.ass]] whose first operand is an
541
- lvalue of unsigned ordinary character type or `std::byte` type, an
542
- indeterminate value replaces the value of the object referred to by
543
- the left operand.
544
- - If an indeterminate value of unsigned ordinary character type is
545
- produced by the evaluation of the initialization expression when
546
- initializing an object of unsigned ordinary character type, that
547
- object is initialized to an indeterminate value.
 
 
 
548
  - If an indeterminate value of unsigned ordinary character type or
549
  `std::byte` type is produced by the evaluation of the initialization
550
  expression when initializing an object of `std::byte` type, that
551
- object is initialized to an indeterminate value.
 
 
 
 
 
 
552
 
553
  [*Example 1*:
554
 
555
  ``` cpp
556
  int f(bool b) {
557
- unsigned char c;
558
- unsigned char d = c; // OK, d has an indeterminate value
559
  int e = d; // undefined behavior
560
  return b ? d : 0; // undefined behavior if b is true
561
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
562
  ```
563
 
564
  — *end example*]
565
 
566
  ### Storage duration <a id="basic.stc">[[basic.stc]]</a>
@@ -575,24 +757,24 @@ object and is one of the following:
575
  - static storage duration
576
  - thread storage duration
577
  - automatic storage duration
578
  - dynamic storage duration
579
 
 
 
 
 
580
  Static, thread, and automatic storage durations are associated with
581
- objects introduced by declarations [[basic.def]] and implicitly created
582
- by the implementation [[class.temporary]]. The dynamic storage duration
583
- is associated with objects created by a *new-expression* [[expr.new]].
 
584
 
585
  The storage duration categories apply to references as well.
586
 
587
- When the end of the duration of a region of storage is reached, the
588
- values of all pointers representing the address of any part of that
589
- region of storage become invalid pointer values [[basic.compound]].
590
- Indirection through an invalid pointer value and passing an invalid
591
- pointer value to a deallocation function have undefined behavior. Any
592
- other use of an invalid pointer value has *implementation-defined*
593
- behavior.[^10]
594
 
595
  #### Static storage duration <a id="basic.stc.static">[[basic.stc.static]]</a>
596
 
597
  All variables which
598
 
@@ -629,18 +811,22 @@ specified in  [[basic.start.static]], [[basic.start.dynamic]], and
629
  [[stmt.dcl]] and, if constructed, is destroyed on thread exit
630
  [[basic.start.term]]. — *end note*]
631
 
632
  #### Automatic storage duration <a id="basic.stc.auto">[[basic.stc.auto]]</a>
633
 
634
- Variables that belong to a block or parameter scope and are not
635
- explicitly declared `static`, `thread_local`, or `extern` have
636
- *automatic storage duration*. The storage for these entities lasts until
637
- the block in which they are created exits.
638
 
639
  [*Note 1*: These variables are initialized and destroyed as described
640
  in  [[stmt.dcl]]. — *end note*]
641
 
 
 
 
 
642
  If a variable with automatic storage duration has initialization or a
643
  destructor with side effects, an implementation shall not destroy it
644
  before the end of its block nor eliminate it as an optimization, even if
645
  it appears to be unused, except that a class object or its copy/move may
646
  be eliminated as specified in  [[class.copy.elision]].
@@ -661,29 +847,25 @@ global *deallocation functions* `operator delete` and
661
  [[new.delete.placement]] do not perform allocation or
662
  deallocation. — *end note*]
663
 
664
  The library provides default definitions for the global allocation and
665
  deallocation functions. Some global allocation and deallocation
666
- functions are replaceable [[new.delete]]; these are attached to the
667
- global module [[module.unit]]. A C++ program shall provide at most one
668
- definition of a replaceable allocation or deallocation function. Any
669
- such function definition replaces the default version provided in the
670
- library [[replacement.functions]]. The following allocation and
671
- deallocation functions [[support.dynamic]] are implicitly declared in
672
- global scope in each translation unit of a program.
673
 
674
  ``` cpp
675
- [[nodiscard]] void* operator new(std::size_t);
676
- [[nodiscard]] void* operator new(std::size_t, std::align_val_t);
677
 
678
  void operator delete(void*) noexcept;
679
  void operator delete(void*, std::size_t) noexcept;
680
  void operator delete(void*, std::align_val_t) noexcept;
681
  void operator delete(void*, std::size_t, std::align_val_t) noexcept;
682
 
683
- [[nodiscard]] void* operator new[](std::size_t);
684
- [[nodiscard]] void* operator new[](std::size_t, std::align_val_t);
685
 
686
  void operator delete[](void*) noexcept;
687
  void operator delete[](void*, std::size_t) noexcept;
688
  void operator delete[](void*, std::align_val_t) noexcept;
689
  void operator delete[](void*, std::size_t, std::align_val_t) noexcept;
@@ -738,11 +920,11 @@ from any previously returned value `p1`, unless that value `p1` was
738
  subsequently passed to a replaceable deallocation function. Furthermore,
739
  for the library allocation functions in  [[new.delete.single]] and 
740
  [[new.delete.array]], `p0` represents the address of a block of storage
741
  disjoint from the storage for any other object accessible to the caller.
742
  The effect of indirecting through a pointer returned from a request for
743
- zero size is undefined.[^11]
744
 
745
  For an allocation function other than a reserved placement allocation
746
  function [[new.delete.placement]], the pointer returned on a successful
747
  call shall represent the address of storage that is aligned as follows:
748
 
@@ -757,12 +939,12 @@ call shall represent the address of storage that is aligned as follows:
757
 
758
  An allocation function that fails to allocate storage can invoke the
759
  currently installed new-handler function [[new.handler]], if any.
760
 
761
  [*Note 3*: A program-supplied allocation function can obtain the
762
- address of the currently installed `new_handler` using the
763
- `std::get_new_handler` function [[get.new.handler]]. — *end note*]
764
 
765
  An allocation function that has a non-throwing exception specification
766
  [[except.spec]] indicates failure by returning a null pointer value. Any
767
  other allocation function never returns a null pointer value and
768
  indicates failure only by throwing an exception [[except.throw]] of a
@@ -777,11 +959,13 @@ calls to the functions in the C++ standard library.
777
 
778
  [*Note 4*: In particular, a global allocation function is not called to
779
  allocate storage for objects with static storage duration
780
  [[basic.stc.static]], for objects or references with thread storage
781
  duration [[basic.stc.thread]], for objects of type `std::type_info`
782
- [[expr.typeid]], or for an exception object
 
 
783
  [[except.throw]]. — *end note*]
784
 
785
  ##### Deallocation functions <a id="basic.stc.dynamic.deallocation">[[basic.stc.dynamic.deallocation]]</a>
786
 
787
  A deallocation function that is not a class member function shall belong
@@ -801,11 +985,11 @@ first parameter shall be `C*`; otherwise, the type of its first
801
  parameter shall be `void*`. A deallocation function may have more than
802
  one parameter. A *usual deallocation function* is a deallocation
803
  function whose parameters after the first are
804
 
805
  - optionally, a parameter of type `std::destroying_delete_t`, then
806
- - optionally, a parameter of type `std::size_t`,[^12] then
807
  - optionally, a parameter of type `std::align_val_t`.
808
 
809
  A destroying operator delete shall be a usual deallocation function. A
810
  deallocation function may be an instance of a function template. Neither
811
  the first parameter nor the return type shall depend on a template
@@ -822,129 +1006,41 @@ has no effect.
822
  If the argument given to a deallocation function in the standard library
823
  is a pointer that is not the null pointer value [[basic.compound]], the
824
  deallocation function shall deallocate the storage referenced by the
825
  pointer, ending the duration of the region of storage.
826
 
827
- #### Duration of subobjects <a id="basic.stc.inherit">[[basic.stc.inherit]]</a>
828
-
829
- The storage duration of subobjects and reference members is that of
830
- their complete object [[intro.object]].
831
-
832
- ### Alignment <a id="basic.align">[[basic.align]]</a>
833
-
834
- Object types have *alignment requirements*
835
- [[basic.fundamental]], [[basic.compound]] which place restrictions on
836
- the addresses at which an object of that type may be allocated. An
837
- *alignment* is an *implementation-defined* integer value representing
838
- the number of bytes between successive addresses at which a given object
839
- can be allocated. An object type imposes an alignment requirement on
840
- every object of that type; stricter alignment can be requested using the
841
- alignment specifier [[dcl.align]].
842
-
843
- A *fundamental alignment* is represented by an alignment less than or
844
- equal to the greatest alignment supported by the implementation in all
845
- contexts, which is equal to `alignof(std::max_align_t)`
846
- [[support.types]]. The alignment required for a type may be different
847
- when it is used as the type of a complete object and when it is used as
848
- the type of a subobject.
849
-
850
- [*Example 1*:
851
-
852
- ``` cpp
853
- struct B { long double d; };
854
- struct D : virtual B { char c; };
855
- ```
856
-
857
- When `D` is the type of a complete object, it will have a subobject of
858
- type `B`, so it must be aligned appropriately for a `long double`. If
859
- `D` appears as a subobject of another object that also has `B` as a
860
- virtual base class, the `B` subobject might be part of a different
861
- subobject, reducing the alignment requirements on the `D` subobject.
862
-
863
- — *end example*]
864
-
865
- The result of the `alignof` operator reflects the alignment requirement
866
- of the type in the complete-object case.
867
-
868
- An *extended alignment* is represented by an alignment greater than
869
- `alignof(std::max_align_t)`. It is *implementation-defined* whether any
870
- extended alignments are supported and the contexts in which they are
871
- supported [[dcl.align]]. A type having an extended alignment requirement
872
- is an *over-aligned type*.
873
-
874
- [*Note 1*: Every over-aligned type is or contains a class type to which
875
- extended alignment applies (possibly through a non-static data
876
- member). — *end note*]
877
-
878
- A *new-extended alignment* is represented by an alignment greater than
879
- `__STDCPP_DEFAULT_NEW_ALIGNMENT__` [[cpp.predefined]].
880
-
881
- Alignments are represented as values of the type `std::size_t`. Valid
882
- alignments include only those values returned by an `alignof` expression
883
- for the fundamental types plus an additional *implementation-defined*
884
- set of values, which may be empty. Every alignment value shall be a
885
- non-negative integral power of two.
886
-
887
- Alignments have an order from *weaker* to *stronger* or *stricter*
888
- alignments. Stricter alignments have larger alignment values. An address
889
- that satisfies an alignment requirement also satisfies any weaker valid
890
- alignment requirement.
891
-
892
- The alignment requirement of a complete type can be queried using an
893
- `alignof` expression [[expr.alignof]]. Furthermore, the narrow character
894
- types [[basic.fundamental]] shall have the weakest alignment
895
- requirement.
896
-
897
- [*Note 2*: This enables the ordinary character types to be used as the
898
- underlying type for an aligned memory area [[dcl.align]]. — *end note*]
899
-
900
- Comparing alignments is meaningful and provides the obvious results:
901
-
902
- - Two alignments are equal when their numeric values are equal.
903
- - Two alignments are different when their numeric values are not equal.
904
- - When an alignment is larger than another it represents a stricter
905
- alignment.
906
-
907
- [*Note 3*: The runtime pointer alignment function [[ptr.align]] can be
908
- used to obtain an aligned pointer within a buffer; an
909
- *alignment-specifier* [[dcl.align]] can be used to align storage
910
- explicitly. — *end note*]
911
-
912
- If a request for a specific extended alignment in a specific context is
913
- not supported by an implementation, the program is ill-formed.
914
-
915
  ### Temporary objects <a id="class.temporary">[[class.temporary]]</a>
916
 
917
- Temporary objects are created
918
 
919
- - when a prvalue is converted to an xvalue [[conv.rval]],
920
  - when needed by the implementation to pass or return an object of
921
- trivially copyable type (see below), and
922
- - when throwing an exception [[except.throw]]. \[*Note 1*: The lifetime
923
- of exception objects is described in  [[except.throw]]. — *end note*]
924
 
925
  Even when the creation of the temporary object is unevaluated
926
  [[expr.context]], all the semantic restrictions shall be respected as if
927
  the temporary object had been created and later destroyed.
928
 
929
- [*Note 2*: This includes accessibility [[class.access]] and whether it
930
  is deleted, for the constructor selected and for the destructor.
931
  However, in the special case of the operand of a *decltype-specifier*
932
  [[dcl.type.decltype]], no temporary is introduced, so the foregoing does
933
  not apply to such a prvalue. — *end note*]
934
 
935
  The materialization of a temporary object is generally delayed as long
936
  as possible in order to avoid creating unnecessary temporary objects.
937
 
938
- [*Note 3*:
939
 
940
  Temporary objects are materialized:
941
 
942
  - when binding a reference to a prvalue
943
  [[dcl.init.ref]], [[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]], [[expr.const.cast]], [[expr.cast]],
944
- - when performing member access on a class prvalue
945
  [[expr.ref]], [[expr.mptr.oper]],
 
 
946
  - when performing an array-to-pointer conversion or subscripting on an
947
  array prvalue [[conv.array]], [[expr.sub]],
948
  - when initializing an object of type `std::initializer_list<T>` from a
949
  *braced-init-list* [[dcl.init.list]],
950
  - for certain unevaluated operands [[expr.typeid]], [[expr.sizeof]], and
@@ -992,49 +1088,56 @@ result is constructed directly in `c`. On the other hand, the expression
992
  materialized so that the reference parameter of `X::operator=(const X&)`
993
  can bind to it.
994
 
995
  — *end example*]
996
 
997
- When an object of class type `X` is passed to or returned from a
998
- function, if `X` has at least one eligible copy or move constructor
999
- [[special]], each such constructor is trivial, and the destructor of `X`
1000
- is either trivial or deleted, implementations are permitted to create a
1001
- temporary object to hold the function parameter or result object. The
1002
- temporary object is constructed from the function argument or return
1003
- value, respectively, and the function’s parameter or return object is
1004
- initialized as if by using the eligible trivial constructor to copy the
1005
- temporary (even if that constructor is inaccessible or would not be
1006
- selected by overload resolution to perform a copy or move of the
1007
- object).
1008
 
1009
- [*Note 4*: This latitude is granted to allow objects of class type to
1010
- be passed to or returned from functions in registers. *end note*]
 
 
1011
 
1012
- When an implementation introduces a temporary object of a class that has
1013
- a non-trivial constructor [[class.default.ctor]], [[class.copy.ctor]],
1014
- it shall ensure that a constructor is called for the temporary object.
1015
- Similarly, the destructor shall be called for a temporary with a
1016
- non-trivial destructor [[class.dtor]]. Temporary objects are destroyed
1017
- as the last step in evaluating the full-expression [[intro.execution]]
1018
- that (lexically) contains the point where they were created. This is
1019
- true even if that evaluation ends in throwing an exception. The value
1020
- computations and side effects of destroying a temporary object are
1021
- associated only with the full-expression, not with any specific
1022
- subexpression.
1023
 
1024
- There are four contexts in which temporaries are destroyed at a
1025
- different point than the end of the full-expression. The first context
1026
- is when a default constructor is called to initialize an element of an
1027
- array with no corresponding initializer [[dcl.init]]. The second context
1028
- is when a copy constructor is called to copy an element of an array
1029
- while the entire array is copied
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1030
  [[expr.prim.lambda.capture]], [[class.copy.ctor]]. In either case, if
1031
  the constructor has one or more default arguments, the destruction of
1032
  every temporary created in a default argument is sequenced before the
1033
  construction of the next array element, if any.
1034
 
1035
- The third context is when a reference binds to a temporary object.[^13]
1036
 
1037
  The temporary object to which the reference is bound or the temporary
1038
  object that is the complete object of a subobject to which the reference
1039
  is bound persists for the lifetime of the reference if the glvalue to
1040
  which the reference is bound was obtained through one of the following:
@@ -1076,11 +1179,11 @@ int&& c = cond ? id<int[3]>{1, 2, 3}[i] : static_cast<int&&>(0);
1076
  // exactly one of the two temporaries is lifetime-extended
1077
  ```
1078
 
1079
  — *end example*]
1080
 
1081
- [*Note 5*:
1082
 
1083
  An explicit type conversion [[expr.type.conv]], [[expr.cast]] is
1084
  interpreted as a sequence of elementary casts, covered above.
1085
 
1086
  [*Example 3*:
@@ -1091,11 +1194,11 @@ const int& x = (const int&)1; // temporary for value 1 has same lifetime as x
1091
 
1092
  — *end example*]
1093
 
1094
  — *end note*]
1095
 
1096
- [*Note 6*:
1097
 
1098
  If a temporary object has a reference member initialized by another
1099
  temporary object, lifetime extension applies recursively to such a
1100
  member’s initializer.
1101
 
@@ -1119,43 +1222,52 @@ The exceptions to this lifetime rule are:
1119
  containing the call.
1120
  - A temporary object bound to a reference element of an aggregate of
1121
  class type initialized from a parenthesized *expression-list*
1122
  [[dcl.init]] persists until the completion of the full-expression
1123
  containing the *expression-list*.
1124
- - The lifetime of a temporary bound to the returned value in a function
1125
- `return` statement [[stmt.return]] is not extended; the temporary is
1126
- destroyed at the end of the full-expression in the `return` statement.
1127
  - A temporary bound to a reference in a *new-initializer* [[expr.new]]
1128
  persists until the completion of the full-expression containing the
1129
  *new-initializer*.
1130
- \[*Note 7*: This might introduce a dangling reference. — *end note*]
1131
  \[*Example 5*:
1132
  ``` cpp
1133
  struct S { int mi; const std::pair<int,int>& mp; };
1134
  S a { 1, {2,3} };
1135
  S* p = new S{ 1, {2,3} }; // creates dangling reference
1136
  ```
1137
 
1138
  — *end example*]
1139
 
1140
- The fourth context is when a temporary object other than a function
1141
- parameter object is created in the *for-range-initializer* of a
1142
- range-based `for` statement. If such a temporary object would otherwise
1143
- be destroyed at the end of the *for-range-initializer* full-expression,
1144
- the object persists for the lifetime of the reference initialized by the
1145
- *for-range-initializer*.
1146
 
1147
- The destruction of a temporary whose lifetime is not extended beyond the
1148
- full-expression in which it was created is sequenced before the
1149
- destruction of every temporary which is constructed earlier in the same
1150
- full-expression. If the lifetime of two or more temporaries with
1151
- lifetimes extending beyond the full-expressions in which they were
1152
- created ends at the same point, these temporaries are destroyed at that
1153
- point in the reverse order of the completion of their construction. In
1154
- addition, the destruction of such temporaries shall take into account
1155
- the ordering of destruction of objects with static, thread, or automatic
1156
- storage duration
 
 
 
 
 
 
 
 
 
 
 
 
1157
  [[basic.stc.static]], [[basic.stc.thread]], [[basic.stc.auto]]; that is,
1158
  if `obj1` is an object with the same storage duration as the temporary
1159
  and created before the temporary is created the temporary shall be
1160
  destroyed before `obj1` is destroyed; if `obj2` is an object with the
1161
  same storage duration as the temporary and created after the temporary
 
3
  ### Memory model <a id="intro.memory">[[intro.memory]]</a>
4
 
5
  The fundamental storage unit in the C++ memory model is the *byte*. A
6
  byte is at least large enough to contain the ordinary literal encoding
7
  of any element of the basic literal character set [[lex.charset]] and
8
+ the eight-bit code units of the Unicode UTF-8 encoding form and is
9
+ composed of a contiguous sequence of bits,[^5]
10
 
11
+ the number of which is *implementation-defined*. The memory available to
12
+ a C++ program consists of one or more sequences of contiguous bytes.
13
+ Every byte has a unique address.
 
 
 
 
 
14
 
15
  [*Note 1*: The representation of types is described in 
16
  [[basic.types.general]]. — *end note*]
17
 
18
+ A *memory location* is the storage occupied by the object representation
19
+ of either an object of scalar type that is not a bit-field or a maximal
20
+ sequence of adjacent bit-fields all having nonzero width.
21
 
22
  [*Note 2*: Various features of the language, such as references and
23
  virtual functions, might involve additional memory locations that are
24
  not accessible to programs but are managed by the
25
  implementation. — *end note*]
 
90
  be a *member subobject* [[class.mem]], a *base class subobject*
91
  [[class.derived]], or an array element. An object that is not a
92
  subobject of any other object is called a *complete object*. If an
93
  object is created in storage associated with a member subobject or array
94
  element *e* (which may or may not be within its lifetime), the created
95
+ object is a subobject of *e*’s containing object if
96
 
97
  - the lifetime of *e*’s containing object has begun and not ended, and
98
  - the storage for the new object exactly overlays the storage location
99
  associated with *e*, and
100
  - the new object is of the same type as *e* (ignoring cv-qualification).
101
 
102
  If a complete object is created [[expr.new]] in storage associated with
103
  another object *e* of type “array of N `unsigned char`” or of type
104
  “array of N `std::byte`” [[cstddef.syn]], that array *provides storage*
105
+ for the created object if
106
 
107
  - the lifetime of *e* has begun and not ended, and
108
  - the storage for the new object fits entirely within *e*, and
109
  - there is no array object that satisfies these constraints nested
110
  within *e*.
 
114
  reused [[basic.life]]. — *end note*]
115
 
116
  [*Example 1*:
117
 
118
  ``` cpp
119
+ // assumes that sizeof(int) is equal to 4
120
+
121
  template<typename ...T>
122
  struct AlignedUnion {
123
  alignas(T...) unsigned char data[max(sizeof(T)...)];
124
  };
125
  int f() {
 
130
  return *c + *d; // OK
131
  }
132
 
133
  struct A { unsigned char a[32]; };
134
  struct B { unsigned char b[16]; };
135
+ alignas(int) A a;
136
  B *b = new (a.a + 8) B; // a.a provides storage for *b
137
  int *p = new (b->b + 4) int; // b->b provides storage for *p
138
  // a.a does not provide storage for *p (directly),
139
  // but *p is nested within a (see below)
140
  ```
141
 
142
  — *end example*]
143
 
144
+ An object *a* is *nested within* another object *b* if
145
 
146
  - *a* is a subobject of *b*, or
147
  - *b* provides storage for *a*, or
148
  - there exists an object *c* where *a* is nested within *c*, and *c* is
149
  nested within *b*.
 
184
  object with nonzero size shall occupy one or more bytes of storage,
185
  including every byte that is occupied in full or in part by any of its
186
  subobjects. An object of trivially copyable or standard-layout type
187
  [[basic.types.general]] shall occupy contiguous bytes of storage.
188
 
189
+ An object is a *potentially non-unique object* if it is
190
+
191
+ - a string literal object [[lex.string]],
192
+ - the backing array of an initializer list [[dcl.init.ref]], or
193
+ - the object introduced by a call to `std::meta::reflect_constant_array`
194
+ or `std::meta::reflect_constant_string` [[meta.define.static]], or
195
+ - a subobject thereof.
196
+
197
  Unless an object is a bit-field or a subobject of zero size, the address
198
  of that object is the address of the first byte it occupies. Two objects
199
  with overlapping lifetimes that are not bit-fields may have the same
200
+ address if
201
+
202
+ - one is nested within the other,
203
+ - at least one is a subobject of zero size and they are not of similar
204
+ types [[conv.qual]], or
205
+ - they are both potentially non-unique objects;
206
+
207
+ otherwise, they have distinct addresses and occupy disjoint bytes of
208
+ storage.[^6]
209
 
210
  [*Example 2*:
211
 
212
  ``` cpp
213
  static const char test1 = 'x';
214
  static const char test2 = 'x';
215
  const bool b = &test1 != &test2; // always true
216
+
217
+ static const char (&r) [] = "x";
218
+ static const char *s = "x";
219
+ static std::initializer_list<char> il = { 'x' };
220
+ const bool b2 = r != il.begin(); // unspecified result
221
+ const bool b3 = r != s; // unspecified result
222
+ const bool b4 = il.begin() != &test1; // always true
223
+ const bool b5 = r != &test1; // always true
224
  ```
225
 
226
  — *end example*]
227
 
228
  The address of a non-bit-field subobject of zero size is the address of
 
231
 
232
  Some operations are described as *implicitly creating objects* within a
233
  specified region of storage. For each operation that is specified as
234
  implicitly creating objects, that operation implicitly creates and
235
  starts the lifetime of zero or more objects of implicit-lifetime types
236
+ [[term.implicit.lifetime.type]] in its specified region of storage if
237
+ doing so would result in the program having defined behavior. If no such
238
+ set of objects would give the program defined behavior, the behavior of
239
+ the program is undefined. If multiple such sets of objects would give
240
+ the program defined behavior, it is unspecified which such set of
241
+ objects is created.
242
 
243
  [*Note 4*: Such operations do not start the lifetimes of subobjects of
244
  such objects that are not themselves of implicit-lifetime
245
  types. — *end note*]
246
 
 
273
  }
274
  ```
275
 
276
  — *end example*]
277
 
278
+ Except during constant evaluation, an operation that begins the lifetime
279
+ of an array of `unsigned char` or `std::byte` implicitly creates objects
280
+ within the region of storage occupied by the array.
281
 
282
  [*Note 5*: The array object provides storage for these
283
  objects. — *end note*]
284
 
285
+ Except during constant evaluation, any implicit or explicit invocation
286
+ of a function named `operator new` or `operator new[]` implicitly
287
+ creates objects in the returned region of storage and returns a pointer
288
+ to a suitable created object.
289
 
290
  [*Note 6*: Some functions in the C++ standard library implicitly create
291
  objects
292
+ [[obj.lifetime]], [[c.malloc]], [[mem.res.public]], [[bit.cast]], [[cstring.syn]]. — *end note*]
293
+
294
+ ### Alignment <a id="basic.align">[[basic.align]]</a>
295
+
296
+ Object types have *alignment requirements*
297
+ [[basic.fundamental]], [[basic.compound]] which place restrictions on
298
+ the addresses at which an object of that type may be allocated. An
299
+ *alignment* is an *implementation-defined* integer value representing
300
+ the number of bytes between successive addresses at which a given object
301
+ can be allocated. An object type imposes an alignment requirement on
302
+ every object of that type; stricter alignment can be requested using the
303
+ alignment specifier [[dcl.align]]. Attempting to create an object
304
+ [[intro.object]] in storage that does not meet the alignment
305
+ requirements of the object’s type is undefined behavior.
306
+
307
+ A *fundamental alignment* is represented by an alignment less than or
308
+ equal to the greatest alignment supported by the implementation in all
309
+ contexts, which is equal to `alignof(std::max_align_t)`
310
+ [[support.types]]. The alignment required for a type may be different
311
+ when it is used as the type of a complete object and when it is used as
312
+ the type of a subobject.
313
+
314
+ [*Example 1*:
315
+
316
+ ``` cpp
317
+ struct B { long double d; };
318
+ struct D : virtual B { char c; };
319
+ ```
320
+
321
+ When `D` is the type of a complete object, it will have a subobject of
322
+ type `B`, so it must be aligned appropriately for a `long double`. If
323
+ `D` appears as a subobject of another object that also has `B` as a
324
+ virtual base class, the `B` subobject might be part of a different
325
+ subobject, reducing the alignment requirements on the `D` subobject.
326
+
327
+ — *end example*]
328
+
329
+ The result of the `alignof` operator reflects the alignment requirement
330
+ of the type in the complete-object case.
331
+
332
+ An *extended alignment* is represented by an alignment greater than
333
+ `alignof(std::max_align_t)`. It is *implementation-defined* whether any
334
+ extended alignments are supported and the contexts in which they are
335
+ supported [[dcl.align]]. A type having an extended alignment requirement
336
+ is an *over-aligned type*.
337
+
338
+ [*Note 1*: Every over-aligned type is or contains a class type to which
339
+ extended alignment applies (possibly through a non-static data
340
+ member). — *end note*]
341
+
342
+ A *new-extended alignment* is represented by an alignment greater than
343
+ `__STDCPP_DEFAULT_NEW_ALIGNMENT__` [[cpp.predefined]].
344
+
345
+ Alignments are represented as values of the type `std::size_t`. Valid
346
+ alignments include only those values returned by an `alignof` expression
347
+ for the fundamental types plus an additional *implementation-defined*
348
+ set of values, which may be empty. Every alignment value shall be a
349
+ non-negative integral power of two.
350
+
351
+ Alignments have an order from *weaker* to *stronger* or *stricter*
352
+ alignments. Stricter alignments have larger alignment values. An address
353
+ that satisfies an alignment requirement also satisfies any weaker valid
354
+ alignment requirement.
355
+
356
+ The alignment requirement of a complete type can be queried using an
357
+ `alignof` expression [[expr.alignof]]. Furthermore, the narrow character
358
+ types [[basic.fundamental]] shall have the weakest alignment
359
+ requirement.
360
+
361
+ [*Note 2*: This enables the ordinary character types to be used as the
362
+ underlying type for an aligned memory area [[dcl.align]]. — *end note*]
363
+
364
+ Comparing alignments is meaningful and provides the obvious results:
365
+
366
+ - Two alignments are equal when their numeric values are equal.
367
+ - Two alignments are different when their numeric values are not equal.
368
+ - When an alignment is larger than another it represents a stricter
369
+ alignment.
370
+
371
+ [*Note 3*: The runtime pointer alignment function [[ptr.align]] can be
372
+ used to obtain an aligned pointer within a buffer; an
373
+ *alignment-specifier* [[dcl.align]] can be used to align storage
374
+ explicitly. — *end note*]
375
+
376
+ If a request for a specific extended alignment in a specific context is
377
+ not supported by an implementation, the program is ill-formed.
378
 
379
  ### Lifetime <a id="basic.life">[[basic.life]]</a>
380
 
381
+ In this subclause, “before” and “after” refer to the “happens before”
382
+ relation [[intro.multithread]].
383
+
384
  The *lifetime* of an object or reference is a runtime property of the
385
  object or reference. A variable is said to have *vacuous initialization*
386
+ if it is default-initialized, no other initialization is performed, and,
387
+ if it is of class type or a (possibly multidimensional) array thereof, a
388
+ trivial constructor of that class type is selected for the
389
+ default-initialization. The lifetime of an object of type `T` begins
390
+ when:
391
 
392
  - storage with the proper alignment and size for type `T` is obtained,
393
  and
394
  - its initialization (if any) is complete (including vacuous
395
  initialization) [[dcl.init]],
 
404
  - if `T` is a non-class type, the object is destroyed, or
405
  - if `T` is a class type, the destructor call starts, or
406
  - the storage which the object occupies is released, or is reused by an
407
  object that is not nested within *o* [[intro.object]].
408
 
409
+ When evaluating a *new-expression*, storage is considered reused after
410
+ it is returned from the allocation function, but before the evaluation
411
+ of the *new-initializer* [[expr.new]].
412
+
413
+ [*Example 1*:
414
+
415
+ ``` cpp
416
+ struct S {
417
+ int m;
418
+ };
419
+
420
+ void f() {
421
+ S x{1};
422
+ new(&x) S(x.m); // undefined behavior
423
+ }
424
+ ```
425
+
426
+ — *end example*]
427
+
428
  The lifetime of a reference begins when its initialization is complete.
429
  The lifetime of a reference ends as if it were a scalar object requiring
430
  storage.
431
 
432
  [*Note 1*: [[class.base.init]] describes the lifetime of base and
 
455
 
456
  [*Note 4*: The correct behavior of a program often depends on the
457
  destructor being invoked for each object of class type. — *end note*]
458
 
459
  Before the lifetime of an object has started but after the storage which
460
+ the object will occupy has been allocated[^7]
461
 
462
+ or after the lifetime of an object has ended and before the storage
463
  which the object occupied is reused or released, any pointer that
464
  represents the address of the storage location where the object will be
465
  or was located may be used but only in limited ways. For an object under
466
  construction or destruction, see  [[class.cdtor]]. Otherwise, such a
467
  pointer refers to allocated storage [[basic.stc.dynamic.allocation]],
468
  and using the pointer as if the pointer were of type `void*` is
469
  well-defined. Indirection through such a pointer is permitted but the
470
  resulting lvalue may only be used in limited ways, as described below.
471
+ The program has undefined behavior if
472
 
473
  - the pointer is used as the operand of a *delete-expression*,
474
  - the pointer is used to access a non-static data member or call a
475
  non-static member function of the object, or
476
  - the pointer is implicitly converted [[conv.ptr]] to a pointer to a
 
480
  cv `void`, or to pointer to cv `void` and subsequently to pointer to
481
  cv `char`, cv `unsigned char`, or cv `std::byte` [[cstddef.syn]], or
482
  - the pointer is used as the operand of a `dynamic_cast`
483
  [[expr.dynamic.cast]].
484
 
485
+ [*Example 2*:
486
 
487
  ``` cpp
488
  #include <cstdlib>
489
 
490
  struct B {
 
513
  ```
514
 
515
  — *end example*]
516
 
517
  Similarly, before the lifetime of an object has started but after the
518
+ storage which the object will occupy has been allocated or after the
519
  lifetime of an object has ended and before the storage which the object
520
  occupied is reused or released, any glvalue that refers to the original
521
  object may be used but only in limited ways. For an object under
522
  construction or destruction, see  [[class.cdtor]]. Otherwise, such a
523
  glvalue refers to allocated storage [[basic.stc.dynamic.allocation]],
524
  and using the properties of the glvalue that do not depend on its value
525
+ is well-defined. The program has undefined behavior if
526
 
527
  - the glvalue is used to access the object, or
528
  - the glvalue is used to call a non-static member function of the
529
  object, or
530
  - the glvalue is bound to a reference to a virtual base class
531
  [[dcl.init.ref]], or
532
  - the glvalue is used as the operand of a `dynamic_cast`
533
  [[expr.dynamic.cast]] or as the operand of `typeid`.
534
 
535
+ [*Note 5*: Therefore, undefined behavior results if an object that is
536
+ being constructed in one thread is referenced from another thread
537
+ without adequate synchronization. *end note*]
538
+
539
+ An object o₁ is *transparently replaceable* by an object o₂ if
 
 
 
 
540
 
541
  - the storage that o₂ occupies exactly overlays the storage that o₁
542
  occupied, and
543
  - o₁ and o₂ are of the same type (ignoring the top-level cv-qualifiers),
544
  and
 
547
  [[intro.object]], and
548
  - either o₁ and o₂ are both complete objects, or o₁ and o₂ are direct
549
  subobjects of objects p₁ and p₂, respectively, and p₁ is transparently
550
  replaceable by p₂.
551
 
552
+ After the lifetime of an object has ended and before the storage which
553
+ the object occupied is reused or released, if a new object is created at
554
+ the storage location which the original object occupied and the original
555
+ object was transparently replaceable by the new object, a pointer that
556
+ pointed to the original object, a reference that referred to the
557
+ original object, or the name of the original object will automatically
558
+ refer to the new object and, once the lifetime of the new object has
559
+ started, can be used to manipulate the new object.
560
+
561
+ [*Example 3*:
562
 
563
  ``` cpp
564
  struct C {
565
  int i;
566
  void f();
 
582
  c1.f(); // well-defined; c1 refers to a new object of type C
583
  ```
584
 
585
  — *end example*]
586
 
587
+ [*Note 6*: If these conditions are not met, a pointer to the new object
588
  can be obtained from a pointer that represents the address of its
589
  storage by calling `std::launder` [[ptr.launder]]. — *end note*]
590
 
591
  If a program ends the lifetime of an object of type `T` with static
592
  [[basic.stc.static]], thread [[basic.stc.thread]], or automatic
593
  [[basic.stc.auto]] storage duration and if `T` has a non-trivial
594
+ destructor,[^8]
595
 
596
  and another object of the original type does not occupy that same
597
  storage location when the implicit destructor call takes place, the
598
  behavior of the program is undefined. This is true even if the block is
599
  exited with an exception.
600
 
601
+ [*Example 4*:
602
 
603
  ``` cpp
604
  class T { };
605
  struct B {
606
  ~B();
 
617
  Creating a new object within the storage that a const, complete object
618
  with static, thread, or automatic storage duration occupies, or within
619
  the storage that such a const object used to occupy before its lifetime
620
  ended, results in undefined behavior.
621
 
622
+ [*Example 5*:
623
 
624
  ``` cpp
625
  struct B {
626
  B();
627
  ~B();
 
635
  }
636
  ```
637
 
638
  — *end example*]
639
 
640
+ ### Indeterminate and erroneous values <a id="basic.indet">[[basic.indet]]</a>
 
 
 
 
 
 
 
641
 
642
  When storage for an object with automatic or dynamic storage duration is
643
+ obtained, the bytes comprising the storage for the object have the
644
+ following initial value:
 
645
 
646
+ - If the object has dynamic storage duration, or is the object
647
+ associated with a variable or function parameter whose first
648
+ declaration is marked with the `[[indeterminate]]` attribute
649
+ [[dcl.attr.indet]], the bytes have *indeterminate values*;
650
+ - otherwise, the bytes have *erroneous values*, where each value is
651
+ determined by the implementation independently of the state of the
652
+ program.
653
+
654
+ If no initialization is performed for an object (including subobjects),
655
+ such a byte retains its initial value until that value is replaced
656
+ [[dcl.init.general]], [[expr.assign]]. If any bit in the value
657
+ representation has an indeterminate value, the object has an
658
+ indeterminate value; otherwise, if any bit in the value representation
659
+ has an erroneous value, the object has an erroneous value.
660
+
661
+ [*Note 1*: Lvalue-to-rvalue conversion has undefined behavior if the
662
+ erroneous value of an object is not valid for its type
663
+ [[conv.lval]]. — *end note*]
664
+
665
+ [*Note 2*: Objects with static or thread storage duration are
666
  zero-initialized, see  [[basic.start.static]]. — *end note*]
667
 
668
+ Except in the following cases, if an indeterminate value is produced by
669
+ an evaluation, the behavior is undefined, and if an erroneous value is
670
+ produced by an evaluation, the behavior is erroneous and the result of
671
+ the evaluation is the value so produced but is not erroneous:
672
 
673
+ - If an indeterminate or erroneous value of unsigned ordinary character
674
+ type [[basic.fundamental]] or `std::byte` type [[cstddef.syn]] is
675
+ produced by the evaluation of:
676
  - the second or third operand of a conditional expression
677
  [[expr.cond]],
678
  - the right operand of a comma expression [[expr.comma]],
679
  - the operand of a cast or conversion
680
  [[conv.integral]], [[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]
681
  to an unsigned ordinary character type or `std::byte` type
682
  [[cstddef.syn]], or
683
  - a discarded-value expression [[expr.context]],
684
 
685
+ then the result of the operation is an indeterminate value or that
686
+ erroneous value, respectively.
687
+ - If an indeterminate or erroneous value of unsigned ordinary character
688
+ type or `std::byte` type is produced by the evaluation of the right
689
+ operand of a simple assignment operator [[expr.assign]] whose first
690
+ operand is an lvalue of unsigned ordinary character type or
691
+ `std::byte` type, an indeterminate value or that erroneous value,
692
+ respectively, replaces the value of the object referred to by the left
693
+ operand.
694
+ - If an indeterminate or erroneous value of unsigned ordinary character
695
+ type is produced by the evaluation of the initialization expression
696
+ when initializing an object of unsigned ordinary character type, that
697
+ object is initialized to an indeterminate value or that erroneous
698
+ value, respectively.
699
  - If an indeterminate value of unsigned ordinary character type or
700
  `std::byte` type is produced by the evaluation of the initialization
701
  expression when initializing an object of `std::byte` type, that
702
+ object is initialized to an indeterminate value or that erroneous
703
+ value, respectively.
704
+
705
+ Converting an indeterminate or erroneous value of unsigned ordinary
706
+ character type or `std::byte` type produces an indeterminate or
707
+ erroneous value, respectively. In the latter case, the result of the
708
+ conversion is the value of the converted operand.
709
 
710
  [*Example 1*:
711
 
712
  ``` cpp
713
  int f(bool b) {
714
+ unsigned char *c = new unsigned char;
715
+ unsigned char d = *c; // OK, d has an indeterminate value
716
  int e = d; // undefined behavior
717
  return b ? d : 0; // undefined behavior if b is true
718
  }
719
+
720
+ int g(bool b) {
721
+ unsigned char c;
722
+ unsigned char d = c; // no erroneous behavior, but d has an erroneous value
723
+
724
+ assert(c == d); // holds, both integral promotions have erroneous behavior
725
+
726
+ int e = d; // erroneous behavior
727
+ return b ? d : 0; // erroneous behavior if b is true
728
+ }
729
+
730
+ void h() {
731
+ int d1, d2;
732
+
733
+ int e1 = d1; // erroneous behavior
734
+ int e2 = d1; // erroneous behavior
735
+
736
+ assert(e1 == e2); // holds
737
+ assert(e1 == d1); // holds, erroneous behavior
738
+ assert(e2 == d1); // holds, erroneous behavior
739
+
740
+ std::memcpy(&d2, &d1, sizeof(int)); // no erroneous behavior, but d2 has an erroneous value
741
+ assert(e1 == d2); // holds, erroneous behavior
742
+ assert(e2 == d2); // holds, erroneous behavior
743
+ }
744
  ```
745
 
746
  — *end example*]
747
 
748
  ### Storage duration <a id="basic.stc">[[basic.stc]]</a>
 
757
  - static storage duration
758
  - thread storage duration
759
  - automatic storage duration
760
  - dynamic storage duration
761
 
762
+ [*Note 1*: After the duration of a region of storage has ended, the use
763
+ of pointers to that region of storage is limited
764
+ [[basic.compound]]. — *end note*]
765
+
766
  Static, thread, and automatic storage durations are associated with
767
+ objects introduced by declarations [[basic.def]] and with temporary
768
+ objects [[class.temporary]]. The dynamic storage duration is associated
769
+ with objects created by a *new-expression* [[expr.new]] or with
770
+ implicitly created objects [[intro.object]].
771
 
772
  The storage duration categories apply to references as well.
773
 
774
+ The storage duration of subobjects and reference members is that of
775
+ their complete object [[intro.object]].
 
 
 
 
 
776
 
777
  #### Static storage duration <a id="basic.stc.static">[[basic.stc.static]]</a>
778
 
779
  All variables which
780
 
 
811
  [[stmt.dcl]] and, if constructed, is destroyed on thread exit
812
  [[basic.start.term]]. — *end note*]
813
 
814
  #### Automatic storage duration <a id="basic.stc.auto">[[basic.stc.auto]]</a>
815
 
816
+ Variables that belong to a block scope and are not explicitly declared
817
+ `static`, `thread_local`, or `extern` have *automatic storage duration*.
818
+ The storage for such variables lasts until the block in which they are
819
+ created exits.
820
 
821
  [*Note 1*: These variables are initialized and destroyed as described
822
  in  [[stmt.dcl]]. — *end note*]
823
 
824
+ Variables that belong to a parameter scope also have automatic storage
825
+ duration. The storage for a function parameter lasts until immediately
826
+ after its destruction [[expr.call]].
827
+
828
  If a variable with automatic storage duration has initialization or a
829
  destructor with side effects, an implementation shall not destroy it
830
  before the end of its block nor eliminate it as an optimization, even if
831
  it appears to be unused, except that a class object or its copy/move may
832
  be eliminated as specified in  [[class.copy.elision]].
 
847
  [[new.delete.placement]] do not perform allocation or
848
  deallocation. — *end note*]
849
 
850
  The library provides default definitions for the global allocation and
851
  deallocation functions. Some global allocation and deallocation
852
+ functions are replaceable [[term.replaceable.function]]. The following
853
+ allocation and deallocation functions [[support.dynamic]] are implicitly
854
+ declared in global scope in each translation unit of a program.
 
 
 
 
855
 
856
  ``` cpp
857
+ void* operator new(std::size_t);
858
+ void* operator new(std::size_t, std::align_val_t);
859
 
860
  void operator delete(void*) noexcept;
861
  void operator delete(void*, std::size_t) noexcept;
862
  void operator delete(void*, std::align_val_t) noexcept;
863
  void operator delete(void*, std::size_t, std::align_val_t) noexcept;
864
 
865
+ void* operator new[](std::size_t);
866
+ void* operator new[](std::size_t, std::align_val_t);
867
 
868
  void operator delete[](void*) noexcept;
869
  void operator delete[](void*, std::size_t) noexcept;
870
  void operator delete[](void*, std::align_val_t) noexcept;
871
  void operator delete[](void*, std::size_t, std::align_val_t) noexcept;
 
920
  subsequently passed to a replaceable deallocation function. Furthermore,
921
  for the library allocation functions in  [[new.delete.single]] and 
922
  [[new.delete.array]], `p0` represents the address of a block of storage
923
  disjoint from the storage for any other object accessible to the caller.
924
  The effect of indirecting through a pointer returned from a request for
925
+ zero size is undefined.[^9]
926
 
927
  For an allocation function other than a reserved placement allocation
928
  function [[new.delete.placement]], the pointer returned on a successful
929
  call shall represent the address of storage that is aligned as follows:
930
 
 
939
 
940
  An allocation function that fails to allocate storage can invoke the
941
  currently installed new-handler function [[new.handler]], if any.
942
 
943
  [*Note 3*: A program-supplied allocation function can obtain the
944
+ currently installed `new_handler` using the `std::get_new_handler`
945
+ function [[get.new.handler]]. — *end note*]
946
 
947
  An allocation function that has a non-throwing exception specification
948
  [[except.spec]] indicates failure by returning a null pointer value. Any
949
  other allocation function never returns a null pointer value and
950
  indicates failure only by throwing an exception [[except.throw]] of a
 
959
 
960
  [*Note 4*: In particular, a global allocation function is not called to
961
  allocate storage for objects with static storage duration
962
  [[basic.stc.static]], for objects or references with thread storage
963
  duration [[basic.stc.thread]], for objects of type `std::type_info`
964
+ [[expr.typeid]], for an object of type
965
+ `std::contracts::contract_violation` when a contract violation occurs
966
+ [[basic.contract.eval]], or for an exception object
967
  [[except.throw]]. — *end note*]
968
 
969
  ##### Deallocation functions <a id="basic.stc.dynamic.deallocation">[[basic.stc.dynamic.deallocation]]</a>
970
 
971
  A deallocation function that is not a class member function shall belong
 
985
  parameter shall be `void*`. A deallocation function may have more than
986
  one parameter. A *usual deallocation function* is a deallocation
987
  function whose parameters after the first are
988
 
989
  - optionally, a parameter of type `std::destroying_delete_t`, then
990
+ - optionally, a parameter of type `std::size_t`,[^10] then
991
  - optionally, a parameter of type `std::align_val_t`.
992
 
993
  A destroying operator delete shall be a usual deallocation function. A
994
  deallocation function may be an instance of a function template. Neither
995
  the first parameter nor the return type shall depend on a template
 
1006
  If the argument given to a deallocation function in the standard library
1007
  is a pointer that is not the null pointer value [[basic.compound]], the
1008
  deallocation function shall deallocate the storage referenced by the
1009
  pointer, ending the duration of the region of storage.
1010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1011
  ### Temporary objects <a id="class.temporary">[[class.temporary]]</a>
1012
 
1013
+ A *temporary object* is an object created
1014
 
1015
+ - when a prvalue is converted to an xvalue [[conv.rval]] and
1016
  - when needed by the implementation to pass or return an object of
1017
+ suitable type (see below).
 
 
1018
 
1019
  Even when the creation of the temporary object is unevaluated
1020
  [[expr.context]], all the semantic restrictions shall be respected as if
1021
  the temporary object had been created and later destroyed.
1022
 
1023
+ [*Note 1*: This includes accessibility [[class.access]] and whether it
1024
  is deleted, for the constructor selected and for the destructor.
1025
  However, in the special case of the operand of a *decltype-specifier*
1026
  [[dcl.type.decltype]], no temporary is introduced, so the foregoing does
1027
  not apply to such a prvalue. — *end note*]
1028
 
1029
  The materialization of a temporary object is generally delayed as long
1030
  as possible in order to avoid creating unnecessary temporary objects.
1031
 
1032
+ [*Note 2*:
1033
 
1034
  Temporary objects are materialized:
1035
 
1036
  - when binding a reference to a prvalue
1037
  [[dcl.init.ref]], [[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]], [[expr.const.cast]], [[expr.cast]],
1038
+ - when performing certain member accesses on a class prvalue
1039
  [[expr.ref]], [[expr.mptr.oper]],
1040
+ - when invoking an implicit object member function on a class prvalue
1041
+ [[expr.call]],
1042
  - when performing an array-to-pointer conversion or subscripting on an
1043
  array prvalue [[conv.array]], [[expr.sub]],
1044
  - when initializing an object of type `std::initializer_list<T>` from a
1045
  *braced-init-list* [[dcl.init.list]],
1046
  - for certain unevaluated operands [[expr.typeid]], [[expr.sizeof]], and
 
1088
  materialized so that the reference parameter of `X::operator=(const X&)`
1089
  can bind to it.
1090
 
1091
  — *end example*]
1092
 
1093
+ When an object of type `X` is passed to or returned from a
1094
+ potentially-evaluated function call, if `X` is
 
 
 
 
 
 
 
 
 
1095
 
1096
+ - a scalar type or
1097
+ - a class type that has at least one eligible copy or move constructor
1098
+ [[special]], where each such constructor is trivial, and the
1099
+ destructor of `X` is either trivial or deleted,
1100
 
1101
+ implementations are permitted to create temporary objects to hold the
1102
+ function parameter or result object, as follows:
 
 
 
 
 
 
 
 
 
1103
 
1104
+ - The first such temporary object is constructed from the function
1105
+ argument or return value, respectively.
1106
+ - Each successive temporary object is initialized from the previous one
1107
+ as if by direct-initialization if `X` is a scalar type, otherwise by
1108
+ using an eligible trivial constructor.
1109
+ - The function parameter or return object is initialized from the final
1110
+ temporary as if by direct-initialization if `X` is a scalar type,
1111
+ otherwise by using an eligible trivial constructor.
1112
+
1113
+ (In all cases, the eligible constructor is used even if that constructor
1114
+ is inaccessible or would not be selected by overload resolution to
1115
+ perform a copy or move of the object).
1116
+
1117
+ [*Note 3*: This latitude is granted to allow objects to be passed to or
1118
+ returned from functions in registers. — *end note*]
1119
+
1120
+ Temporary objects are destroyed as the last step in evaluating the
1121
+ full-expression [[intro.execution]] that (lexically) contains the point
1122
+ where they were created. This is true even if that evaluation ends in
1123
+ throwing an exception. The value computations and side effects of
1124
+ destroying a temporary object are associated only with the
1125
+ full-expression, not with any specific subexpression.
1126
+
1127
+ Temporary objects are destroyed at a different point than the end of the
1128
+ full-expression in the following contexts: The first context is when a
1129
+ default constructor is called to initialize an element of an array with
1130
+ no corresponding initializer [[dcl.init]]. The second context is when a
1131
+ copy constructor is called to copy an element of an array while the
1132
+ entire array is copied
1133
  [[expr.prim.lambda.capture]], [[class.copy.ctor]]. In either case, if
1134
  the constructor has one or more default arguments, the destruction of
1135
  every temporary created in a default argument is sequenced before the
1136
  construction of the next array element, if any.
1137
 
1138
+ The third context is when a reference binds to a temporary object.[^11]
1139
 
1140
  The temporary object to which the reference is bound or the temporary
1141
  object that is the complete object of a subobject to which the reference
1142
  is bound persists for the lifetime of the reference if the glvalue to
1143
  which the reference is bound was obtained through one of the following:
 
1179
  // exactly one of the two temporaries is lifetime-extended
1180
  ```
1181
 
1182
  — *end example*]
1183
 
1184
+ [*Note 4*:
1185
 
1186
  An explicit type conversion [[expr.type.conv]], [[expr.cast]] is
1187
  interpreted as a sequence of elementary casts, covered above.
1188
 
1189
  [*Example 3*:
 
1194
 
1195
  — *end example*]
1196
 
1197
  — *end note*]
1198
 
1199
+ [*Note 5*:
1200
 
1201
  If a temporary object has a reference member initialized by another
1202
  temporary object, lifetime extension applies recursively to such a
1203
  member’s initializer.
1204
 
 
1222
  containing the call.
1223
  - A temporary object bound to a reference element of an aggregate of
1224
  class type initialized from a parenthesized *expression-list*
1225
  [[dcl.init]] persists until the completion of the full-expression
1226
  containing the *expression-list*.
 
 
 
1227
  - A temporary bound to a reference in a *new-initializer* [[expr.new]]
1228
  persists until the completion of the full-expression containing the
1229
  *new-initializer*.
1230
+ \[*Note 6*: This might introduce a dangling reference. — *end note*]
1231
  \[*Example 5*:
1232
  ``` cpp
1233
  struct S { int mi; const std::pair<int,int>& mp; };
1234
  S a { 1, {2,3} };
1235
  S* p = new S{ 1, {2,3} }; // creates dangling reference
1236
  ```
1237
 
1238
  — *end example*]
1239
 
1240
+ The fourth context is when a temporary object is created in the
1241
+ *for-range-initializer* of either a range-based `for` statement or an
1242
+ enumerating expansion statement [[stmt.expand]]. If such a temporary
1243
+ object would otherwise be destroyed at the end of the
1244
+ *for-range-initializer* full-expression, the object persists for the
1245
+ lifetime of the reference initialized by the *for-range-initializer*.
1246
 
1247
+ The fifth context is when a temporary object is created in the
1248
+ *expansion-initializer* of an iterating or destructuring expansion
1249
+ statement. If such a temporary object would otherwise be destroyed at
1250
+ the end of that *expansion-initializer*, the object persists for the
1251
+ lifetime of the reference initialized by the *expansion-initializer*, if
1252
+ any.
1253
+
1254
+ The sixth context is when a temporary object is created in a structured
1255
+ binding declaration [[dcl.struct.bind]]. Any temporary objects
1256
+ introduced by the *initializer*s for the variables with unique names are
1257
+ destroyed at the end of the structured binding declaration.
1258
+
1259
+ Let `x` and `y` each be either a temporary object whose lifetime is not
1260
+ extended, or a function parameter. If the lifetimes of `x` and `y` end
1261
+ at the end of the same full-expression, and `x` is initialized before
1262
+ `y`, then the destruction of `y` is sequenced before that of `x`. If the
1263
+ lifetime of two or more temporaries with lifetimes extending beyond the
1264
+ full-expressions in which they were created ends at the same point,
1265
+ these temporaries are destroyed at that point in the reverse order of
1266
+ the completion of their construction. In addition, the destruction of
1267
+ such temporaries shall take into account the ordering of destruction of
1268
+ objects with static, thread, or automatic storage duration
1269
  [[basic.stc.static]], [[basic.stc.thread]], [[basic.stc.auto]]; that is,
1270
  if `obj1` is an object with the same storage duration as the temporary
1271
  and created before the temporary is created the temporary shall be
1272
  destroyed before `obj1` is destroyed; if `obj2` is an object with the
1273
  same storage duration as the temporary and created after the temporary