Lifetime

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.

# Notes

Until the resolution of CWG issue 2256, the end of lifetime rules are different between non-class objects (end of storage duration) and class objects (reverse order of construction):

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:

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
CWG 119C++98an object of a class type with a non-trivial constructor canonly start its lifetime when the constructor call has completedlifetime also startedfor other initializations
CWG 201C++98lifetime of a temporary object in a default argumentof a default constructor was required to endwhen the initialization of the array completeslifetime ends beforeinitializing the nextelement (also resolvesCWG issue 124)
CWG 274C++98an lvalue designating an out-of-lifetime object could beused as the operand of static_cast only if the conversionwas ultimately to cv-unqualified char& or unsigned char&cv-qualified char&and unsigned char&also allowed
CWG 597C++98the following behaviors were undefined:1. a pointer to an out-of-lifetime object is implicitlyconverted to a pointer to a non-virtual base class2. an lvalue referring to an out-of-lifetime objectis bound to a reference to a non-virtual base class3. an lvalue referring to an out-of-lifetime object is usedas the operand of a static_cast (with a few exceptions)made well-defined
CWG 2012C++98lifetime of references was specified to match storage duration,requiring that extern references are alive before their initializers runlifetime beginsat initialization
CWG 2107C++98the resolution of CWG issue 124 was not applied to copy constructorsapplied
CWG 2256C++98lifetime of trivially destructible objects were inconsistent with other objectsmade consistent
CWG 2470C++98more than one arrays could provide storage for the same objectonly one provides
CWG 2489C++98char[] cannot provide storage, but objectscould be implicitly created within its storageobjects cannot beimplicitly created within the storage of char[]
CWG 2527C++98if a destructor is not invoked because of reusing storage and theprogram depends on its side effects, the behavior was undefinedthe behavior is well-defined in this case
CWG 2721C++98the exact time point of storage reuse was unclear for placement newmade clear
CWG 2849C++23function parameter objects were considered as temporaryobjects for range-for loop temporary object lifetime extensionnot considered astemporary objects
CWG 2854C++98exception objects were temporary objectsthey are nottemporary objects
CWG 2867C++17the lifetime of temporary objects created instructured binding declarations were not extendedextended to the endof the declaration
P0137R1C++98creating an object in an array of unsigned char reused its storageits storage is not reused
P0593R6C++98a pseudo-destructor call had no effectsit destroys the object
P1971R0C++98a non-static data member of a const-qualified type or a reference typeprevented its containing object from being transparently replaceablerestriction removed
P2103R0C++98transparently replaceability did not require keeping the original structurerequires

# See also