Files
2025-10-25 03:02:53 +03:00

3.3 KiB

[unique.ptr.general]

20 Memory management library [mem]

20.3 Smart pointers [smartptr]

20.3.1 Unique-ownership pointers [unique.ptr]

20.3.1.1 General [unique.ptr.general]

1

#

A unique pointer is an object that owns another object and manages that other object through a pointer.

More precisely, a unique pointer is an object u that stores a pointer to a second object p and will dispose of p when u is itself destroyed (e.g., when leaving block scope ([stmt.dcl])).

In this context, u is said to own p.

2

#

The mechanism by which u disposes of p is known asp's associated deleter, a function object whose correct invocation results in p's appropriate disposition (typically its deletion).

3

#

Let the notation u.p denote the pointer stored by u, and let u.d denote the associated deleter.

Upon request, u canreset (replace) u.p and u.d with another pointer and deleter, but properly disposes of its owned object via the associated deleter before such replacement is considered completed.

4

#

Each object of a type U instantiated from the unique_ptr template specified in [unique.ptr] has the strict ownership semantics, specified above, of a unique pointer.

In partial satisfaction of these semantics, each such U is Cpp17MoveConstructible and Cpp17MoveAssignable, but is notCpp17CopyConstructible nor Cpp17CopyAssignable.

The template parameter T of unique_ptr may be an incomplete type.

5

#

[Note 1:

The uses of unique_ptr include providing exception safety for dynamically allocated memory, passing ownership of dynamically allocated memory to a function, and returning dynamically allocated memory from a function.

— end note]