---
name: "std::unique_ptr"
section: "3cxx"
platform: "linux"
package: "libstdc++-12-doc"
version: "12.4.0-5"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/std::unique_ptr/3cxx"
---

# std::unique\_ptr(3cxx)

A move-only smart pointer that manages unique ownership of a resource.

## NAME

std::unique\_ptr\< \_Tp, \_Dp \> - A move-only smart pointer that manages unique ownership of a resource.

## SYNOPSIS

`#include <memory>`

### Public Types

**using** **deleter\_type** = **\_Dp**\
 **using** **element\_type** = \_Tp\
 **using** **pointer** = **typename** **\_\_uniq\_ptr\_impl**\< \_Tp, **\_Dp** \>::pointer\

### Public Member Functions

template\<**typename** **\_Del**  = \_Dp, **typename**  = \_DeleterConstraint\<\_Del\>\> constexpr **unique\_ptr** () noexcept\
 Default constructor, creates a unique\_ptr that owns nothing.  template\<**typename** \_Up , **typename**  = \_Require\<	       is\_convertible\<\_Up\*, \_Tp\*\>, is\_same\<\_Dp, default\_delete\<\_Tp\>\>\>\> **unique\_ptr** (**auto\_ptr**\< \_Up \> &&**\_\_u**) noexcept\
 Converting constructor from `auto_ptr`.  **unique\_ptr** (const **unique\_ptr** &)=**delete**\
 template\<**typename** **\_Del**  = \_Dp, **typename**  = \_DeleterConstraint\<\_Del\>\> constexpr **unique\_ptr** (nullptr\_t) noexcept\
 Creates a unique\_ptr that owns nothing.  template\<**typename** **\_Del**  = \_Dp, **typename**  = \_DeleterConstraint\<\_Del\>\> constexpr **unique\_ptr** (pointer \_\_p) noexcept\
 template\<**typename** **\_Del**  = deleter\_type, **typename**  = \_Require\<is\_move\_constructible\<\_Del\>\>\> constexpr **unique\_ptr** (pointer \_\_p, **\_\_enable\_if\_t**\<!**is\_lvalue\_reference**\< **\_Del** \>::value, **\_Del** && \> \_\_d) noexcept\
 template\<**typename** **\_Del**  = deleter\_type, **typename**  = \_Require\<is\_copy\_constructible\<\_Del\>\>\> constexpr **unique\_ptr** (pointer \_\_p, const **deleter\_type** &\_\_d) noexcept\
 template\<**typename** **\_Del**  = deleter\_type, **typename** **\_DelUnref**  = typename remove\_reference\<\_Del\>::type\> constexpr **unique\_ptr** (pointer, **\_\_enable\_if\_t**\< **is\_lvalue\_reference**\< **\_Del** \>::value, **\_DelUnref** && \>)=**delete**\
 **unique\_ptr** (**unique\_ptr** &&)=**default**\
 Move constructor.  template\<**typename** \_Up , **typename** **\_Ep** , **typename**  = \_Require\<               \_\_safe\_conversion\_up\<\_Up, \_Ep\>,	       \_\_conditional\_t\<is\_reference\<\_Dp\>::value,			       is\_same\<\_Ep, \_Dp\>,			       is\_convertible\<\_Ep, \_Dp\>\>\>\> constexpr **unique\_ptr** (**unique\_ptr**\< \_Up, **\_Ep** \> &&**\_\_u**) noexcept\
 Converting constructor from another type.  **~unique\_ptr** () noexcept\
 Destructor, invokes the deleter if the stored pointer is not null.  constexpr pointer **get** () const noexcept\
 Return the stored pointer.  constexpr const **deleter\_type** & **get\_deleter** () const noexcept\
 Return a reference to the stored deleter.  constexpr **deleter\_type** & **get\_deleter** () noexcept\
 Return a reference to the stored deleter.  constexpr **operator bool** () const noexcept\
 Return `true` if the stored pointer is not null.  constexpr **add\_lvalue\_reference**\< element\_type \>::type **operator\*** () const noexcept(noexcept(\***std::declval**\< pointer \>()))\
 Dereference the stored pointer.  constexpr pointer **operator-\>** () const noexcept\
 Return the stored pointer.  **unique\_ptr** & **operator=** (const **unique\_ptr** &)=**delete**\
 constexpr **unique\_ptr** & **operator=** (nullptr\_t) noexcept\
 Reset the unique\_ptr to empty, invoking the deleter if necessary.  **unique\_ptr** & **operator=** (**unique\_ptr** &&)=**default**\
 Move assignment operator.  template\<**typename** \_Up , **typename** **\_Ep** \> constexpr **enable\_if**\< \_\_and\_\< **\_\_safe\_conversion\_up**\< \_Up, **\_Ep** \>, **is\_assignable**\< **deleter\_type** &, **\_Ep** && \> \>::value, **unique\_ptr** & \>::type **operator=** (**unique\_ptr**\< \_Up, **\_Ep** \> &&**\_\_u**) noexcept\
 Assignment from another type.  constexpr pointer **release** () noexcept\
 Release ownership of any stored pointer.  constexpr **void** **reset** (pointer \_\_p=pointer()) noexcept\
 Replace the stored pointer.  constexpr **void** **swap** (**unique\_ptr** &**\_\_u**) noexcept\
 Exchange the pointer and deleter with another object.

