From Jason Turner

[basic.memobj]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpfjnlpqxv/{from.md → to.md} +226 -268
tmp/tmpfjnlpqxv/{from.md → to.md} RENAMED
@@ -1,24 +1,29 @@
1
  ## Memory and objects <a id="basic.memobj">[[basic.memobj]]</a>
2
 
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 any member of the basic
7
- execution character set [[lex.charset]] and the eight-bit code units of
8
- the Unicode UTF-8 encoding form and is composed of a contiguous sequence
9
- of bits,[^9] the number of which is *implementation-defined*. The least
10
- significant bit is called the *low-order bit*; the most significant bit
11
- is called the *high-order bit*. The memory available to a C++ program
12
- consists of one or more sequences of contiguous bytes. Every byte has a
13
- unique address.
 
 
 
 
14
 
15
  [*Note 1*: The representation of types is described in 
16
- [[basic.types]]. — *end note*]
17
 
18
- A *memory location* is either an object of scalar type or a maximal
19
- sequence of adjacent bit-fields all having nonzero width.
 
20
 
21
  [*Note 2*: Various features of the language, such as references and
22
  virtual functions, might involve additional memory locations that are
23
  not accessible to programs but are managed by the
24
  implementation. — *end note*]
@@ -46,11 +51,11 @@ struct {
46
  int b:5,
47
  c:11,
48
  :0,
49
  d:8;
50
  struct {int ee:8;} e;
51
- }
52
  ```
53
 
54
  contains four separate memory locations: The member `a` and bit-fields
55
  `d` and `e.ee` are each separate memory locations, and can be modified
56
  concurrently without interfering with each other. The bit-fields `b` and
@@ -65,27 +70,27 @@ can be.
65
  The constructs in a C++ program create, destroy, refer to, access, and
66
  manipulate objects. An *object* is created by a definition
67
  [[basic.def]], by a *new-expression* [[expr.new]], by an operation that
68
  implicitly creates objects (see below), when implicitly changing the
69
  active member of a union [[class.union]], or when a temporary object is
70
- created ([[conv.rval]], [[class.temporary]]). An object occupies a
71
- region of storage in its period of construction [[class.cdtor]],
72
- throughout its lifetime [[basic.life]], and in its period of destruction
73
  [[class.cdtor]].
74
 
75
  [*Note 1*: A function is not an object, regardless of whether or not it
76
  occupies storage in the way that objects do. — *end note*]
77
 
78
  The properties of an object are determined when the object is created.
79
  An object can have a name [[basic.pre]]. An object has a storage
80
  duration [[basic.stc]] which influences its lifetime [[basic.life]]. An
81
- object has a type [[basic.types]]. Some objects are polymorphic
82
- [[class.virtual]]; the implementation generates information associated
83
- with each such object that makes it possible to determine that object’s
84
- type during program execution. For other objects, the interpretation of
85
- the values found therein is determined by the type of the *expression*s
86
- [[expr.compound]] used to access them.
87
 
88
  Objects can contain other objects, called *subobjects*. A subobject can
89
  be a *member subobject* [[class.mem]], a *base class subobject*
90
  [[class.derived]], or an array element. An object that is not a
91
  subobject of any other object is called a *complete object*. If an
@@ -103,13 +108,14 @@ another object *e* of type “array of N `unsigned char`” or of type
103
  “array of N `std::byte`” [[cstddef.syn]], that array *provides storage*
104
  for the created object if:
105
 
106
  - the lifetime of *e* has begun and not ended, and
107
  - the storage for the new object fits entirely within *e*, and
108
- - there is no smaller array object that satisfies these constraints.
 
109
 
110
- [*Note 2*: If that portion of the array previously provided storage for
111
  another object, the lifetime of that object ends because its storage was
112
  reused [[basic.life]]. — *end note*]
113
 
114
  [*Example 1*:
115
 
@@ -150,12 +156,12 @@ of* `x`, determined as follows:
150
  - If `x` is a complete object, then the complete object of `x` is
151
  itself.
152
  - Otherwise, the complete object of `x` is the complete object of the
153
  (unique) object that contains `x`.
154
 
155
- If a complete object, a data member [[class.mem]], or an array element
156
- is of class type, its type is considered the *most derived class*, to
157
  distinguish it from the class type of any base class subobject; an
158
  object of a most derived class type or of a non-class type is called a
159
  *most derived object*.
160
 
161
  A *potentially-overlapping subobject* is either:
@@ -168,27 +174,28 @@ An object has nonzero size if it
168
 
169
  - is not a potentially-overlapping subobject, or
170
  - is not of class type, or
171
  - is of a class type with virtual member functions or virtual base
172
  classes, or
173
- - has subobjects of nonzero size or bit-fields of nonzero length.
 
174
 
175
  Otherwise, if the object is a base class subobject of a standard-layout
176
  class type with no non-static data members, it has zero size. Otherwise,
177
  the circumstances under which the object has zero size are
178
  *implementation-defined*. Unless it is a bit-field [[class.bit]], an
179
  object with nonzero size shall occupy one or more bytes of storage,
180
  including every byte that is occupied in full or in part by any of its
181
  subobjects. An object of trivially copyable or standard-layout type
182
- [[basic.types]] shall occupy contiguous bytes of storage.
183
 
184
  Unless an object is a bit-field or a subobject of zero size, the address
185
  of that object is the address of the first byte it occupies. Two objects
186
  with overlapping lifetimes that are not bit-fields may have the same
187
  address if one is nested within the other, or if at least one is a
188
  subobject of zero size and they are of different types; otherwise, they
189
- have distinct addresses and occupy disjoint bytes of storage.[^10]
190
 
191
  [*Example 2*:
192
 
193
  ``` cpp
194
  static const char test1 = 'x';
@@ -204,18 +211,18 @@ subobject.
204
 
