# Lifetime

> C++ Atlas: `cppatlas.com/w/cpp/language/lifetime` | Source: https://en.cppreference.com/w/cpp/language/lifetime | Domain: Language | Standards: C++11, C++17, C++20, C++23

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.

## Executive Mental Model (Study Companion)

**Core Intuition:** Allocating bytes does not create an object, and freeing bytes is not what destroys one—lifetime begins at initialization and ends at destruction or storage reuse.

- **Standard Era:** C++11 → C++17 → C++20 → C++23
- **Primary ISO Clauses:** [basic.life], [basic.stc], [class.temporary], [stmt.ranged]
- **CppCon Talk Angle:** Show the two timelines side by side: storage duration (how long the bytes exist) vs. object lifetime (when the typed object is legally alive inside those bytes).
- **Common Audience Trap:** Assuming temporary lifetime extension propagates through accessor methods like auto& x = make_container().front(); inside range-for loops.
- **Stage Takeaway:** Use C++20 std::bit_cast instead of pointer-punning reinterpret_cast, and verify C++23 P2718 before trusting chained temporaries in range-for.

## Memory Map Walkthrough: Virtual Address Space & Scope Lifetime Scrubber

In C++, storage duration and object lifetime are distinct concepts. Storage duration governs when raw memory exists—static, thread-local, automatic on the stack, or dynamic on the heap—while object lifetime begins only when initialization completes and ends when the destructor starts or the storage is reused. Binding a temporary directly to a const lvalue reference or rvalue reference extends its lifetime to the reference's scope, but chaining through a member function or accessor stops lifetime extension cold, creating a classic dangling reference before C++23's range-for fix (P2718R0). Meanwhile, placement-new reusing storage that contained const or reference subobjects requires std::launder so the optimizer does not reuse cached values from the previous object.

### Storage duration is not object lifetime ([basic.life] §6.7.3)

When you allocate raw aligned storage (alignas(Widget) std::byte buf[sizeof(Widget)];), storage exists, yet zero Widget objects live there. Accessing a Widget* pointing to buf before placement-new construction is Undefined Behavior.

C++20 softened one long-standing C interop pain point with Implicit-Lifetime Types ([intro.object]): malloc or std::start_lifetime_as (C++23) can implicitly begin the lifetime of trivial aggregates/arrays so C network buffers no longer rely on theoretical UB.

*Why std::launder exists for const members:*
```cpp
struct Node { const int id; };
Node* p = new Node{1};
// Reuse p's storage with placement new:
new (p) Node{2};
// Compiler may legally assume p->id is STILL 1 because 'id' is const!
int safe_id = std::launder(p)->id; // Guaranteed 2 ([ptr.launder])
```

**Key takeaway:** Compilers track pointer provenance and const member immutability; std::launder tells the optimizer a fresh object replaced the old one at that address.

### Temporary lifetime extension & the range-for cliff ([class.temporary] & [stmt.ranged])

Writing const std::string& s = make_name(); extends the materialized temporary string to match s's scope. Write const char* p = make_name().c_str(); or auto& item : get_wrapper().items() in C++11–C++20, and the outer temporary wrapper is destroyed BEFORE the loop body even begins!

C++23 paper P2718R0 finally fixed range-based for loops by extending the lifetime of ALL temporaries in the range-initializer expression until the loop finishes—matching what every developer already assumed.

*Pre-C++23 range-for dangling temporary:*
```cpp
struct Wrapper { std::vector<int> items{1, 2, 3}; };
Wrapper make_wrapper();

// Pre-C++23: UB! Wrapper temporary dies before loop starts!
// C++23 (P2718R0): Well-defined! All temporaries in range-init extend across loop.
for (int v : make_wrapper().items) {
    use(v);
}
```

**Key takeaway:** Whenever a function returns a reference or view into a temporary receiver (std::string_view, std::span, or .items), check whether you are pre-C++23.

## Official Specification Summary (cppreference)

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:

- storage with the proper alignment and size for its type is obtained, and
- its initialization (if any) is complete (including default initialization via no constructor or trivial default constructor), 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, or it is made active,
  - if the object is nested in a union object, its lifetime may begin if the containing union object is assigned or constructed by a trivial special member function,
  - an array object's lifetime may also begin if it is allocated by `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:

- if it is of a non-class type, the object is destroyed (maybe via a pseudo-destructor call), or
- if it is of 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 it.

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 object lifetime

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:

- binding a reference to a prvalue

(since C++11):

- initializing an object of type `std::initializer_list<T>` from a brace-enclosed initializer list

(until C++17):

- returning a prvalue from a function
- conversion that creates a prvalue (including `T(a, b, c)` and `T{}`)