### Related Symbols

(Note that these are not member symbols.)  template\<**typename** \_Tp , typename... \_Args\> constexpr \_\_detail::\_\_unique\_ptr\_t\< \_Tp \> **make\_unique** (**\_Args** &&... **\_\_args**)\
 template\<**typename** \_Tp , typename... \_Args\> \_\_detail::\_\_invalid\_make\_unique\_t\< \_Tp \> **make\_unique** (**\_Args** &&...)=**delete**\
 template\<**typename** \_Tp \> constexpr \_\_detail::\_\_unique\_ptr\_array\_t\< \_Tp \> **make\_unique** (size\_t **\_\_num**)\
 template\<**typename** \_Tp \> constexpr \_\_detail::\_\_unique\_ptr\_t\< \_Tp \> **make\_unique\_for\_overwrite** ()\
 template\<**typename** \_Tp , typename... \_Args\> \_\_detail::\_\_invalid\_make\_unique\_t\< \_Tp \> **make\_unique\_for\_overwrite** (**\_Args** &&...)=**delete**\
 template\<**typename** \_Tp \> constexpr \_\_detail::\_\_unique\_ptr\_array\_t\< \_Tp \> **make\_unique\_for\_overwrite** (size\_t **\_\_num**)\
 template\<**typename** \_CharT , **typename** \_Traits , **typename** \_Tp , **typename** **\_Dp** \> **basic\_ostream**\< \_CharT, \_Traits \> & **operator\<\<** (**basic\_ostream**\< \_CharT, \_Traits \> &**\_\_os**, const **unique\_ptr**\< \_Tp, **\_Dp** \> &\_\_p)\
 Stream output operator for unique\_ptr.  template\<**typename** \_Tp , **typename** **\_Dp** \> constexpr **enable\_if**\< **\_\_is\_swappable**\< **\_Dp** \>::value \>::type **swap** (**unique\_ptr**\< \_Tp, **\_Dp** \> &\_\_x, **unique\_ptr**\< \_Tp, **\_Dp** \> &\_\_y) noexcept\

## Detailed Description

### template\<typename \_Tp, typename \_Dp = default\_delete\<\_Tp\>\>

class std::unique\_ptr\< \_Tp, \_Dp \>"A move-only smart pointer that manages unique ownership of a resource.

**Since**

C++11

## Constructor & Destructor Documentation

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> template\<typename \_Del = \_Dp, typename = \_DeleterConstraint\<\_Del\>\> constexpr std::unique\_ptr\< \_Tp, \_Dp \>::unique\_ptr ()` [inline]`, ` [constexpr]`, ` [noexcept]`

Default constructor, creates a unique\_ptr that owns nothing.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> template\<typename \_Del = \_Dp, typename = \_DeleterConstraint\<\_Del\>\> constexpr std::unique\_ptr\< \_Tp, \_Dp \>::unique\_ptr (pointer \_\_p)` [inline]`, ` [explicit]`, ` [constexpr]`, ` [noexcept]`