205
  Some operations are described as *implicitly creating objects* within a
206
  specified region of storage. For each operation that is specified as
207
  implicitly creating objects, that operation implicitly creates and
208
  starts the lifetime of zero or more objects of implicit-lifetime types
209
- [[basic.types]] in its specified region of storage if doing so would
210
- result in the program having defined behavior. If no such set of objects
211
- would give the program defined behavior, the behavior of the program is
212
- undefined. If multiple such sets of objects would give the program
213
- defined behavior, it is unspecified which such set of objects is
214
  created.
215
 
216
- [*Note 3*: Such operations do not start the lifetimes of subobjects of
217
  such objects that are not themselves of implicit-lifetime
218
  types. — *end note*]
219
 
220
  Further, after implicitly creating objects within a specified region of
221
  storage, some operations are described as producing a pointer to a
@@ -246,44 +253,44 @@ X *make_x() {
246
  }
247
  ```
248
 
249
  — *end example*]
250
 
251
- An operation that begins the lifetime of an array of `char`,
252
- `unsigned char`, or `std::byte` implicitly creates objects within the
253
- region of storage occupied by the array.
254
 
255
- [*Note 4*: The array object provides storage for these
256
  objects. — *end note*]
257
 
258
  Any implicit or explicit invocation of a function named `operator new`
259
  or `operator new[]` implicitly creates objects in the returned region of
260
  storage and returns a pointer to a suitable created object.
261
 
262
- [*Note 5*: Some functions in the C++ standard library implicitly create
263
- objects ([[allocator.traits.members]], [[c.malloc]], [[cstring.syn]],
264
- [[bit.cast]]). — *end note*]
265
 
266
  ### Lifetime <a id="basic.life">[[basic.life]]</a>
267
 
268
  The *lifetime* of an object or reference is a runtime property of the
269
  object or reference. A variable is said to have *vacuous initialization*
270
  if it is default-initialized and, if it is of class type or a (possibly
271
- multi-dimensional) array thereof, that class type has a trivial default
272
  constructor. The lifetime of an object of type `T` begins when:
273
 
274
  - storage with the proper alignment and size for type `T` is obtained,
275
  and
276
  - its initialization (if any) is complete (including vacuous
277
  initialization) [[dcl.init]],
278
 
279
  except that if the object is a union member or subobject thereof, its
280
  lifetime only begins if that union member is the initialized member in
281
- the union ([[dcl.init.aggr]], [[class.base.init]]), or as described in
282
- [[class.union]] and [[class.copy.ctor]], and except as described in
283
- [[allocator.members]]. The lifetime of an object *o* of type `T` ends
284
- when:
285
 
286
  - if `T` is a non-class type, the object is destroyed, or
287
  - if `T` is a class type, the destructor call starts, or
288
  - the storage which the object occupies is released, or is reused by an
289
  object that is not nested within *o* [[intro.object]].
@@ -298,43 +305,44 @@ member subobjects. — *end note*]
298
  The properties ascribed to objects and references throughout this
299
  document apply for a given object or reference only during its lifetime.
300
 
301
  [*Note 2*: In particular, before the lifetime of an object starts and
302
  after its lifetime ends there are significant restrictions on the use of
303
- the object, as described below, in  [[class.base.init]] and in 
304
  [[class.cdtor]]. Also, the behavior of an object under construction and
305
- destruction might not be the same as the behavior of an object whose
306
- lifetime has started and not ended. [[class.base.init]] and 
307
- [[class.cdtor]] describe the behavior of an object during its periods of
308
- construction and destruction. — *end note*]
309
-
310
- A program may end the lifetime of any object by reusing the storage
311
- which the object occupies or by explicitly calling a destructor or
312
- pseudo-destructor [[expr.prim.id.dtor]] for the object. For an object of
313
- a class type, the program is not required to call the destructor
314
- explicitly before the storage which the object occupies is reused or
315
- released; however, if there is no explicit call to the destructor or if
316
- a *delete-expression* [[expr.delete]] is not used to release the
317
- storage, the destructor is not implicitly called and any program that
318
- depends on the side effects produced by the destructor has undefined
319
- behavior.
 
320
 
321
  Before the lifetime of an object has started but after the storage which
322
- the object will occupy has been allocated[^11] or, after the lifetime of
323
- an object has ended and before the storage which the object occupied is
324
- reused or released, any pointer that represents the address of the
325
- storage location where the object will be or was located may be used but
326
- only in limited ways. For an object under construction or destruction,
327
- see  [[class.cdtor]]. Otherwise, such a pointer refers to allocated
328
- storage [[basic.stc.dynamic.allocation]], and using the pointer as if
329
- the pointer were of type `void*` is well-defined. Indirection through
330
- such a pointer is permitted but the resulting lvalue may only be used in
331
- limited ways, as described below. The program has undefined behavior if:
332
 
333
- - the object will be or was of a class type with a non-trivial
334
- destructor and the pointer is used as the operand of a
335
- *delete-expression*,
 
 
 
 
 
 
 
 
 
336
  - the pointer is used to access a non-static data member or call a
337
  non-static member function of the object, or
338
  - the pointer is implicitly converted [[conv.ptr]] to a pointer to a
339
  virtual base class, or
340
  - the pointer is used as the operand of a `static_cast`
@@ -366,12 +374,12 @@ void B::mutate() {
366
 
367
  void g() {
368
  void* p = std::malloc(sizeof(D1) + sizeof(D2));
369
  B* pb = new (p) D1;
370
  pb->mutate();
371
- *pb; // OK: pb points to valid memory
372
- void* q = pb; // OK: pb points to valid memory
373
  pb->f(); // undefined behavior: lifetime of *pb has ended
374
  }
375
  ```
376
 
377
  — *end example*]
@@ -406,11 +414,11 @@ object o₁ is *transparently replaceable* by an object o₂ if:
406
 
