---
name: "std::unique_ptr"
section: "3"
platform: "linux"
package: "gcc"
version: "16.2.1+r23+gd564253eb6c8-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/std::unique_ptr"
---

# std::unique\_ptr(3)

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**\
 using **element\_type**\
 using **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\*, pointer\>,				   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.  constexpr **~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.

### Friends

template\<typename, typename, typename...\> class **inout\_ptr\_t**\
 template\<typename, typename, typename...\> class **out\_ptr\_t**\

### Related Symbols

(Note that these are not member symbols.)  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\
 Swap overload for unique\_ptr.

## 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\>\> 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\>\> 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\>\>\> 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\>\>\> 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\>\> 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\>\>\>\> 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\], \[constexpr\], \[noexcept\]

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

## Member Function Documentation

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

Return the stored pointer.

Referenced by **std::unique\_ptr\< \_State \>::operator bool()**, **std::unique\_ptr\< \_State \>::operator-\>()**, **std::operator\<()**, **std::operator\<()**, **std::operator\<()**, **std::unique\_ptr\< \_State \>::operator\<\<()**, **std::operator==()**, **std::operator\>()**, and **std::operator\>()**.

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

Return a reference to the stored deleter.

Referenced by **std::unique\_ptr\< \_State \>::~unique\_ptr()**, and **std::unique\_ptr\< \_State \>::operator=()**.

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

Return true if the stored pointer is not null.

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

Dereference the stored pointer.

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

Return the stored pointer.

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

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

### 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\> 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.

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

Release ownership of any stored pointer.

### template\<typename \_Tp, typename \_Dp = default\_delete\<\_Tp\>\> 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.

Referenced by **std::unique\_ptr\< \_State \>::operator=()**, and **std::unique\_ptr\< \_State \>::operator=()**.

### template\<typename \_Tp, typename \_Dp = default\_delete\<\_Tp\>\> 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.