Takes ownership of a pointer.

**Parameters**

*\_\_p* A pointer to an object of `element_type`

The deleter will be value-initialized.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> template\<typename \_Del = deleter\_type, typename = \_Require\<is\_copy\_constructible\<\_Del\>\>\> constexpr std::unique\_ptr\< \_Tp, \_Dp \>::unique\_ptr (pointer \_\_p, const deleter\_type & \_\_d)` [inline]`, ` [constexpr]`, ` [noexcept]`

Takes ownership of a pointer.

**Parameters**

*\_\_p* A pointer to an object of `element_type` \
 *\_\_d* A reference to a deleter.

The deleter will be initialized with `__d`

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> template\<typename \_Del = deleter\_type, typename = \_Require\<is\_move\_constructible\<\_Del\>\>\> constexpr std::unique\_ptr\< \_Tp, \_Dp \>::unique\_ptr (pointer \_\_p, \_\_enable\_if\_t\<!is\_lvalue\_reference\< \_Del \>::value, \_Del && \> \_\_d)` [inline]`, ` [constexpr]`, ` [noexcept]`

Takes ownership of a pointer.

**Parameters**

*\_\_p* A pointer to an object of `element_type` \
 *\_\_d* An rvalue reference to a (non-reference) deleter.

The deleter will be initialized with `std::move(__d)`

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> template\<typename \_Del = \_Dp, typename = \_DeleterConstraint\<\_Del\>\> constexpr std::unique\_ptr\< \_Tp, \_Dp \>::unique\_ptr (nullptr\_t)` [inline]`, ` [constexpr]`, ` [noexcept]`

Creates a unique\_ptr that owns nothing.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> std::unique\_ptr\< \_Tp, \_Dp \>::unique\_ptr (unique\_ptr\< \_Tp, \_Dp \> &&)` [default]`

Move constructor.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> template\<typename \_Up , typename \_Ep , typename = \_Require\< \_\_safe\_conversion\_up\<\_Up, \_Ep\>, \_\_conditional\_t\<is\_reference\<\_Dp\>::value, is\_same\<\_Ep, \_Dp\>, is\_convertible\<\_Ep, \_Dp\>\>\>\> constexpr std::unique\_ptr\< \_Tp, \_Dp \>::unique\_ptr (unique\_ptr\< \_Up, \_Ep \> && \_\_u)` [inline]`, ` [constexpr]`, ` [noexcept]`

Converting constructor from another type. Requires that the pointer owned by `__u` is convertible to the type of pointer owned by this object, `__u` does not own an array, and `__u` has a compatible deleter type.

### template\<typename \_Tp , typename \_Dp \> template\<typename \_Up , typename \> std::unique\_ptr\< \_Tp, \_Dp \>::unique\_ptr (auto\_ptr\< \_Up \> && \_\_u)` [inline]`, ` [noexcept]`

Converting constructor from `auto_ptr`.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> std::unique\_ptr\< \_Tp, \_Dp \>::~unique\_ptr ()` [inline]`, ` [noexcept]`

Destructor, invokes the deleter if the stored pointer is not null.

References **std::unique\_ptr\< \_Tp, \_Dp \>::get\_deleter()**, and **std::move()**.

## Member Function Documentation

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> constexpr pointer std::unique\_ptr\< \_Tp, \_Dp \>::get () const` [inline]`, ` [constexpr]`, ` [noexcept]`

Return the stored pointer.

Referenced by **std::unique\_ptr\< \_Tp, \_Dp \>::operator bool()**, **std::unique\_ptr\< \_Tp\[\], \_Dp \>::operator bool()**, **std::unique\_ptr\< \_Tp, \_Dp \>::operator\*()**, **std::unique\_ptr\< \_Tp, \_Dp \>::operator-\>()**, and **std::unique\_ptr\< \_Tp\[\], \_Dp \>::operator\[\]()**.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> constexpr const deleter\_type & std::unique\_ptr\< \_Tp, \_Dp \>::get\_deleter () const` [inline]`, ` [constexpr]`, ` [noexcept]`

Return a reference to the stored deleter.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> constexpr deleter\_type & std::unique\_ptr\< \_Tp, \_Dp \>::get\_deleter ()` [inline]`, ` [constexpr]`, ` [noexcept]`