407
  - the storage that o₂ occupies exactly overlays the storage that o₁
408
  occupied, and
409
  - o₁ and o₂ are of the same type (ignoring the top-level cv-qualifiers),
410
  and
411
- - o₁ is not a complete const object, and
412
  - neither o₁ nor o₂ is a potentially-overlapping subobject
413
  [[intro.object]], and
414
  - either o₁ and o₂ are both complete objects, or o₁ and o₂ are direct
415
  subobjects of objects p₁ and p₂, respectively, and p₁ is transparently
416
  replaceable by p₂.
@@ -439,21 +447,23 @@ c1 = c2; // well-defined
439
  c1.f(); // well-defined; c1 refers to a new object of type C
440
  ```
441
 
442
  — *end example*]
443
 
444
- [*Note 3*: If these conditions are not met, a pointer to the new object
445
  can be obtained from a pointer that represents the address of its
446
  storage by calling `std::launder` [[ptr.launder]]. — *end note*]
447
 
448
  If a program ends the lifetime of an object of type `T` with static
449
  [[basic.stc.static]], thread [[basic.stc.thread]], or automatic
450
  [[basic.stc.auto]] storage duration and if `T` has a non-trivial
451
- destructor,[^12] the program must ensure that an object of the original
452
- type occupies that same storage location when the implicit destructor
453
- call takes place; otherwise the behavior of the program is undefined.
454
- This is true even if the block is exited with an exception.
 
 
455
 
456
  [*Example 3*:
457
 
458
  ``` cpp
459
  class T { };
@@ -467,11 +477,11 @@ void h() {
467
  } // undefined behavior at block exit
468
  ```
469
 
470
  — *end example*]
471
 
472
- Creating a new object within the storage that a const complete object
473
  with static, thread, or automatic storage duration occupies, or within
474
  the storage that such a const object used to occupy before its lifetime
475
  ended, results in undefined behavior.
476
 
477
  [*Example 4*:
@@ -493,11 +503,11 @@ void h() {
493
  — *end example*]
494
 
495
  In this subclause, “before” and “after” refer to the “happens before”
496
  relation [[intro.multithread]].
497
 
498
- [*Note 4*: Therefore, undefined behavior results if an object that is
499
  being constructed in one thread is referenced from another thread
500
  without adequate synchronization. — *end note*]
501
 
502
  ### Indeterminate values <a id="basic.indet">[[basic.indet]]</a>
503
 
@@ -516,13 +526,13 @@ undefined except in the following cases:
516
  [[basic.fundamental]] or `std::byte` type [[cstddef.syn]] is produced
517
  by the evaluation of:
518
  - the second or third operand of a conditional expression
519
  [[expr.cond]],
520
  - the right operand of a comma expression [[expr.comma]],
521
- - the operand of a cast or conversion ([[conv.integral]],
522
- [[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]) to an
523
- unsigned ordinary character type or `std::byte` type
524
  [[cstddef.syn]], or
525
  - a discarded-value expression [[expr.context]],
526
 
527
  then the result of the operation is an indeterminate value.
528
  - If an indeterminate value of unsigned ordinary character type or
@@ -553,10 +563,12 @@ int f(bool b) {
553
 
554
  — *end example*]
555
 
556
  ### Storage duration <a id="basic.stc">[[basic.stc]]</a>
557
 
 
 
558
  The *storage duration* is the property of an object that defines the
559
  minimum potential lifetime of the storage containing the object. The
560
  storage duration is determined by the construct used to create the
561
  object and is one of the following:
562
 
@@ -576,33 +588,35 @@ When the end of the duration of a region of storage is reached, the
576
  values of all pointers representing the address of any part of that
577
  region of storage become invalid pointer values [[basic.compound]].
578
  Indirection through an invalid pointer value and passing an invalid
579
  pointer value to a deallocation function have undefined behavior. Any
580
  other use of an invalid pointer value has *implementation-defined*
581
- behavior.[^13]
582
 
583
  #### Static storage duration <a id="basic.stc.static">[[basic.stc.static]]</a>
584
 
585
- All variables which do not have dynamic storage duration, do not have
586
- thread storage duration, and are not local have *static storage
587
- duration*. The storage for these entities lasts for the duration of the
588
- program ([[basic.start.static]], [[basic.start.term]]).
 
 
 
 
 
589
 
590
  If a variable with static storage duration has initialization or a
591
  destructor with side effects, it shall not be eliminated even if it
592
  appears to be unused, except that a class object or its copy/move may be
593
  eliminated as specified in  [[class.copy.elision]].
594
 
595
- The keyword `static` can be used to declare a local variable with static
596
- storage duration.
597
-
598
- [*Note 1*: [[stmt.dcl]] describes the initialization of local `static`
599
- variables; [[basic.start.term]] describes the destruction of local
600
- `static` variables. — *end note*]
601
-
602
- The keyword `static` applied to a class data member in a class
603
- definition gives the data member static storage duration.
604
 
605
  #### Thread storage duration <a id="basic.stc.thread">[[basic.stc.thread]]</a>
606
 
607
  All variables declared with the `thread_local` keyword have
608
  *thread storage duration*. The storage for these entities lasts for the
@@ -615,13 +629,14 @@ specified in  [[basic.start.static]], [[basic.start.dynamic]], and
615
  [[stmt.dcl]] and, if constructed, is destroyed on thread exit
616
  [[basic.start.term]]. — *end note*]
617
 
618
  #### Automatic storage duration <a id="basic.stc.auto">[[basic.stc.auto]]</a>
619
 
620
- Block-scope variables not explicitly declared `static`, `thread_local`,
621
- or `extern` have *automatic storage duration*. The storage for these
622
- entities lasts until the block in which they are created exits.
 
623
 
624
  [*Note 1*: These variables are initialized and destroyed as described
625
  in  [[stmt.dcl]]. — *end note*]
626
 
627
  If a variable with automatic storage duration has initialization or a
@@ -630,30 +645,33 @@ before the end of its block nor eliminate it as an optimization, even if
630
  it appears to be unused, except that a class object or its copy/move may
631
  be eliminated as specified in  [[class.copy.elision]].
632
 
633
  #### Dynamic storage duration <a id="basic.stc.dynamic">[[basic.stc.dynamic]]</a>
634
 
 
 
635
  Objects can be created dynamically during program execution
636
  [[intro.execution]], using *new-expression*s [[expr.new]], and destroyed
637
  using *delete-expression*s [[expr.delete]]. A C++ implementation
638
  provides access to, and management of, dynamic storage via the global
639
- *allocation functions* `operator new` and `operator
640
- new[]` and the global *deallocation functions* `operator
641
- delete` and `operator delete[]`.
642
 
643
  [*Note 1*: The non-allocating forms described in
644
  [[new.delete.placement]] do not perform allocation or
645
  deallocation. — *end note*]
646
 
647
  The library provides default definitions for the global allocation and
648
  deallocation functions. Some global allocation and deallocation
649
- functions are replaceable [[new.delete]]. A C++ program shall provide at
650
- most one definition of a replaceable allocation or deallocation
651
- function. Any such function definition replaces the default version
652
- provided in the library [[replacement.functions]]. The following
653
- allocation and deallocation functions [[support.dynamic]] are implicitly
654
- declared in global scope in each translation unit of a program.
 
655
 
656
  ``` cpp
