- tmp/tmp0ilj2cna/{from.md → to.md} +125 -67
tmp/tmp0ilj2cna/{from.md → to.md}
RENAMED
|
@@ -1,77 +1,57 @@
|
|
| 1 |
-
##
|
| 2 |
|
| 3 |
The constructs in a C++ program create, destroy, refer to, access, and
|
| 4 |
-
manipulate objects. An *object* is created by a definition
|
| 5 |
-
[[basic.def]]
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
[*Note 1*: A function is not an object, regardless of whether or not it
|
| 13 |
occupies storage in the way that objects do. — *end note*]
|
| 14 |
|
| 15 |
The properties of an object are determined when the object is created.
|
| 16 |
-
An object can have a name
|
| 17 |
-
duration
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
Objects can contain other objects, called *subobjects*. A subobject can
|
| 26 |
-
be a *member subobject*
|
| 27 |
-
|
| 28 |
-
|
| 29 |
object is created in storage associated with a member subobject or array
|
| 30 |
element *e* (which may or may not be within its lifetime), the created
|
| 31 |
object is a subobject of *e*’s containing object if:
|
| 32 |
|
| 33 |
- the lifetime of *e*’s containing object has begun and not ended, and
|
| 34 |
- the storage for the new object exactly overlays the storage location
|
| 35 |
associated with *e*, and
|
| 36 |
- the new object is of the same type as *e* (ignoring cv-qualification).
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
[*Example 1*:
|
| 43 |
-
|
| 44 |
-
``` cpp
|
| 45 |
-
struct X { const int n; };
|
| 46 |
-
union U { X x; float f; };
|
| 47 |
-
void tong() {
|
| 48 |
-
U u = {{ 1 }};
|
| 49 |
-
u.f = 5.f; // OK, creates new subobject of u ([class.union])
|
| 50 |
-
X *p = new (&u.x) X {2}; // OK, creates new subobject of u
|
| 51 |
-
assert(p->n == 2); // OK
|
| 52 |
-
assert(*std::launder(&u.x.n) == 2); // OK
|
| 53 |
-
assert(u.x.n == 2); // undefined behavior, u.x does not name new subobject
|
| 54 |
-
}
|
| 55 |
-
```
|
| 56 |
-
|
| 57 |
-
— *end example*]
|
| 58 |
-
|
| 59 |
-
If a complete object is created ([[expr.new]]) in storage associated
|
| 60 |
-
with another object *e* of type “array of N `unsigned char`” or of type
|
| 61 |
-
“array of N `std::byte`” ([[cstddef.syn]]), that array *provides
|
| 62 |
-
storage* for the created object if:
|
| 63 |
|
| 64 |
- the lifetime of *e* has begun and not ended, and
|
| 65 |
- the storage for the new object fits entirely within *e*, and
|
| 66 |
- there is no smaller array object that satisfies these constraints.
|
| 67 |
|
| 68 |
-
[*Note
|
| 69 |
another object, the lifetime of that object ends because its storage was
|
| 70 |
-
reused
|
| 71 |
|
| 72 |
-
[*Example
|
| 73 |
|
| 74 |
``` cpp
|
| 75 |
template<typename ...T>
|
| 76 |
struct AlignedUnion {
|
| 77 |
alignas(T...) unsigned char data[max(sizeof(T)...)];
|
|
@@ -108,38 +88,116 @@ of* `x`, determined as follows:
|
|
| 108 |
- If `x` is a complete object, then the complete object of `x` is
|
| 109 |
itself.
|
| 110 |
- Otherwise, the complete object of `x` is the complete object of the
|
| 111 |
(unique) object that contains `x`.
|
| 112 |
|
| 113 |
-
If a complete object, a data member
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
|
| 119 |
-
|
| 120 |
-
have a nonzero size and shall occupy one or more bytes of storage. Base
|
| 121 |
-
class subobjects may have zero size. An object of trivially copyable or
|
| 122 |
-
standard-layout type ([[basic.types]]) shall occupy contiguous bytes of
|
| 123 |
-
storage.
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
bit-fields may have the same address if one is nested within the other,
|
| 129 |
-
or if at least one is a base class subobject of zero size and they are
|
| 130 |
-
of different types; otherwise, they have distinct addresses.[^5]
|
| 131 |
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
``` cpp
|
| 135 |
static const char test1 = 'x';
|
| 136 |
static const char test2 = 'x';
|
| 137 |
const bool b = &test1 != &test2; // always true
|
| 138 |
```
|
| 139 |
|
| 140 |
— *end example*]
|
| 141 |
|
| 142 |
-
|
| 143 |
-
of
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
|
|
|
| 1 |
+
### Object model <a id="intro.object">[[intro.object]]</a>
|
| 2 |
|
| 3 |
The constructs in a C++ program create, destroy, refer to, access, and
|
| 4 |
+
manipulate objects. An *object* is created by a definition
|
| 5 |
+
[[basic.def]], by a *new-expression* [[expr.new]], by an operation that
|
| 6 |
+
implicitly creates objects (see below), when implicitly changing the
|
| 7 |
+
active member of a union [[class.union]], or when a temporary object is
|
| 8 |
+
created ([[conv.rval]], [[class.temporary]]). An object occupies a
|
| 9 |
+
region of storage in its period of construction [[class.cdtor]],
|
| 10 |
+
throughout its lifetime [[basic.life]], and in its period of destruction
|
| 11 |
+
[[class.cdtor]].
|
| 12 |
|
| 13 |
[*Note 1*: A function is not an object, regardless of whether or not it
|
| 14 |
occupies storage in the way that objects do. — *end note*]
|
| 15 |
|
| 16 |
The properties of an object are determined when the object is created.
|
| 17 |
+
An object can have a name [[basic.pre]]. An object has a storage
|
| 18 |
+
duration [[basic.stc]] which influences its lifetime [[basic.life]]. An
|
| 19 |
+
object has a type [[basic.types]]. Some objects are polymorphic
|
| 20 |
+
[[class.virtual]]; the implementation generates information associated
|
| 21 |
+
with each such object that makes it possible to determine that object’s
|
| 22 |
+
type during program execution. For other objects, the interpretation of
|
| 23 |
+
the values found therein is determined by the type of the *expression*s
|
| 24 |
+
[[expr.compound]] used to access them.
|
| 25 |
|
| 26 |
Objects can contain other objects, called *subobjects*. A subobject can
|
| 27 |
+
be a *member subobject* [[class.mem]], a *base class subobject*
|
| 28 |
+
[[class.derived]], or an array element. An object that is not a
|
| 29 |
+
subobject of any other object is called a *complete object*. If an
|
| 30 |
object is created in storage associated with a member subobject or array
|
| 31 |
element *e* (which may or may not be within its lifetime), the created
|
| 32 |
object is a subobject of *e*’s containing object if:
|
| 33 |
|
| 34 |
- the lifetime of *e*’s containing object has begun and not ended, and
|
| 35 |
- the storage for the new object exactly overlays the storage location
|
| 36 |
associated with *e*, and
|
| 37 |
- the new object is of the same type as *e* (ignoring cv-qualification).
|
| 38 |
|
| 39 |
+
If a complete object is created [[expr.new]] in storage associated with
|
| 40 |
+
another object *e* of type “array of N `unsigned char`” or of type
|
| 41 |
+
“array of N `std::byte`” [[cstddef.syn]], that array *provides storage*
|
| 42 |
+
for the created object if:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
- the lifetime of *e* has begun and not ended, and
|
| 45 |
- the storage for the new object fits entirely within *e*, and
|
| 46 |
- there is no smaller array object that satisfies these constraints.
|
| 47 |
|
| 48 |
+
[*Note 2*: If that portion of the array previously provided storage for
|
| 49 |
another object, the lifetime of that object ends because its storage was
|
| 50 |
+
reused [[basic.life]]. — *end note*]
|
| 51 |
|
| 52 |
+
[*Example 1*:
|
| 53 |
|
| 54 |
``` cpp
|
| 55 |
template<typename ...T>
|
| 56 |
struct AlignedUnion {
|
| 57 |
alignas(T...) unsigned char data[max(sizeof(T)...)];
|
|
|
|
| 88 |
- If `x` is a complete object, then the complete object of `x` is
|
| 89 |
itself.
|
| 90 |
- Otherwise, the complete object of `x` is the complete object of the
|
| 91 |
(unique) object that contains `x`.
|
| 92 |
|
| 93 |
+
If a complete object, a data member [[class.mem]], or an array element
|
| 94 |
+
is of class type, its type is considered the *most derived class*, to
|
| 95 |
+
distinguish it from the class type of any base class subobject; an
|
| 96 |
+
object of a most derived class type or of a non-class type is called a
|
| 97 |
+
*most derived object*.
|
| 98 |
|
| 99 |
+
A *potentially-overlapping subobject* is either:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
- a base class subobject, or
|
| 102 |
+
- a non-static data member declared with the `no_unique_address`
|
| 103 |
+
attribute [[dcl.attr.nouniqueaddr]].
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
An object has nonzero size if it
|
| 106 |
+
|
| 107 |
+
- is not a potentially-overlapping subobject, or
|
| 108 |
+
- is not of class type, or
|
| 109 |
+
- is of a class type with virtual member functions or virtual base
|
| 110 |
+
classes, or
|
| 111 |
+
- has subobjects of nonzero size or bit-fields of nonzero length.
|
| 112 |
+
|
| 113 |
+
Otherwise, if the object is a base class subobject of a standard-layout
|
| 114 |
+
class type with no non-static data members, it has zero size. Otherwise,
|
| 115 |
+
the circumstances under which the object has zero size are
|
| 116 |
+
*implementation-defined*. Unless it is a bit-field [[class.bit]], an
|
| 117 |
+
object with nonzero size shall occupy one or more bytes of storage,
|
| 118 |
+
including every byte that is occupied in full or in part by any of its
|
| 119 |
+
subobjects. An object of trivially copyable or standard-layout type
|
| 120 |
+
[[basic.types]] shall occupy contiguous bytes of storage.
|
| 121 |
+
|
| 122 |
+
Unless an object is a bit-field or a subobject of zero size, the address
|
| 123 |
+
of that object is the address of the first byte it occupies. Two objects
|
| 124 |
+
with overlapping lifetimes that are not bit-fields may have the same
|
| 125 |
+
address if one is nested within the other, or if at least one is a
|
| 126 |
+
subobject of zero size and they are of different types; otherwise, they
|
| 127 |
+
have distinct addresses and occupy disjoint bytes of storage.[^10]
|
| 128 |
+
|
| 129 |
+
[*Example 2*:
|
| 130 |
|
| 131 |
``` cpp
|
| 132 |
static const char test1 = 'x';
|
| 133 |
static const char test2 = 'x';
|
| 134 |
const bool b = &test1 != &test2; // always true
|
| 135 |
```
|
| 136 |
|
| 137 |
— *end example*]
|
| 138 |
|
| 139 |
+
The address of a non-bit-field subobject of zero size is the address of
|
| 140 |
+
an unspecified byte of storage occupied by the complete object of that
|
| 141 |
+
subobject.
|
| 142 |
+
|
| 143 |
+
Some operations are described as *implicitly creating objects* within a
|
| 144 |
+
specified region of storage. For each operation that is specified as
|
| 145 |
+
implicitly creating objects, that operation implicitly creates and
|
| 146 |
+
starts the lifetime of zero or more objects of implicit-lifetime types
|
| 147 |
+
[[basic.types]] in its specified region of storage if doing so would
|
| 148 |
+
result in the program having defined behavior. If no such set of objects
|
| 149 |
+
would give the program defined behavior, the behavior of the program is
|
| 150 |
+
undefined. If multiple such sets of objects would give the program
|
| 151 |
+
defined behavior, it is unspecified which such set of objects is
|
| 152 |
+
created.
|
| 153 |
+
|
| 154 |
+
[*Note 3*: Such operations do not start the lifetimes of subobjects of
|
| 155 |
+
such objects that are not themselves of implicit-lifetime
|
| 156 |
+
types. — *end note*]
|
| 157 |
+
|
| 158 |
+
Further, after implicitly creating objects within a specified region of
|
| 159 |
+
storage, some operations are described as producing a pointer to a
|
| 160 |
+
*suitable created object*. These operations select one of the
|
| 161 |
+
implicitly-created objects whose address is the address of the start of
|
| 162 |
+
the region of storage, and produce a pointer value that points to that
|
| 163 |
+
object, if that value would result in the program having defined
|
| 164 |
+
behavior. If no such pointer value would give the program defined
|
| 165 |
+
behavior, the behavior of the program is undefined. If multiple such
|
| 166 |
+
pointer values would give the program defined behavior, it is
|
| 167 |
+
unspecified which such pointer value is produced.
|
| 168 |
+
|
| 169 |
+
[*Example 3*:
|
| 170 |
+
|
| 171 |
+
``` cpp
|
| 172 |
+
#include <cstdlib>
|
| 173 |
+
struct X { int a, b; };
|
| 174 |
+
X *make_x() {
|
| 175 |
+
// The call to std::malloc implicitly creates an object of type X
|
| 176 |
+
// and its subobjects a and b, and returns a pointer to that X object
|
| 177 |
+
// (or an object that is pointer-interconvertible[basic.compound] with it),
|
| 178 |
+
// in order to give the subsequent class member access operations
|
| 179 |
+
// defined behavior.
|
| 180 |
+
X *p = (X*)std::malloc(sizeof(struct X));
|
| 181 |
+
p->a = 1;
|
| 182 |
+
p->b = 2;
|
| 183 |
+
return p;
|
| 184 |
+
}
|
| 185 |
+
```
|
| 186 |
+
|
| 187 |
+
— *end example*]
|
| 188 |
+
|
| 189 |
+
An operation that begins the lifetime of an array of `char`,
|
| 190 |
+
`unsigned char`, or `std::byte` implicitly creates objects within the
|
| 191 |
+
region of storage occupied by the array.
|
| 192 |
+
|
| 193 |
+
[*Note 4*: The array object provides storage for these
|
| 194 |
+
objects. — *end note*]
|
| 195 |
+
|
| 196 |
+
Any implicit or explicit invocation of a function named `operator new`
|
| 197 |
+
or `operator new[]` implicitly creates objects in the returned region of
|
| 198 |
+
storage and returns a pointer to a suitable created object.
|
| 199 |
+
|
| 200 |
+
[*Note 5*: Some functions in the C++ standard library implicitly create
|
| 201 |
+
objects ([[allocator.traits.members]], [[c.malloc]], [[cstring.syn]],
|
| 202 |
+
[[bit.cast]]). — *end note*]
|
| 203 |
|