Return a reference to the stored deleter.

Referenced by **std::unique\_ptr\< \_Tp\[\], \_Dp \>::~unique\_ptr()**, **std::unique\_ptr\< \_Tp, \_Dp \>::~unique\_ptr()**, **std::unique\_ptr\< \_Tp, \_Dp \>::operator=()**, and **std::unique\_ptr\< \_Tp\[\], \_Dp \>::operator=()**.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> constexpr std::unique\_ptr\< \_Tp, \_Dp \>::operator bool () const` [inline]`, ` [explicit]`, ` [constexpr]`, ` [noexcept]`

Return `true` if the stored pointer is not null.

References **std::unique\_ptr\< \_Tp, \_Dp \>::get()**.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> constexpr add\_lvalue\_reference\< element\_type \>::type std::unique\_ptr\< \_Tp, \_Dp \>::operator\* () const` [inline]`, ` [constexpr]`, ` [noexcept]`

Dereference the stored pointer.

References **std::unique\_ptr\< \_Tp, \_Dp \>::get()**.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> constexpr pointer std::unique\_ptr\< \_Tp, \_Dp \>::operator-\> () const` [inline]`, ` [constexpr]`, ` [noexcept]`

Return the stored pointer.

References **std::unique\_ptr\< \_Tp, \_Dp \>::get()**.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> constexpr unique\_ptr & std::unique\_ptr\< \_Tp, \_Dp \>::operator= (nullptr\_t)` [inline]`, ` [constexpr]`, ` [noexcept]`

Reset the unique\_ptr to empty, invoking the deleter if necessary.

References **std::unique\_ptr\< \_Tp, \_Dp \>::reset()**.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> unique\_ptr & std::unique\_ptr\< \_Tp, \_Dp \>::operator= (unique\_ptr\< \_Tp, \_Dp \> &&)` [default]`

Move assignment operator. Invokes the deleter if this object owns a pointer.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> template\<typename \_Up , typename \_Ep \> constexpr enable\_if\< \_\_and\_\< \_\_safe\_conversion\_up\< \_Up, \_Ep \>, is\_assignable\< deleter\_type &, \_Ep && \> \>::value, unique\_ptr & \>::type std::unique\_ptr\< \_Tp, \_Dp \>::operator= (unique\_ptr\< \_Up, \_Ep \> && \_\_u)` [inline]`, ` [constexpr]`, ` [noexcept]`

Assignment from another type.

**Parameters**

*\_\_u* The object to transfer ownership from, which owns a convertible pointer to a non-array object.

Invokes the deleter if this object owns a pointer.

References **std::unique\_ptr\< \_Tp, \_Dp \>::get\_deleter()**, and **std::unique\_ptr\< \_Tp, \_Dp \>::reset()**.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> constexpr pointer std::unique\_ptr\< \_Tp, \_Dp \>::release ()` [inline]`, ` [constexpr]`, ` [noexcept]`

Release ownership of any stored pointer.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> constexpr void std::unique\_ptr\< \_Tp, \_Dp \>::reset (pointer \_\_p = `pointer()`)` [inline]`, ` [constexpr]`, ` [noexcept]`

Replace the stored pointer.

**Parameters**

*\_\_p* The new pointer to store.

The deleter will be invoked if a pointer is already owned.

References **std::move()**.

Referenced by **std::unique\_ptr\< \_Tp, \_Dp \>::operator=()**, **std::unique\_ptr\< \_Tp\[\], \_Dp \>::operator=()**, **std::unique\_ptr\< \_Tp, \_Dp \>::operator=()**, and **std::unique\_ptr\< \_Tp\[\], \_Dp \>::operator=()**.

### template\<typename \_Tp , typename \_Dp = default\_delete\<\_Tp\>\> constexpr void std::unique\_ptr\< \_Tp, \_Dp \>::swap (unique\_ptr\< \_Tp, \_Dp \> & \_\_u)` [inline]`, ` [constexpr]`, ` [noexcept]`

Exchange the pointer and deleter with another object.

## Author

Generated automatically by Doxygen for libstdc++ from the source code.