657
  [[nodiscard]] void* operator new(std::size_t);
658
  [[nodiscard]] void* operator new(std::size_t, std::align_val_t);
659
 
@@ -669,22 +687,24 @@ void operator delete[](void*) noexcept;
669
  void operator delete[](void*, std::size_t) noexcept;
670
  void operator delete[](void*, std::align_val_t) noexcept;
671
  void operator delete[](void*, std::size_t, std::align_val_t) noexcept;
672
  ```
673
 
674
- These implicit declarations introduce only the function names `operator`
675
- `new`, `operator` `new[]`, `operator` `delete`, and `operator`
676
- `delete[]`.
677
 
678
  [*Note 2*: The implicit declarations do not introduce the names `std`,
679
  `std::size_t`, `std::align_val_t`, or any other names that the library
680
  uses to declare these names. Thus, a *new-expression*,
681
  *delete-expression*, or function call that refers to one of these
682
- functions without importing or including the header `<new>` is
683
- well-formed. However, referring to `std` or `std::size_t` or
684
- `std::align_val_t` is ill-formed unless the name has been declared by
685
- importing or including the appropriate header. — *end note*]
 
 
686
 
687
  Allocation and/or deallocation functions may also be declared and
688
  defined for any class [[class.free]].
689
 
690
  If the behavior of an allocation or deallocation function does not
@@ -692,22 +712,21 @@ satisfy the semantic constraints specified in 
692
  [[basic.stc.dynamic.allocation]] and 
693
  [[basic.stc.dynamic.deallocation]], the behavior is undefined.
694
 
695
  ##### Allocation functions <a id="basic.stc.dynamic.allocation">[[basic.stc.dynamic.allocation]]</a>
696
 
697
- An allocation function shall be a class member function or a global
698
- function; a program is ill-formed if an allocation function is declared
699
- in a namespace scope other than global scope or declared static in
700
- global scope. The return type shall be `void*`. The first parameter
701
- shall have type `std::size_t` [[support.types]]. The first parameter
702
- shall not have an associated default argument [[dcl.fct.default]]. The
703
- value of the first parameter is interpreted as the requested size of the
704
- allocation. An allocation function can be a function template. Such a
705
- template shall declare its return type and first parameter as specified
706
- above (that is, template parameter types shall not be used in the return
707
- type and first parameter type). Template allocation functions shall have
708
- two or more parameters.
709
 
710
  An allocation function attempts to allocate the requested amount of
711
  storage. If it is successful, it returns the address of the start of a
712
  block of storage whose length in bytes is at least as large as the
713
  requested size. The order, contiguity, and initial value of storage
@@ -719,11 +738,11 @@ from any previously returned value `p1`, unless that value `p1` was
719
  subsequently passed to a replaceable deallocation function. Furthermore,
720
  for the library allocation functions in  [[new.delete.single]] and 
721
  [[new.delete.array]], `p0` represents the address of a block of storage
722
  disjoint from the storage for any other object accessible to the caller.
723
  The effect of indirecting through a pointer returned from a request for
724
- zero size is undefined.[^14]
725
 
726
  For an allocation function other than a reserved placement allocation
727
  function [[new.delete.placement]], the pointer returned on a successful
728
  call shall represent the address of storage that is aligned as follows:
729
 
@@ -763,14 +782,12 @@ duration [[basic.stc.thread]], for objects of type `std::type_info`
763
  [[expr.typeid]], or for an exception object
764
  [[except.throw]]. — *end note*]
765
 
766
  ##### Deallocation functions <a id="basic.stc.dynamic.deallocation">[[basic.stc.dynamic.deallocation]]</a>
767
 
768
- Deallocation functions shall be class member functions or global
769
- functions; a program is ill-formed if deallocation functions are
770
- declared in a namespace scope other than global scope or declared static
771
- in global scope.
772
 
773
  A deallocation function is a *destroying operator delete* if it has at
774
  least two parameters and its second parameter is of type
775
  `std::destroying_delete_t`. A destroying operator delete shall be a
776
  class member function named `operator delete`.
@@ -784,11 +801,11 @@ first parameter shall be `C*`; otherwise, the type of its first
784
  parameter shall be `void*`. A deallocation function may have more than
785
  one parameter. A *usual deallocation function* is a deallocation
786
  function whose parameters after the first are
787
 
788
  - optionally, a parameter of type `std::destroying_delete_t`, then
789
- - optionally, a parameter of type `std::size_t` [^15], then
790
  - optionally, a parameter of type `std::align_val_t`.
791
 
792
  A destroying operator delete shall be a usual deallocation function. A
793
  deallocation function may be an instance of a function template. Neither
794
  the first parameter nor the return type shall depend on a template
@@ -805,95 +822,30 @@ has no effect.
805
  If the argument given to a deallocation function in the standard library
806
  is a pointer that is not the null pointer value [[basic.compound]], the
807
  deallocation function shall deallocate the storage referenced by the
808
  pointer, ending the duration of the region of storage.
809
 
810
- ##### Safely-derived pointers <a id="basic.stc.dynamic.safety">[[basic.stc.dynamic.safety]]</a>
811
-
812
- A *traceable pointer object* is
813
-
814
- - an object of an object pointer type [[basic.compound]], or
815
- - an object of an integral type that is at least as large as
816
- `std::intptr_t`, or
817
- - a sequence of elements in an array of narrow character type
818
- [[basic.fundamental]], where the size and alignment of the sequence
819
- match those of some object pointer type.
820
-
821
- A pointer value is a *safely-derived pointer* to an object with dynamic
822
- storage duration only if the pointer value has an object pointer type
823
- and is one of the following:
824
-
825
- - the value returned by a call to the C++ standard library
826
- implementation of `::operator new(std::{}size_t)` or
827
- `::operator new(std::size_t, std::align_val_t)` ;[^16]
828
- - the result of taking the address of an object (or one of its
829
- subobjects) designated by an lvalue resulting from indirection through
830
- a safely-derived pointer value;
831
- - the result of well-defined pointer arithmetic [[expr.add]] using a
832
- safely-derived pointer value;
833
- - the result of a well-defined pointer conversion ([[conv.ptr]],
834
- [[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]) of a
835
- safely-derived pointer value;
836
- - the result of a `reinterpret_cast` of a safely-derived pointer value;
837
- - the result of a `reinterpret_cast` of an integer representation of a
838
- safely-derived pointer value;
839
- - the value of an object whose value was copied from a traceable pointer
840
- object, where at the time of the copy the source object contained a
841
- copy of a safely-derived pointer value.
842
-
843
- An integer value is an *integer representation of a safely-derived
844
- pointer* only if its type is at least as large as `std::intptr_t` and it
845
- is one of the following:
846
-
847
- - the result of a `reinterpret_cast` of a safely-derived pointer value;
848
- - the result of a valid conversion of an integer representation of a
849
- safely-derived pointer value;
850
- - the value of an object whose value was copied from a traceable pointer
851
- object, where at the time of the copy the source object contained an
852
- integer representation of a safely-derived pointer value;
853
- - the result of an additive or bitwise operation, one of whose operands
854
- is an integer representation of a safely-derived pointer value `P`, if
855
- that result converted by `reinterpret_cast<void*>` would compare equal
856
- to a safely-derived pointer computable from
857
- `reinterpret_cast<void*>(P)`.
858
-
859
- An implementation may have *relaxed pointer safety*, in which case the
860
- validity of a pointer value does not depend on whether it is a
861
- safely-derived pointer value. Alternatively, an implementation may have
862
- *strict pointer safety*, in which case a pointer value referring to an
863
- object with dynamic storage duration that is not a safely-derived
864
- pointer value is an invalid pointer value unless the referenced complete
865
- object has previously been declared reachable [[util.dynamic.safety]].
866
-
867
- [*Note 6*: The effect of using an invalid pointer value (including
868
- passing it to a deallocation function) is undefined, see  [[basic.stc]].
869
- This is true even if the unsafely-derived pointer value might compare
870
- equal to some safely-derived pointer value. — *end note*]
871
-
872
- It is *implementation-defined* whether an implementation has relaxed or
873
- strict pointer safety.
874
-
875
  #### Duration of subobjects <a id="basic.stc.inherit">[[basic.stc.inherit]]</a>
876
 
877
  The storage duration of subobjects and reference members is that of
878
  their complete object [[intro.object]].
879
 
880
  ### Alignment <a id="basic.align">[[basic.align]]</a>
881
 
882
- Object types have *alignment requirements* ([[basic.fundamental]],
883
- [[basic.compound]]) which place restrictions on the addresses at which
884
- an object of that type may be allocated. An *alignment* is an
885
- *implementation-defined* integer value representing the number of bytes
886
- between successive addresses at which a given object can be allocated.
887
- An object type imposes an alignment requirement on every object of that
888
- type; stricter alignment can be requested using the alignment specifier
889
- [[dcl.align]].
890
 
891
  A *fundamental alignment* is represented by an alignment less than or
892
  equal to the greatest alignment supported by the implementation in all
893
  contexts, which is equal to `alignof(std::max_align_t)`
894
- [[support.types]]. The alignment required for a type might be different
895
  when it is used as the type of a complete object and when it is used as
896
  the type of a subobject.
897
 
898
  [*Example 1*:
899
 
@@ -951,57 +903,55 @@ Comparing alignments is meaningful and provides the obvious results:
951
  - Two alignments are different when their numeric values are not equal.
952
  - When an alignment is larger than another it represents a stricter
953
  alignment.
954
 
955
  [*Note 3*: The runtime pointer alignment function [[ptr.align]] can be
956
- used to obtain an aligned pointer within a buffer; the aligned-storage
957
- templates in the library [[meta.trans.other]] can be used to obtain
958
- aligned storage. — *end note*]
959
 
960
  If a request for a specific extended alignment in a specific context is
961
  not supported by an implementation, the program is ill-formed.
962
 
963
  ### Temporary objects <a id="class.temporary">[[class.temporary]]</a>
964
 
965
  Temporary objects are created
966
 
967
  - when a prvalue is converted to an xvalue [[conv.rval]],
968
  - when needed by the implementation to pass or return an object of
969
- trivially-copyable type (see below), and
970
  - when throwing an exception [[except.throw]]. \[*Note 1*: The lifetime
971
  of exception objects is described in  [[except.throw]]. — *end note*]
972
 
973
  Even when the creation of the temporary object is unevaluated
974
- [[expr.prop]], all the semantic restrictions shall be respected as if
975
  the temporary object had been created and later destroyed.
976
 
977
  [*Note 2*: This includes accessibility [[class.access]] and whether it
978
  is deleted, for the constructor selected and for the destructor.
979
  However, in the special case of the operand of a *decltype-specifier*
980
- [[expr.call]], no temporary is introduced, so the foregoing does not
981
- apply to such a prvalue. — *end note*]
982
 
983
  The materialization of a temporary object is generally delayed as long
984
  as possible in order to avoid creating unnecessary temporary objects.
985
 
986
  [*Note 3*:
987
 
988
  Temporary objects are materialized:
989
 
990
- - when binding a reference to a prvalue ([[dcl.init.ref]],
991
- [[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]],
992
- [[expr.const.cast]], [[expr.cast]]),
993
- - when performing member access on a class prvalue ([[expr.ref]],
994
- [[expr.mptr.oper]]),
995
  - when performing an array-to-pointer conversion or subscripting on an
996
- array prvalue ([[conv.array]], [[expr.sub]]),
997
  - when initializing an object of type `std::initializer_list<T>` from a
998
  *braced-init-list* [[dcl.init.list]],
999
- - for certain unevaluated operands ([[expr.typeid]], [[expr.sizeof]]),
1000
- and
1001
  - when a prvalue that has type other than cv `void` appears as a
1002
- discarded-value expression [[expr.prop]].
1003
 
1004
  — *end note*]
1005
 
1006
  [*Example 1*:
1007
 
@@ -1058,38 +1008,38 @@ object).
1058
 
1059
  [*Note 4*: This latitude is granted to allow objects of class type to
1060
  be passed to or returned from functions in registers. — *end note*]
1061
 
1062
  When an implementation introduces a temporary object of a class that has
1063
- a non-trivial constructor ([[class.default.ctor]],
1064
- [[class.copy.ctor]]), it shall ensure that a constructor is called for
1065
- the temporary object. Similarly, the destructor shall be called for a
1066
- temporary with a non-trivial destructor [[class.dtor]]. Temporary
1067
- objects are destroyed as the last step in evaluating the full-expression
1068
- [[intro.execution]] that (lexically) contains the point where they were
1069
- created. This is true even if that evaluation ends in throwing an
1070
- exception. The value computations and side effects of destroying a
1071
- temporary object are associated only with the full-expression, not with
1072
- any specific subexpression.
1073
 
1074
- There are three contexts in which temporaries are destroyed at a
1075
  different point than the end of the full-expression. The first context
1076
  is when a default constructor is called to initialize an element of an
1077
  array with no corresponding initializer [[dcl.init]]. The second context
1078
  is when a copy constructor is called to copy an element of an array
1079
- while the entire array is copied ([[expr.prim.lambda.capture]],
1080
- [[class.copy.ctor]]). In either case, if the constructor has one or more
1081
- default arguments, the destruction of every temporary created in a
1082
- default argument is sequenced before the construction of the next array
1083
- element, if any.
1084
 
1085
- The third context is when a reference is bound to a temporary
1086
- object.[^17] The temporary object to which the reference is bound or the
1087
- temporary object that is the complete object of a subobject to which the
1088
- reference is bound persists for the lifetime of the reference if the
1089
- glvalue to which the reference is bound was obtained through one of the
1090
- following:
1091
 
1092
  - a temporary materialization conversion [[conv.rval]],
1093
  - `(` *expression* `)`, where *expression* is one of these expressions,
1094
  - subscripting [[expr.sub]] of an array operand, where that operand is
1095
  one of these expressions,
@@ -1128,11 +1078,11 @@ int&& c = cond ? id<int[3]>{1, 2, 3}[i] : static_cast<int&&>(0);
1128
 
1129
  — *end example*]
1130
 
1131
  [*Note 5*:
1132
 
1133
- An explicit type conversion ([[expr.type.conv]], [[expr.cast]]) is
1134
  interpreted as a sequence of elementary casts, covered above.
1135
 
1136
  [*Example 3*:
1137
 
1138
  ``` cpp
