Every object and reference has a lifetime, which is a runtime property: for any object or reference, there is a point of execution of a program when its lifetime begins, and there is a moment when it ends.
The lifetime of an object begins when:
std::allocator::allocate
. Some operations implicitly create objects of implicit-lifetime types in given region of storage and start their lifetime. If a subobject of an implicitly created object is not of an implicit-lifetime type, its lifetime does not begin implicitly.
The lifetime of an object ends when:
Lifetime of an object is equal to or is nested within the lifetime of its storage, see storage duration.
The lifetime of a reference begins when its initialization is complete and ends as if it were a scalar object.
Note: the lifetime of the referred object may end before the end of the lifetime of the reference, which makes dangling references possible.
Lifetimes of non-static data members and base subobjects begin and end following class initialization order.
Temporary objects are created when a prvalue is materialized so that it can be used as a glvalue, which occurs (since C++17) in the following situations:
| (since C++11) |
| (until C++17) | ||
| (since C++17) |
Also, temporary objects are created:
| (since C++17) |
The materialization of a temporary object is generally delayed as long as possible in order to avoid creating unnecessary temporary object: see copy elision. | (since C++17) |
All temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created, and if multiple temporary objects were created, they are destroyed in the order opposite to the order of creation. This is true even if that evaluation ends in throwing an exception.
There are the following exceptions from that:
| (since C++23) |
A program is not required to call the destructor of an object to end its lifetime if the object is trivially-destructible (be careful that the correct behavior of the program may depend on the destructor). However, if a program ends the lifetime of a non-trivially destructible object that is a variable explicitly, it must ensure that a new object of the same type is constructed in-place (e.g. via placement new) before the destructor may be called implicitly, i.e. due to scope exit or exception for automatic objects, due to thread exit for thread-local objects, or due to program exit for static objects; otherwise the behavior is undefined.
class T {}; // trivial struct B { ~B() {} // non-trivial }; void x() { long long n; // automatic, trivial new (&n) double(3.14); // reuse with a different type okay } // okay void h() { B b; // automatic non-trivially destructible b.~B(); // end lifetime (not required, since no side-effects) new (&b) T; // wrong type: okay until the destructor is called } // destructor is called: undefined behavior
It is undefined behavior to reuse storage that is or was occupied by a const complete object of static, thread-local, or automatic storage duration because such objects may be stored in read-only memory.
struct B { B(); // non-trivial ~B(); // non-trivial }; const B b; // const static void h() { b.~B(); // end the lifetime of b new (const_cast<B*>(&b)) const B; // undefined behavior: attempted reuse of a const }
If a new object is created at the address that was occupied by another object, then all pointers, references, and the name of the original object will automatically refer to the new object and, once the lifetime of the new object begins, can be used to manipulate the new object, but only if the original object is transparently replaceable by the new object.
Object x
is transparently replaceable by object y
if:
y
exactly overlays the storage location which x
occupied y
is of the same type as x
(ignoring the top-level cv-qualifiers) x
is not a complete const object x
nor y
is a base class subobject, or a member subobject declared with [[no_unique_address]]
(since C++20) x
and y
are both complete objects, or x
and y
are direct subobjects of objects ox
and oy
respectively, and ox
is transparently replaceable by oy
. struct C { int i; void f(); const C& operator=(const C&); }; const C& C::operator=(const C& other) { if (this != &other) { this->~C(); // lifetime of *this ends new (this) C(other); // new object of type C created f(); // well-defined } return *this; } C c1; C c2; c1 = c2; // well-defined c1.f(); // well-defined; c1 refers to a new object of type C
If the conditions listed above are not met, a valid pointer to the new object may still be obtained by applying the pointer optimization barrier struct A { virtual int transmogrify(); }; struct B : A { int transmogrify() override { ::new(this) A; return 2; } }; inline int A::transmogrify() { ::new(this) B; return 1; } void test() { A i; int n = i.transmogrify(); // int m = i.transmogrify(); // undefined behavior: // the new A object is a base subobject, while the old one is a complete object int m = std::launder(&i)->transmogrify(); // OK assert(m + n == 3); } | (since C++17) |
Similarly, if an object is created in the storage of a class member or array element, the created object is only a subobject (member or element) of the original object's containing object if:
Otherwise, the name of the original subobject cannot be used to access the new object without | (since C++17) |
As a special case, objects can be created in arrays of unsigned char or std::byte
(since C++17) (in which case it is said that the array provides storage for the object) if.
If that portion of the array previously provided storage for another object, the lifetime of that object ends because its storage was reused, however the lifetime of the array itself does not end (its storage is not considered to have been reused).
template<typename... T> struct AlignedUnion { alignas(T...) unsigned char data[max(sizeof(T)...)]; }; int f() { AlignedUnion<int, char> au; int *p = new (au.data) int; // OK, au.data provides storage char *c = new (au.data) char(); // OK, ends lifetime of *p char *d = new (au.data + 1) char(); return *c + *d; // OK }
Before the lifetime of an object has started but after the storage which the object will occupy has been allocated or, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, the behaviors of the following uses of the glvalue expression that identifies that object are undefined, unless the object is being constructed or destructed (separate set of rules applies):
dynamic_cast
or typeid
expressions. The above rules apply to pointers as well (binding a reference to virtual base is replaced by implicit conversion to a pointer to virtual base), with two additional rules:
static_cast
of a pointer to storage without an object is only allowed when casting to (possibly cv-qualified) void*. static_cast
to pointers to possibly cv-qualified char, or possibly cv-qualified unsigned char, or possibly cv-qualified std::byte
(since C++17). During construction and destruction it is generally allowed to call non-static member functions, access non-static data members, and use typeid
and dynamic_cast
. However, because the lifetime either has not begun yet (during construction) or has already ended (during destruction), only specific operations are allowed. For one restriction, see virtual function calls during construction and destruction.
Until the resolution of core issue 2256, the end of lifetime rules are different between non-class objects (end of storage duration) and class objects (reverse order of construction):
struct A { int* p; ~A() { std::cout << *p; } // undefined behavior since CWG2256: n does not outlive a // well-defined until CWG2256: prints 123 }; void f() { A a; int n = 123; // if n did not outlive a, this could have been optimized out (dead store) a.p = &n; }
Until the resolution of RU007, a non-static member of a const-qualified type or a reference type prevents its containing object from being transparently replaceable, which makes std::vector
and std::deque
hard to implement:
struct X { const int n; }; union U { X x; float f; }; void tong() { U u = { {1} }; u.f = 5.f; // OK: creates new subobject of 'u' X *p = new (&u.x) X {2}; // OK: creates new subobject of 'u' assert(p->n == 2); // OK assert(u.x.n == 2); // undefined until RU007: // 'u.x' does not name the new subobject assert(*std::launder(&u.x.n) == 2); // OK even until RU007 }
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 119 | C++98 | an object of a class type with a non-trivial constructor can only start its lifetime when the constructor call has completed | lifetime also started for other initializations |
CWG 201 | C++98 | lifetime of a temporary object in a default argument of a default constructor was required to end when the initialization of the array completes | lifetime ends before initializing the next element (also resolves CWG issue 124) |
CWG 274 | C++98 | an lvalue designating an out-of-lifetime object could be used as the operand of static_cast only if the conversion was ultimately to cv-unqualified char& or unsigned char& | cv-qualified char& and unsigned char& also allowed |
CWG 597 | C++98 | the following behaviors were undefined: 1. a pointer to an out-of-lifetime object is implicitly converted to a pointer to a non-virtual base class 2. an lvalue referring to an out-of-lifetime object is bound to a reference to a non-virtual base class 3. an lvalue referring to an out-of-lifetime object is used as the operand of a static_cast (with a few exceptions) | made well-defined |
CWG 2012 | C++98 | lifetime of references was specified to match storage duration, requiring that extern references are alive before their initializers run | lifetime begins at initialization |
CWG 2107 | C++98 | the resolution of CWG issue 124 was not applied to copy constructors | applied |
CWG 2256 | C++98 | lifetime of trivially destructible objects were inconsistent with other objects | made consistent |
CWG 2470 | C++98 | more than one arrays could provide storage for the same object | only one provides |
CWG 2527 | C++98 | if a destructor is not invoked because of reusing storage and the program depends on its side effects, the behavior was undefined | the behavior is well- defined in this case |
P0137R1 | C++98 | creating an object in an array of unsigned char reused its storage | its storage is not reused |
P0593R6 | C++98 | a pseudo-destructor call had no effects | it destroys the object |
P1971R0 | C++98 | a non-static data member of a const-qualified type or a reference type prevented its containing object from being transparently replaceable | restriction removed |
P2103R0 | C++98 | transparently replaceability did not require keeping the original structure | requires |
C documentation for Lifetime |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/language/lifetime