(since C++11):

- lambda expression

- copy-initialization that requires conversion of the initializer,
- reference-initialization to a different but convertible type or to a bitfield.

(since C++17):

- when performing member access on a class prvalue
- when performing an array-to-pointer conversion or subscripting on an array prvalue
- for unevaluated operands in `sizeof` and `typeid`
- when a prvalue appears as a discarded-value expression

(since C++17):

Also, temporary objects are created:

- if supported by the implementation, when passing or returning an object of trivially-copyable type in a function call expression (this allows passing structs in CPU registers)

The materialization of a temporary object is generally delayed as long as possible in order to avoid creating unnecessary temporary object: see copy elision.

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:

- The lifetime of a temporary object may be extended by binding to a reference, see reference initialization for details.
- The lifetime of a temporary object created when evaluating the default arguments of a default or copy constructor used to initialize or copy an element of an array ends before the next element of the array begins initialization.

(since C++17):

- The lifetime of a temporary object created in a structured binding declaration (introduced by the initializer for a variable with unique name) is extended to the end of the structured binding declaration.

(since C++23):

- The lifetime of a temporary object created in the *range-initializer* of a range-`for` statement that would otherwise be destroyed at the end of the *range-initializer* is extended to the end of the loop body.

### Storage reuse

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, (since C++11) or due to program exit for static objects; otherwise the behavior is undefined.

```cpp
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, (since C++11) or automatic storage duration because such objects may be stored in read-only memory:

```cpp
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
}
```

When evaluating a new expression, storage is considered reused after it is returned from the allocation function, but before the evaluation of the *initializer* of the new expression:

```cpp
struct S
{
    int m;
};

void f()
{
    S x{1};
    new(&x) S(x.m); // undefined behavior: the storage is reused
}
```

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.

If all following conditions are satisfied, object `x` is *transparently replaceable* by object `y`:

- The storage for `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.
- Neither `x` nor `y` is a base class subobject, or a member subobject declared with `[[no_unique_address]]` (since C++20).
- One of the following conditions is satisfied:

  - `x` and `y` are both complete objects.
  - `x` and `y` are direct subobjects of objects `ox` and `oy` respectively, and `ox` is transparently replaceable by `oy`.

```cpp
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
```

(since C++17):

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 `std::launder`:

```cpp
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);
}
```

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:

- the lifetime of the containing object has begun and not ended
- the storage for the new object exactly overlays the storage of the original object
- the new object is of the same type as the original object (ignoring cv-qualification).

(since C++17):

Otherwise, the name of the original subobject cannot be used to access the new object without `std::launder`:

> Note: This section is incomplete Reason: an appropriate example

#### Providing storage

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

- the lifetime of the array has begun and not ended
- the storage for the new object fits entirely within the array
- there is no array object that satisfies these constraints nested within the array.

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).

```cpp
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
}
```

### Access outside of lifetime

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):

1. Lvalue to rvalue conversion (e.g. function call to a function that takes a value).
2. Access to a non-static data member or a call to a non-static member function.
3. Binding a reference to a virtual base class subobject.
4. `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:

1. `static_cast` of a pointer to storage without an object is only allowed when casting to (possibly cv-qualified) `void*`.
2. Pointers to storage without an object that were cast to possibly cv-qualified `void*` can only be `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.

### 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):