@@ -1175,35 +1125,43 @@ The exceptions to this lifetime rule are:
1175
  `return` statement [[stmt.return]] is not extended; the temporary is
1176
  destroyed at the end of the full-expression in the `return` statement.
1177
  - A temporary bound to a reference in a *new-initializer* [[expr.new]]
1178
  persists until the completion of the full-expression containing the
1179
  *new-initializer*.
1180
- \[*Note 7*: This may introduce a dangling reference. — *end note*]
1181
  \[*Example 5*:
1182
  ``` cpp
1183
  struct S { int mi; const std::pair<int,int>& mp; };
1184
  S a { 1, {2,3} };
1185
  S* p = new S{ 1, {2,3} }; // creates dangling reference
1186
  ```
1187
 
1188
  — *end example*]
1189
 
1190
- The destruction of a temporary whose lifetime is not extended by being
1191
- bound to a reference is sequenced before the destruction of every
1192
- temporary which is constructed earlier in the same full-expression. If
1193
- the lifetime of two or more temporaries to which references are bound
1194
- ends at the same point, these temporaries are destroyed at that point in
1195
- the reverse order of the completion of their construction. In addition,
1196
- the destruction of temporaries bound to references shall take into
1197
- account the ordering of destruction of objects with static, thread, or
1198
- automatic storage duration ([[basic.stc.static]], [[basic.stc.thread]],
1199
- [[basic.stc.auto]]); that is, if `obj1` is an object with the same
1200
- storage duration as the temporary and created before the temporary is
1201
- created the temporary shall be destroyed before `obj1` is destroyed; if
1202
- `obj2` is an object with the same storage duration as the temporary and
1203
- created after the temporary is created the temporary shall be destroyed
1204
- after `obj2` is destroyed.
 
 
 
 
 
 
 
 
1205
 
