The
lifetime of an object or reference is a runtime property of the
object or reference
. A variable is said to have
vacuous initialization
if it is default-initialized, no other initialization is performed, and,
if it is of class type or a (possibly multidimensional) array thereof,
a trivial constructor of that class type is selected for
the default-initialization
. The lifetime of an object of type
T begins when:
- storage with the proper alignment and size
for type T is obtained, and
- its initialization (if any) is complete
(including vacuous initialization) ([dcl.init], [class.base.init]),
except that if the object is a union member or subobject thereof,
its lifetime only begins if that union member is the
initialized member in the union (
[dcl.init.aggr],
[class.base.init]),
or as described in
[class.union],
[class.copy.ctor],
[class.copy.assign],
and
[obj.lifetime],
and except as described in
[allocator.members]. The lifetime of an object
o of type
T ends when:
- if T is a non-class type, the object is destroyed, or
- if T is a class type, the destructor call starts, or
- the storage which the object occupies is released,
or is reused by an object that is not nested within o ([intro.object]).
[
Example 1:
struct S {
int m;
};
void f() {
S x{1};
new(&x) S(x.m);
}
—
end example]