```cpp
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:

```cpp
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
}
```

### Defect reports

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 2489 | C++98 | `char[]` cannot provide storage, but objects could be implicitly created within its storage | objects cannot be implicitly created within the storage of `char[]` |
| 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 |
| CWG 2721 | C++98 | the exact time point of storage reuse was unclear for placement `new` | made clear |
| CWG 2849 | C++23 | function parameter objects were considered as temporary objects for range-`for` loop temporary object lifetime extension | not considered as temporary objects |
| CWG 2854 | C++98 | exception objects were temporary objects | they are not temporary objects |
| CWG 2867 | C++17 | the lifetime of temporary objects created in structured binding declarations were not extended | extended to the end of the declaration |
| 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 |

### References

- C++23 standard (ISO/IEC 14882:2024):

  - 6.7.3 Object lifetime [basic.life]

  - 11.9.5 Construction and destruction [class.cdtor]

- C++20 standard (ISO/IEC 14882:2020):

  - 6.7.3 Object lifetime [basic.life]

  - 11.10.4 Construction and destruction [class.cdtor]

- C++17 standard (ISO/IEC 14882:2017):

  - 6.8 Object lifetime [basic.life]

  - 15.7 Construction and destruction [class.cdtor]

- C++14 standard (ISO/IEC 14882:2014):

  - 3 Object lifetime [basic.life]

  - 12.7 Construction and destruction [class.cdtor]

- C++11 standard (ISO/IEC 14882:2011):

  - 3.8 Object lifetime [basic.life]

  - 12.7 Construction and destruction [class.cdtor]

- C++03 standard (ISO/IEC 14882:2003):

  - 3.8 Object lifetime [basic.life]

  - 12.7 Construction and destruction [class.cdtor]

- C++98 standard (ISO/IEC 14882:1998):

  - 3.8 Object lifetime [basic.life]

  - 12.7 Construction and destruction [class.cdtor]

### See also

- [C documentation for Lifetime](https://en.cppreference.com/w/c/language/lifetime)

## Podcast Host Debate & Socratic Standard Traps

Use the prompts and tricky ISO standard questions below to guide a Socratic audio discussion:

- Contrast raw storage duration with actual C++ object lifetime—and why malloc doesn't invoke constructors.
- Walk through why range-based for loops with temporary wrappers dangled for 12 years until C++23 P2718.
- Explain why strict aliasing breaks pointer-punning casts and how std::bit_cast solves it cleanly.

### Trap 1: [stmt.ranged] & P2718R0 (cppcon-stage)

Under C++17/C++20 rules, what happens in for (int x : make_holder().vec) if make_holder() returns a Holder by value?

```cpp
struct Holder { std::vector<int> vec{1, 2, 3}; };
Holder make_holder();
for (int x : make_holder().vec) { /* ... */ }
```

A. Safe in C++17 because the range-for __range reference extends Holder
B. Undefined behavior in C++17/C++20 because __range binds to .vec, leaving the Holder temporary to die before the loop body
C. Does not compile because vector cannot be copied
D. Extends Holder only when using auto&& x

**Standard Answer (B):** Undefined behavior in C++17/C++20 because __range binds to .vec, leaving the Holder temporary to die before the loop body

**Why:** In C++11–C++20, the range-for desugaring binds auto&& __range = make_holder().vec;. Because __range binds to the member subobject rather than the whole Holder temporary, the Holder temporary is destroyed at the semicolon of the __range declaration! C++23 (P2718R0) patched this rule specifically for range-for.

*CppCon speaker note:* One of the most famous real-world C++ bugs—even standard committee members tripped on it.

### Trap 2: [basic.life] & [ptr.launder] (standard-lawyer)

After destroying and placement-new constructing a struct containing a const int member in the same storage, how must you access the new object through the old pointer p?

```cpp
struct X { const int n; };
X* p = new X{1};
p->~X();
new (p) X{2};
```

A. p->n directly is always valid once placement new completes
B. std::launder(p)->n is required because X contains a const non-static data member
C. reinterpret_cast<X*>(p)->n avoids the requirement
D. Reusing storage containing a const member is impossible even with std::launder

**Standard Answer (B):** std::launder(p)->n is required because X contains a const non-static data member

**Why:** [basic.life]/8 states an old pointer automatically refers to the new object ONLY if the type has no const or reference non-static data members (among other rules). Otherwise, you must obtain the new pointer via placement new's return value or std::launder(p).

### Trap 3: [stmt.jump] & [class.dtor] (subtle)

In a single block { Guard a; Guard b; }, in what order are destructors invoked upon normal exit or an exception thrown after b is constructed?

A. a then b (construction order)
B. b then a (strict reverse order of construction)
C. Unspecified by the standard
D. Destructors are skipped if an exception unwinds

**Standard Answer (B):** b then a (strict reverse order of construction)

**Why:** Automatic local objects and class member subobjects are ALWAYS destroyed in exact reverse order of construction completion (LIFO), enabling dependent RAII guards like b holding a handle into a.

### Trap 4: [basic.lval] §7.2.1/11 & [bit.cast] (standard-lawyer)

Which method inspects the IEEE-754 bit pattern of a float as a uint32_t without violating C++ strict aliasing rules?

```cpp
float f = 3.14f;
```

A. uint32_t bits = *reinterpret_cast<uint32_t*>(&f);
B. union Pun { float f; uint32_t u; } p{f}; uint32_t bits = p.u;
C. auto bits = std::bit_cast<uint32_t>(f);
D. *(uint32_t*)(void*)&f

**Standard Answer (C):** auto bits = std::bit_cast<uint32_t>(f);

**Why:** Reading a float through a uint32_t* via reinterpret_cast violates strict aliasing ([basic.lval]), and reading an inactive union member is UB in C++ (unlike C99). C++20 std::bit_cast<uint32_t>(f) (or std::memcpy pre-C++20) is completely well-defined and constexpr.

---
Attribution: content from https://en.cppreference.com/w/cpp/language/lifetime (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.