1206
  [*Example 6*:
1207
 
1208
  ``` cpp
1209
  struct S {
 
1
  ## Memory and objects <a id="basic.memobj">[[basic.memobj]]</a>
2
 
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*]
 
51
  int b:5,
52
  c:11,
53
  :0,
54
  d:8;
55
  struct {int ee:8;} e;
56
+ };
57
  ```
58
 
59
  contains four separate memory locations: The member `a` and bit-fields
60
  `d` and `e.ee` are each separate memory locations, and can be modified
61
  concurrently without interfering with each other. The bit-fields `b` and
 
70
  The constructs in a C++ program create, destroy, refer to, access, and
71
  manipulate objects. An *object* is created by a definition
72
  [[basic.def]], by a *new-expression* [[expr.new]], by an operation that
73
  implicitly creates objects (see below), when implicitly changing the
74
  active member of a union [[class.union]], or when a temporary object is
75
+ created [[conv.rval]], [[class.temporary]]. An object occupies a region
76
+ of storage in its period of construction [[class.cdtor]], throughout its
77
+ lifetime [[basic.life]], and in its period of destruction
78
  [[class.cdtor]].
79
 
80
  [*Note 1*: A function is not an object, regardless of whether or not it
81
  occupies storage in the way that objects do. — *end note*]
82
 
83
  The properties of an object are determined when the object is created.
84
  An object can have a name [[basic.pre]]. An object has a storage
85
  duration [[basic.stc]] which influences its lifetime [[basic.life]]. An
86
+ object has a type [[basic.types]].
87
+
88
+ [*Note 2*: Some objects are polymorphic [[class.virtual]]; the
89
+ implementation generates information associated with each such object
90
+ that makes it possible to determine that object’s type during program
91
+ execution. *end note*]
92
 
93
  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
 
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*.
115
 
116
+ [*Note 3*: If that portion of the array previously provided storage for
117
  another object, the lifetime of that object ends because its storage was
118
  reused [[basic.life]]. — *end note*]
119
 
120
  [*Example 1*:
121
 
 
156
  - If `x` is a complete object, then the complete object of `x` is
157
  itself.
158
  - Otherwise, the complete object of `x` is the complete object of the
159
  (unique) object that contains `x`.
160
 
161
+ If a complete object, a member subobject, or an array element is of
162
+ class type, its type is considered the *most derived class*, to
163
  distinguish it from the class type of any base class subobject; an
164
  object of a most derived class type or of a non-class type is called a
165
  *most derived object*.
166
 
167
  A *potentially-overlapping subobject* is either:
 
174
 
175
  - is not a potentially-overlapping subobject, or
176
  - is not of class type, or
177
  - is of a class type with virtual member functions or virtual base
178
  classes, or
179
+ - has subobjects of nonzero size or unnamed bit-fields of nonzero
180
+ length.
181
 
182
  Otherwise, if the object is a base class subobject of a standard-layout
183
  class type with no non-static data members, it has zero size. Otherwise,
184
  the circumstances under which the object has zero size are
185
  *implementation-defined*. Unless it is a bit-field [[class.bit]], an
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';
 
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
 
227
  Further, after implicitly creating objects within a specified region of
228
  storage, some operations are described as producing a pointer to a
 
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]],
285
 
