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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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*]
|
| 25 |
+
|
| 26 |
+
Two or more threads of execution [[intro.multithread]] can access
|
| 27 |
+
separate memory locations without interfering with each other.
|
| 28 |
+
|
| 29 |
+
[*Note 3*: Thus a bit-field and an adjacent non-bit-field are in
|
| 30 |
+
separate memory locations, and therefore can be concurrently updated by
|
| 31 |
+
two threads of execution without interference. The same applies to two
|
| 32 |
+
bit-fields, if one is declared inside a nested struct declaration and
|
| 33 |
+
the other is not, or if the two are separated by a zero-length bit-field
|
| 34 |
+
declaration, or if they are separated by a non-bit-field declaration. It
|
| 35 |
+
is not safe to concurrently update two bit-fields in the same struct if
|
| 36 |
+
all fields between them are also bit-fields of nonzero
|
| 37 |
+
width. — *end note*]
|
| 38 |
+
|
| 39 |
+
[*Example 1*:
|
| 40 |
+
|
| 41 |
+
A class declared as
|
| 42 |
+
|
| 43 |
+
``` cpp
|
| 44 |
+
struct {
|
| 45 |
+
char a;
|
| 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
|
| 57 |
+
`c` together constitute the fourth memory location. The bit-fields `b`
|
| 58 |
+
and `c` cannot be concurrently modified, but `b` and `a`, for example,
|
| 59 |
+
can be.
|
| 60 |
+
|
| 61 |
+
— *end example*]
|
| 62 |
+
|
| 63 |
+
### Object model <a id="intro.object">[[intro.object]]</a>
|
| 64 |
+
|
| 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
|
| 92 |
+
object is created in storage associated with a member subobject or array
|
| 93 |
+
element *e* (which may or may not be within its lifetime), the created
|
| 94 |
+
object is a subobject of *e*’s containing object if:
|
| 95 |
+
|
| 96 |
+
- the lifetime of *e*’s containing object has begun and not ended, and
|
| 97 |
+
- the storage for the new object exactly overlays the storage location
|
| 98 |
+
associated with *e*, and
|
| 99 |
+
- the new object is of the same type as *e* (ignoring cv-qualification).
|
| 100 |
+
|
| 101 |
+
If a complete object is created [[expr.new]] in storage associated with
|
| 102 |
+
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 |
+
|
| 116 |
+
``` cpp
|
| 117 |
+
template<typename ...T>
|
| 118 |
+
struct AlignedUnion {
|
| 119 |
+
alignas(T...) unsigned char data[max(sizeof(T)...)];
|
| 120 |
+
};
|
| 121 |
+
int f() {
|
| 122 |
+
AlignedUnion<int, char> au;
|
| 123 |
+
int *p = new (au.data) int; // OK, au.data provides storage
|
| 124 |
+
char *c = new (au.data) char(); // OK, ends lifetime of *p
|
| 125 |
+
char *d = new (au.data + 1) char();
|
| 126 |
+
return *c + *d; // OK
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
struct A { unsigned char a[32]; };
|
| 130 |
+
struct B { unsigned char b[16]; };
|
| 131 |
+
A a;
|
| 132 |
+
B *b = new (a.a + 8) B; // a.a provides storage for *b
|
| 133 |
+
int *p = new (b->b + 4) int; // b->b provides storage for *p
|
| 134 |
+
// a.a does not provide storage for *p (directly),
|
| 135 |
+
// but *p is nested within a (see below)
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
— *end example*]
|
| 139 |
+
|
| 140 |
+
An object *a* is *nested within* another object *b* if:
|
| 141 |
+
|
| 142 |
+
- *a* is a subobject of *b*, or
|
| 143 |
+
- *b* provides storage for *a*, or
|
| 144 |
+
- there exists an object *c* where *a* is nested within *c*, and *c* is
|
| 145 |
+
nested within *b*.
|
| 146 |
+
|
| 147 |
+
For every object `x`, there is some object called the *complete object
|
| 148 |
+
of* `x`, determined as follows:
|
| 149 |
+
|
| 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:
|
| 162 |
+
|
| 163 |
+
- a base class subobject, or
|
| 164 |
+
- a non-static data member declared with the `no_unique_address`
|
| 165 |
+
attribute [[dcl.attr.nouniqueaddr]].
|
| 166 |
+
|
| 167 |
+
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';
|
| 195 |
+
static const char test2 = 'x';
|
| 196 |
+
const bool b = &test1 != &test2; // always true
|
| 197 |
+
```
|
| 198 |
+
|
| 199 |
+
— *end example*]
|
| 200 |
+
|
| 201 |
+
The address of a non-bit-field subobject of zero size is the address of
|
| 202 |
+
an unspecified byte of storage occupied by the complete object of that
|
| 203 |
+
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
|
| 222 |
+
*suitable created object*. These operations select one of the
|
| 223 |
+
implicitly-created objects whose address is the address of the start of
|
| 224 |
+
the region of storage, and produce a pointer value that points to that
|
| 225 |
+
object, if that value would result in the program having defined
|
| 226 |
+
behavior. If no such pointer value would give the program defined
|
| 227 |
+
behavior, the behavior of the program is undefined. If multiple such
|
| 228 |
+
pointer values would give the program defined behavior, it is
|
| 229 |
+
unspecified which such pointer value is produced.
|
| 230 |
+
|
| 231 |
+
[*Example 3*:
|
| 232 |
+
|
| 233 |
+
``` cpp
|
| 234 |
+
#include <cstdlib>
|
| 235 |
+
struct X { int a, b; };
|
| 236 |
+
X *make_x() {
|
| 237 |
+
// The call to std::malloc implicitly creates an object of type X
|
| 238 |
+
// and its subobjects a and b, and returns a pointer to that X object
|
| 239 |
+
// (or an object that is pointer-interconvertible[basic.compound] with it),
|
| 240 |
+
// in order to give the subsequent class member access operations
|
| 241 |
+
// defined behavior.
|
| 242 |
+
X *p = (X*)std::malloc(sizeof(struct X));
|
| 243 |
+
p->a = 1;
|
| 244 |
+
p->b = 2;
|
| 245 |
+
return p;
|
| 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]].
|
| 290 |
+
|
| 291 |
+
The lifetime of a reference begins when its initialization is complete.
|
| 292 |
+
The lifetime of a reference ends as if it were a scalar object requiring
|
| 293 |
+
storage.
|
| 294 |
+
|
| 295 |
+
[*Note 1*: [[class.base.init]] describes the lifetime of base and
|
| 296 |
+
member subobjects. — *end note*]
|
| 297 |
+
|
| 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`
|
| 341 |
+
[[expr.static.cast]], except when the conversion is to pointer to
|
| 342 |
+
cv `void`, or to pointer to cv `void` and subsequently to pointer to
|
| 343 |
+
cv `char`, cv `unsigned char`, or cv `std::byte` [[cstddef.syn]], or
|
| 344 |
+
- the pointer is used as the operand of a `dynamic_cast`
|
| 345 |
+
[[expr.dynamic.cast]].
|
| 346 |
+
|
| 347 |
+
[*Example 1*:
|
| 348 |
+
|
| 349 |
+
``` cpp
|
| 350 |
+
#include <cstdlib>
|
| 351 |
+
|
| 352 |
+
struct B {
|
| 353 |
+
virtual void f();
|
| 354 |
+
void mutate();
|
| 355 |
+
virtual ~B();
|
| 356 |
+
};
|
| 357 |
+
|
| 358 |
+
struct D1 : B { void f(); };
|
| 359 |
+
struct D2 : B { void f(); };
|
| 360 |
+
|
| 361 |
+
void B::mutate() {
|
| 362 |
+
new (this) D2; // reuses storage --- ends the lifetime of *this
|
| 363 |
+
f(); // undefined behavior
|
| 364 |
+
... = this; // OK, this points to valid memory
|
| 365 |
+
}
|
| 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*]
|
| 378 |
+
|
| 379 |
+
Similarly, before the lifetime of an object has started but after the
|
| 380 |
+
storage which the object will occupy has been allocated or, after the
|
| 381 |
+
lifetime of an object has ended and before the storage which the object
|
| 382 |
+
occupied is reused or released, any glvalue that refers to the original
|
| 383 |
+
object may be used but only in limited ways. For an object under
|
| 384 |
+
construction or destruction, see [[class.cdtor]]. Otherwise, such a
|
| 385 |
+
glvalue refers to allocated storage [[basic.stc.dynamic.allocation]],
|
| 386 |
+
and using the properties of the glvalue that do not depend on its value
|
| 387 |
+
is well-defined. The program has undefined behavior if:
|
| 388 |
+
|
| 389 |
+
- the glvalue is used to access the object, or
|
| 390 |
+
- the glvalue is used to call a non-static member function of the
|
| 391 |
+
object, or
|
| 392 |
+
- the glvalue is bound to a reference to a virtual base class
|
| 393 |
+
[[dcl.init.ref]], or
|
| 394 |
+
- the glvalue is used as the operand of a `dynamic_cast`
|
| 395 |
+
[[expr.dynamic.cast]] or as the operand of `typeid`.
|
| 396 |
+
|
| 397 |
+
If, after the lifetime of an object has ended and before the storage
|
| 398 |
+
which the object occupied is reused or released, a new object is created
|
| 399 |
+
at the storage location which the original object occupied, a pointer
|
| 400 |
+
that pointed to the original object, a reference that referred to the
|
| 401 |
+
original object, or the name of the original object will automatically
|
| 402 |
+
refer to the new object and, once the lifetime of the new object has
|
| 403 |
+
started, can be used to manipulate the new object, if the original
|
| 404 |
+
object is transparently replaceable (see below) by the new object. An
|
| 405 |
+
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₂.
|
| 417 |
+
|
| 418 |
+
[*Example 2*:
|
| 419 |
+
|
| 420 |
+
``` cpp
|
| 421 |
+
struct C {
|
| 422 |
+
int i;
|
| 423 |
+
void f();
|
| 424 |
+
const C& operator=( const C& );
|
| 425 |
+
};
|
| 426 |
+
|
| 427 |
+
const C& C::operator=( const C& other) {
|
| 428 |
+
if ( this != &other ) {
|
| 429 |
+
this->~C(); // lifetime of *this ends
|
| 430 |
+
new (this) C(other); // new object of type C created
|
| 431 |
+
f(); // well-defined
|
| 432 |
+
}
|
| 433 |
+
return *this;
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
C c1;
|
| 437 |
+
C c2;
|
| 438 |
+
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 { };
|
| 460 |
+
struct B {
|
| 461 |
+
~B();
|
| 462 |
+
};
|
| 463 |
+
|
| 464 |
+
void h() {
|
| 465 |
+
B b;
|
| 466 |
+
new (&b) T;
|
| 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*:
|
| 478 |
+
|
| 479 |
+
``` cpp
|
| 480 |
+
struct B {
|
| 481 |
+
B();
|
| 482 |
+
~B();
|
| 483 |
+
};
|
| 484 |
+
|
| 485 |
+
const B b;
|
| 486 |
+
|
| 487 |
+
void h() {
|
| 488 |
+
b.~B();
|
| 489 |
+
new (const_cast<B*>(&b)) const B; // undefined behavior
|
| 490 |
+
}
|
| 491 |
+
```
|
| 492 |
+
|
| 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 |
+
|
| 504 |
+
When storage for an object with automatic or dynamic storage duration is
|
| 505 |
+
obtained, the object has an *indeterminate value*, and if no
|
| 506 |
+
initialization is performed for the object, that object retains an
|
| 507 |
+
indeterminate value until that value is replaced [[expr.ass]].
|
| 508 |
+
|
| 509 |
+
[*Note 1*: Objects with static or thread storage duration are
|
| 510 |
+
zero-initialized, see [[basic.start.static]]. — *end note*]
|
| 511 |
+
|
| 512 |
+
If an indeterminate value is produced by an evaluation, the behavior is
|
| 513 |
+
undefined except in the following cases:
|
| 514 |
+
|
| 515 |
+
- If an indeterminate value of unsigned ordinary character type
|
| 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
|
| 529 |
+
`std::byte` type is produced by the evaluation of the right operand of
|
| 530 |
+
a simple assignment operator [[expr.ass]] whose first operand is an
|
| 531 |
+
lvalue of unsigned ordinary character type or `std::byte` type, an
|
| 532 |
+
indeterminate value replaces the value of the object referred to by
|
| 533 |
+
the left operand.
|
| 534 |
+
- If an indeterminate value of unsigned ordinary character type is
|
| 535 |
+
produced by the evaluation of the initialization expression when
|
| 536 |
+
initializing an object of unsigned ordinary character type, that
|
| 537 |
+
object is initialized to 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 initialization
|
| 540 |
+
expression when initializing an object of `std::byte` type, that
|
| 541 |
+
object is initialized to an indeterminate value.
|
| 542 |
+
|
| 543 |
+
[*Example 1*:
|
| 544 |
+
|
| 545 |
+
``` cpp
|
| 546 |
+
int f(bool b) {
|
| 547 |
+
unsigned char c;
|
| 548 |
+
unsigned char d = c; // OK, d has an indeterminate value
|
| 549 |
+
int e = d; // undefined behavior
|
| 550 |
+
return b ? d : 0; // undefined behavior if b is true
|
| 551 |
+
}
|
| 552 |
+
```
|
| 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 |
+
|
| 563 |
+
- static storage duration
|
| 564 |
+
- thread storage duration
|
| 565 |
+
- automatic storage duration
|
| 566 |
+
- dynamic storage duration
|
| 567 |
+
|
| 568 |
+
Static, thread, and automatic storage durations are associated with
|
| 569 |
+
objects introduced by declarations [[basic.def]] and implicitly created
|
| 570 |
+
by the implementation [[class.temporary]]. The dynamic storage duration
|
| 571 |
+
is associated with objects created by a *new-expression* [[expr.new]].
|
| 572 |
+
|
| 573 |
+
The storage duration categories apply to references as well.
|
| 574 |
+
|
| 575 |
+
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
|
| 609 |
+
duration of the thread in which they are created. There is a distinct
|
| 610 |
+
object or reference per thread, and use of the declared name refers to
|
| 611 |
+
the entity associated with the current thread.
|
| 612 |
+
|
| 613 |
+
[*Note 1*: A variable with thread storage duration is initialized as
|
| 614 |
+
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
|
| 628 |
+
destructor with side effects, an implementation shall not destroy it
|
| 629 |
+
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 |
+
|
| 660 |
+
void operator delete(void*) noexcept;
|
| 661 |
+
void operator delete(void*, std::size_t) noexcept;
|
| 662 |
+
void operator delete(void*, std::align_val_t) noexcept;
|
| 663 |
+
void operator delete(void*, std::size_t, std::align_val_t) noexcept;
|
| 664 |
+
|
| 665 |
+
[[nodiscard]] void* operator new[](std::size_t);
|
| 666 |
+
[[nodiscard]] void* operator new[](std::size_t, std::align_val_t);
|
| 667 |
+
|
| 668 |
+
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
|
| 691 |
+
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
|
| 714 |
+
allocated by successive calls to an allocation function are unspecified.
|
| 715 |
+
Even if the size of the space requested is zero, the request can fail.
|
| 716 |
+
If the request succeeds, the value returned by a replaceable allocation
|
| 717 |
+
function is a non-null pointer value [[basic.compound]] `p0` different
|
| 718 |
+
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 |
+
|
| 730 |
+
- If the allocation function takes an argument of type
|
| 731 |
+
`std::align_val_t`, the storage will have the alignment specified by
|
| 732 |
+
the value of this argument.
|
| 733 |
+
- Otherwise, if the allocation function is named `operator new[]`, the
|
| 734 |
+
storage is aligned for any object that does not have new-extended
|
| 735 |
+
alignment [[basic.align]] and is no larger than the requested size.
|
| 736 |
+
- Otherwise, the storage is aligned for any object that does not have
|
| 737 |
+
new-extended alignment and is of the requested size.
|
| 738 |
+
|
| 739 |
+
An allocation function that fails to allocate storage can invoke the
|
| 740 |
+
currently installed new-handler function [[new.handler]], if any.
|
| 741 |
+
|
| 742 |
+
[*Note 3*: A program-supplied allocation function can obtain the
|
| 743 |
+
address of the currently installed `new_handler` using the
|
| 744 |
+
`std::get_new_handler` function [[get.new.handler]]. — *end note*]
|
| 745 |
+
|
| 746 |
+
An allocation function that has a non-throwing exception specification
|
| 747 |
+
[[except.spec]] indicates failure by returning a null pointer value. Any
|
| 748 |
+
other allocation function never returns a null pointer value and
|
| 749 |
+
indicates failure only by throwing an exception [[except.throw]] of a
|
| 750 |
+
type that would match a handler [[except.handle]] of type
|
| 751 |
+
`std::bad_alloc` [[bad.alloc]].
|
| 752 |
+
|
| 753 |
+
A global allocation function is only called as the result of a new
|
| 754 |
+
expression [[expr.new]], or called directly using the function call
|
| 755 |
+
syntax [[expr.call]], or called indirectly to allocate storage for a
|
| 756 |
+
coroutine state [[dcl.fct.def.coroutine]], or called indirectly through
|
| 757 |
+
calls to the functions in the C++ standard library.
|
| 758 |
+
|
| 759 |
+
[*Note 4*: In particular, a global allocation function is not called to
|
| 760 |
+
allocate storage for objects with static storage duration
|
| 761 |
+
[[basic.stc.static]], for objects or references with thread storage
|
| 762 |
+
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`.
|
| 777 |
+
|
| 778 |
+
[*Note 5*: Array deletion cannot use a destroying operator
|
| 779 |
+
delete. — *end note*]
|
| 780 |
+
|
| 781 |
+
Each deallocation function shall return `void`. If the function is a
|
| 782 |
+
destroying operator delete declared in class type `C`, the type of its
|
| 783 |
+
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
|
| 795 |
+
parameter. A deallocation function template shall have two or more
|
| 796 |
+
function parameters. A template instance is never a usual deallocation
|
| 797 |
+
function, regardless of its signature.
|
| 798 |
+
|
| 799 |
+
If a deallocation function terminates by throwing an exception, the
|
| 800 |
+
behavior is undefined. The value of the first argument supplied to a
|
| 801 |
+
deallocation function may be a null pointer value; if so, and if the
|
| 802 |
+
deallocation function is one supplied in the standard library, the call
|
| 803 |
+
has no effect.
|
| 804 |
+
|
| 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 |
+
|
| 900 |
+
``` cpp
|
| 901 |
+
struct B { long double d; };
|
| 902 |
+
struct D : virtual B { char c; };
|
| 903 |
+
```
|
| 904 |
+
|
| 905 |
+
When `D` is the type of a complete object, it will have a subobject of
|
| 906 |
+
type `B`, so it must be aligned appropriately for a `long double`. If
|
| 907 |
+
`D` appears as a subobject of another object that also has `B` as a
|
| 908 |
+
virtual base class, the `B` subobject might be part of a different
|
| 909 |
+
subobject, reducing the alignment requirements on the `D` subobject.
|
| 910 |
+
|
| 911 |
+
— *end example*]
|
| 912 |
+
|
| 913 |
+
The result of the `alignof` operator reflects the alignment requirement
|
| 914 |
+
of the type in the complete-object case.
|
| 915 |
+
|
| 916 |
+
An *extended alignment* is represented by an alignment greater than
|
| 917 |
+
`alignof(std::max_align_t)`. It is *implementation-defined* whether any
|
| 918 |
+
extended alignments are supported and the contexts in which they are
|
| 919 |
+
supported [[dcl.align]]. A type having an extended alignment requirement
|
| 920 |
+
is an *over-aligned type*.
|
| 921 |
+
|
| 922 |
+
[*Note 1*: Every over-aligned type is or contains a class type to which
|
| 923 |
+
extended alignment applies (possibly through a non-static data
|
| 924 |
+
member). — *end note*]
|
| 925 |
+
|
| 926 |
+
A *new-extended alignment* is represented by an alignment greater than
|
| 927 |
+
`__STDCPP_DEFAULT_NEW_ALIGNMENT__` [[cpp.predefined]].
|
| 928 |
+
|
| 929 |
+
Alignments are represented as values of the type `std::size_t`. Valid
|
| 930 |
+
alignments include only those values returned by an `alignof` expression
|
| 931 |
+
for the fundamental types plus an additional *implementation-defined*
|
| 932 |
+
set of values, which may be empty. Every alignment value shall be a
|
| 933 |
+
non-negative integral power of two.
|
| 934 |
+
|
| 935 |
+
Alignments have an order from *weaker* to *stronger* or *stricter*
|
| 936 |
+
alignments. Stricter alignments have larger alignment values. An address
|
| 937 |
+
that satisfies an alignment requirement also satisfies any weaker valid
|
| 938 |
+
alignment requirement.
|
| 939 |
+
|
| 940 |
+
The alignment requirement of a complete type can be queried using an
|
| 941 |
+
`alignof` expression [[expr.alignof]]. Furthermore, the narrow character
|
| 942 |
+
types [[basic.fundamental]] shall have the weakest alignment
|
| 943 |
+
requirement.
|
| 944 |
+
|
| 945 |
+
[*Note 2*: This enables the ordinary character types to be used as the
|
| 946 |
+
underlying type for an aligned memory area [[dcl.align]]. — *end note*]
|
| 947 |
+
|
| 948 |
+
Comparing alignments is meaningful and provides the obvious results:
|
| 949 |
+
|
| 950 |
+
- Two alignments are equal when their numeric values are equal.
|
| 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 |
+
|
| 1008 |
+
Consider the following code:
|
| 1009 |
+
|
| 1010 |
+
``` cpp
|
| 1011 |
+
class X {
|
| 1012 |
+
public:
|
| 1013 |
+
X(int);
|
| 1014 |
+
X(const X&);
|
| 1015 |
+
X& operator=(const X&);
|
| 1016 |
+
~X();
|
| 1017 |
+
};
|
| 1018 |
+
|
| 1019 |
+
class Y {
|
| 1020 |
+
public:
|
| 1021 |
+
Y(int);
|
| 1022 |
+
Y(Y&&);
|
| 1023 |
+
~Y();
|
| 1024 |
+
};
|
| 1025 |
+
|
| 1026 |
+
X f(X);
|
| 1027 |
+
Y g(Y);
|
| 1028 |
+
|
| 1029 |
+
void h() {
|
| 1030 |
+
X a(1);
|
| 1031 |
+
X b = f(X(2));
|
| 1032 |
+
Y c = g(Y(3));
|
| 1033 |
+
a = f(a);
|
| 1034 |
+
}
|
| 1035 |
+
```
|
| 1036 |
+
|
| 1037 |
+
`X(2)` is constructed in the space used to hold `f()`’s argument and
|
| 1038 |
+
`Y(3)` is constructed in the space used to hold `g()`’s argument.
|
| 1039 |
+
Likewise, `f()`’s result is constructed directly in `b` and `g()`’s
|
| 1040 |
+
result is constructed directly in `c`. On the other hand, the expression
|
| 1041 |
+
`a = f(a)` requires a temporary for the result of `f(a)`, which is
|
| 1042 |
+
materialized so that the reference parameter of `X::operator=(const X&)`
|
| 1043 |
+
can bind to it.
|
| 1044 |
+
|
| 1045 |
+
— *end example*]
|
| 1046 |
+
|
| 1047 |
+
When an object of class type `X` is passed to or returned from a
|
| 1048 |
+
function, if `X` has at least one eligible copy or move constructor
|
| 1049 |
+
[[special]], each such constructor is trivial, and the destructor of `X`
|
| 1050 |
+
is either trivial or deleted, implementations are permitted to create a
|
| 1051 |
+
temporary object to hold the function parameter or result object. The
|
| 1052 |
+
temporary object is constructed from the function argument or return
|
| 1053 |
+
value, respectively, and the function’s parameter or return object is
|
| 1054 |
+
initialized as if by using the eligible trivial constructor to copy the
|
| 1055 |
+
temporary (even if that constructor is inaccessible or would not be
|
| 1056 |
+
selected by overload resolution to perform a copy or move of the
|
| 1057 |
+
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,
|
| 1096 |
+
- a class member access [[expr.ref]] using the `.` operator where the
|
| 1097 |
+
left operand is one of these expressions and the right operand
|
| 1098 |
+
designates a non-static data member of non-reference type,
|
| 1099 |
+
- a pointer-to-member operation [[expr.mptr.oper]] using the `.*`
|
| 1100 |
+
operator where the left operand is one of these expressions and the
|
| 1101 |
+
right operand is a pointer to data member of non-reference type,
|
| 1102 |
+
- a
|
| 1103 |
+
- `const_cast` [[expr.const.cast]],
|
| 1104 |
+
- `static_cast` [[expr.static.cast]],
|
| 1105 |
+
- `dynamic_cast` [[expr.dynamic.cast]], or
|
| 1106 |
+
- `reinterpret_cast` [[expr.reinterpret.cast]]
|
| 1107 |
+
|
| 1108 |
+
converting, without a user-defined conversion, a glvalue operand that
|
| 1109 |
+
is one of these expressions to a glvalue that refers to the object
|
| 1110 |
+
designated by the operand, or to its complete object or a subobject
|
| 1111 |
+
thereof,
|
| 1112 |
+
- a conditional expression [[expr.cond]] that is a glvalue where the
|
| 1113 |
+
second or third operand is one of these expressions, or
|
| 1114 |
+
- a comma expression [[expr.comma]] that is a glvalue where the right
|
| 1115 |
+
operand is one of these expressions.
|
| 1116 |
+
|
| 1117 |
+
[*Example 2*:
|
| 1118 |
+
|
| 1119 |
+
``` cpp
|
| 1120 |
+
template<typename T> using id = T;
|
| 1121 |
+
|
| 1122 |
+
int i = 1;
|
| 1123 |
+
int&& a = id<int[3]>{1, 2, 3}[i]; // temporary array has same lifetime as a
|
| 1124 |
+
const int& b = static_cast<const int&>(0); // temporary int has same lifetime as b
|
| 1125 |
+
int&& c = cond ? id<int[3]>{1, 2, 3}[i] : static_cast<int&&>(0);
|
| 1126 |
+
// exactly one of the two temporaries is lifetime-extended
|
| 1127 |
+
```
|
| 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
|
| 1139 |
+
const int& x = (const int&)1; // temporary for value 1 has same lifetime as x
|
| 1140 |
+
```
|
| 1141 |
+
|
| 1142 |
+
— *end example*]
|
| 1143 |
+
|
| 1144 |
+
— *end note*]
|
| 1145 |
+
|
| 1146 |
+
[*Note 6*:
|
| 1147 |
+
|
| 1148 |
+
If a temporary object has a reference member initialized by another
|
| 1149 |
+
temporary object, lifetime extension applies recursively to such a
|
| 1150 |
+
member’s initializer.
|
| 1151 |
+
|
| 1152 |
+
[*Example 4*:
|
| 1153 |
+
|
| 1154 |
+
``` cpp
|
| 1155 |
+
struct S {
|
| 1156 |
+
const int& m;
|
| 1157 |
+
};
|
| 1158 |
+
const S& s = S{1}; // both S and int temporaries have lifetime of s
|
| 1159 |
+
```
|
| 1160 |
+
|
| 1161 |
+
— *end example*]
|
| 1162 |
+
|
| 1163 |
+
— *end note*]
|
| 1164 |
+
|
| 1165 |
+
The exceptions to this lifetime rule are:
|
| 1166 |
+
|
| 1167 |
+
- A temporary object bound to a reference parameter in a function call
|
| 1168 |
+
[[expr.call]] persists until the completion of the full-expression
|
| 1169 |
+
containing the call.
|
| 1170 |
+
- A temporary object bound to a reference element of an aggregate of
|
| 1171 |
+
class type initialized from a parenthesized *expression-list*
|
| 1172 |
+
[[dcl.init]] persists until the completion of the full-expression
|
| 1173 |
+
containing the *expression-list*.
|
| 1174 |
+
- The lifetime of a temporary bound to the returned value in a function
|
| 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 {
|
| 1210 |
+
S();
|
| 1211 |
+
S(int);
|
| 1212 |
+
friend S operator+(const S&, const S&);
|
| 1213 |
+
~S();
|
| 1214 |
+
};
|
| 1215 |
+
S obj1;
|
| 1216 |
+
const S& cr = S(16)+S(23);
|
| 1217 |
+
S obj2;
|
| 1218 |
+
```
|
| 1219 |
+
|
| 1220 |
+
The expression `S(16) + S(23)` creates three temporaries: a first
|
| 1221 |
+
temporary `T1` to hold the result of the expression `S(16)`, a second
|
| 1222 |
+
temporary `T2` to hold the result of the expression `S(23)`, and a third
|
| 1223 |
+
temporary `T3` to hold the result of the addition of these two
|
| 1224 |
+
expressions. The temporary `T3` is then bound to the reference `cr`. It
|
| 1225 |
+
is unspecified whether `T1` or `T2` is created first. On an
|
| 1226 |
+
implementation where `T1` is created before `T2`, `T2` shall be
|
| 1227 |
+
destroyed before `T1`. The temporaries `T1` and `T2` are bound to the
|
| 1228 |
+
reference parameters of `operator+`; these temporaries are destroyed at
|
| 1229 |
+
the end of the full-expression containing the call to `operator+`. The
|
| 1230 |
+
temporary `T3` bound to the reference `cr` is destroyed at the end of
|
| 1231 |
+
`cr`’s lifetime, that is, at the end of the program. In addition, the
|
| 1232 |
+
order in which `T3` is destroyed takes into account the destruction
|
| 1233 |
+
order of other objects with static storage duration. That is, because
|
| 1234 |
+
`obj1` is constructed before `T3`, and `T3` is constructed before
|
| 1235 |
+
`obj2`, `obj2` shall be destroyed before `T3`, and `T3` shall be
|
| 1236 |
+
destroyed before `obj1`.
|
| 1237 |
+
|
| 1238 |
+
— *end example*]
|
| 1239 |
+
|