286
  except that if the object is a union member or subobject thereof, its
287
  lifetime only begins if that union member is the initialized member in
288
+ the union [[dcl.init.aggr]], [[class.base.init]], or as described in
289
+ [[class.union]], [[class.copy.ctor]], and [[class.copy.assign]], and
290
+ except as described in [[allocator.members]]. The lifetime of an object
291
+ *o* of type `T` ends when:
292
 
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]].
 
305
  The properties ascribed to objects and references throughout this
306
  document apply for a given object or reference only during its lifetime.
307
 
308
  [*Note 2*: In particular, before the lifetime of an object starts and
309
  after its lifetime ends there are significant restrictions on the use of
310
+ the object, as described below, in  [[class.base.init]], and in 
311
  [[class.cdtor]]. Also, the behavior of an object under construction and
312
+ destruction can differ from the behavior of an object whose lifetime has
313
+ started and not ended. [[class.base.init]] and  [[class.cdtor]] describe
314
+ the behavior of an object during its periods of construction and
315
+ destruction. — *end note*]
316
+
317
+ A program may end the lifetime of an object of class type without
318
+ invoking the destructor, by reusing or releasing the storage as
319
+ described above.
320
+
321
+ [*Note 3*: A *delete-expression* [[expr.delete]] invokes the destructor
322
+ prior to releasing the storage. *end note*]
323
+
324
+ 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
347
  virtual base class, or
348
  - the pointer is used as the operand of a `static_cast`
 
374
 
375
  void g() {
376
  void* p = std::malloc(sizeof(D1) + sizeof(D2));
377
  B* pb = new (p) D1;
378
  pb->mutate();
379
+ *pb; // OK, pb points to valid memory
380
+ void* q = pb; // OK, pb points to valid memory
381
  pb->f(); // undefined behavior: lifetime of *pb has ended
382
  }
383
  ```
384
 
385
  — *end example*]
 
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
419
+ - o₁ is not a const, complete object, and
420
  - neither o₁ nor o₂ is a potentially-overlapping subobject
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₂.
 
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 { };
 
477
  } // undefined behavior at block exit
478
  ```
479
 
480
  — *end example*]
481
 
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*:
 
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
 
 
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
 
563
 
564
  — *end example*]
565
 
566
  ### Storage duration <a id="basic.stc">[[basic.stc]]</a>
567
 
568
+ #### General <a id="basic.stc.general">[[basic.stc.general]]</a>
569
+
570
  The *storage duration* is the property of an object that defines the
571
  minimum potential lifetime of the storage containing the object. The
572
  storage duration is determined by the construct used to create the
573
  object and is one of the following:
574
 
 
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
+
599
+ - do not have thread storage duration and
600
+ - belong to a namespace scope [[basic.scope.namespace]] or are first
601
+ declared with the `static` or `extern` keywords [[dcl.stc]]
602
+
603
+ have *static storage duration*. The storage for these entities lasts for
604
+ the duration of the program
605
+ [[basic.start.static]], [[basic.start.term]].
606
 
607
  If a variable with static storage duration has initialization or a
608
  destructor with side effects, it shall not be eliminated even if it
609
  appears to be unused, except that a class object or its copy/move may be
610
  eliminated as specified in  [[class.copy.elision]].
611
 
612
+ [*Note 1*: The keyword `static` can be used to declare a block
613
+ variable [[basic.scope.block]] with static storage duration;
614
+ [[stmt.dcl]] and [[basic.start.term]] describe the initialization and
615
+ destruction of such variables. The keyword `static` applied to a class
616
+ data member in a class definition gives the data member static storage
617
+ duration [[class.static.data]]. — *end note*]
 
 
 
618
 
619
  #### Thread storage duration <a id="basic.stc.thread">[[basic.stc.thread]]</a>
620
 
621
  All variables declared with the `thread_local` keyword have
622
  *thread storage duration*. The storage for these entities lasts for the
 
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
 
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]].
647
 
648
  #### Dynamic storage duration <a id="basic.stc.dynamic">[[basic.stc.dynamic]]</a>
649
 
650
+ ##### General <a id="basic.stc.dynamic.general">[[basic.stc.dynamic.general]]</a>
651
+
652
  Objects can be created dynamically during program execution
653
  [[intro.execution]], using *new-expression*s [[expr.new]], and destroyed
654
  using *delete-expression*s [[expr.delete]]. A C++ implementation
655
  provides access to, and management of, dynamic storage via the global
656
+ *allocation functions* `operator new` and `operator new[]` and the
657
+ global *deallocation functions* `operator delete` and
658
+ `operator delete[]`.
659
 
660
  [*Note 1*: The non-allocating forms described in
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
 
 
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;
690
  ```
691
 
692
+ These implicit declarations introduce only the function names
693
+ `operator new`, `operator new[]`, `operator delete`, and
694
+ `operator delete[]`.
695
 
696
  [*Note 2*: The implicit declarations do not introduce the names `std`,
697
  `std::size_t`, `std::align_val_t`, or any other names that the library
698
  uses to declare these names. Thus, a *new-expression*,
699
  *delete-expression*, or function call that refers to one of these
700
+ functions without importing or including the header `<new>` or importing
701
+ a C++ library module [[std.modules]] is well-formed. However, referring
702
+ to `std` or `std::size_t` or `std::align_val_t` is ill-formed unless a
703
+ standard library declaration
704
+ [[cstddef.syn]], [[new.syn]], [[std.modules]] of that name precedes
705
+ [[basic.lookup.general]] the use of that name. — *end note*]
706
 
707
  Allocation and/or deallocation functions may also be declared and
708
  defined for any class [[class.free]].
709
 
710
  If the behavior of an allocation or deallocation function does not
 
712
  [[basic.stc.dynamic.allocation]] and 
713
  [[basic.stc.dynamic.deallocation]], the behavior is undefined.
714
 
715
  ##### Allocation functions <a id="basic.stc.dynamic.allocation">[[basic.stc.dynamic.allocation]]</a>
716
 
717
+ An allocation function that is not a class member function shall belong
718
+ to the global scope and not have a name with internal linkage. The
719
+ return type shall be `void*`. The first parameter shall have type
720
+ `std::size_t` [[support.types]]. The first parameter shall not have an
721
+ associated default argument [[dcl.fct.default]]. The value of the first
722
+ parameter is interpreted as the requested size of the allocation. An
723
+ allocation function can be a function template. Such a template shall
724
+ declare its return type and first parameter as specified above (that is,
725
+ template parameter types shall not be used in the return type and first
726
+ parameter type). Allocation function templates shall have two or more
727
+ parameters.
 
728
 
729
  An allocation function attempts to allocate the requested amount of
730
  storage. If it is successful, it returns the address of the start of a
731
  block of storage whose length in bytes is at least as large as the
732
  requested size. The order, contiguity, and initial value of storage
 
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
 
 
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
788
+ to the global scope and not have a name with internal linkage.
 
 
789
 
790
  A deallocation function is a *destroying operator delete* if it has at
791
  least two parameters and its second parameter is of type
792
  `std::destroying_delete_t`. A destroying operator delete shall be a
793
  class member function named `operator delete`.
 
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
  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
 
 
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
 
951
  - when a prvalue that has type other than cv `void` appears as a
952
+ discarded-value expression [[expr.context]].
953
 
954
  — *end note*]
955
 
956
  [*Example 1*:
957
 
 
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:
1041
 
1042
  - a temporary materialization conversion [[conv.rval]],
1043
  - `(` *expression* `)`, where *expression* is one of these expressions,
1044
  - subscripting [[expr.sub]] of an array operand, where that operand is
1045
  one of these expressions,
 
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*:
1087
 
1088
  ``` cpp
 
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
1162
+ is created the temporary shall be destroyed after `obj2` is destroyed.
1163
 
1164
  [*Example 6*:
1165
 
1166
  ``` cpp
1167
  struct S {