STD::UNIQUE_PTR(3)
π std::uniqu... _Tp, _Dp >(3) β Library Functions Manual π
okay so bestie, welcome to the Library Functions Manual page for std::unique_ptr< _Tp, _Dp >. no cap, this is a move-only smart pointer that manages unique ownership of a resource. it's giving main character energy and we're obsessed. π
β¨
β¨ NAME β¨
std::unique_ptr< _Tp, _Dp > β a move-only smart pointer that manages unique ownership of a resource. periodt. π
πΈ SYNOPSIS πΈ
#include <memory>
π«Ά Public Types
using deleter_typeusing element_typeusing pointer
π Public Member Functions
- Default constructor β owns nothing.
template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
constexpr unique_ptr () noexcept
Default constructor, creates a unique_ptr that owns nothing.
- Converting constructor from
auto_ptr.
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.
- Copy constructor (deleted).
unique_ptr (const unique_ptr &)=delete
nullptr_tconstructor.
template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
constexpr unique_ptr (nullptr_t) noexcept
Creates a unique_ptr that owns nothing.
- Pointer constructor.
template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>>
constexpr unique_ptr (pointer __p) noexcept
- Pointer + rvalue deleter constructor.
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
- Pointer + const deleter constructor.
template<typename _Del = deleter_type, typename =
_Require<is_copy_constructible<_Del>>> constexpr unique_ptr (pointer
__p, const deleter_type &__d) noexcept
- Pointer + lvalue-ref deleter constructor (deleted).
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
- Move constructor.
unique_ptr (unique_ptr &&)=default
Move constructor.
- Converting constructor from another type.
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.
- Destructor.
constexpr ~unique_ptr () noexcept
Destructor, invokes the deleter if the stored pointer is not null.
get.
constexpr pointer get () const noexcept
Return the stored pointer.
get_deleter(const).
constexpr const deleter_type & get_deleter () const noexcept
Return a reference to the stored deleter.
get_deleter(non-const).
constexpr deleter_type & get_deleter () noexcept
Return a reference to the stored deleter.
operator bool.
constexpr operator bool () const noexcept
Return true if the stored pointer is not null.
operator*.
constexpr add_lvalue_reference< element_type >::type operator* () const
noexcept(noexcept(*std::declval< pointer >()))
Dereference the stored pointer.
operator->.
constexpr pointer operator-> () const noexcept
Return the stored pointer.
- Copy assignment (deleted).
unique_ptr & operator= (const unique_ptr &)=delete
nullptr_tassignment.
constexpr unique_ptr & operator= (nullptr_t) noexcept
Reset the unique_ptr to empty, invoking the deleter if necessary.
- Move assignment.
unique_ptr & operator= (unique_ptr &&)=default
Move assignment operator.
- Converting assignment.
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.
release.
constexpr pointer release () noexcept
Release ownership of any stored pointer.
reset.
constexpr void reset (pointer __p=pointer()) noexcept
Replace the stored pointer.
swap.
constexpr void swap (unique_ptr &__u) noexcept
Exchange the pointer and deleter with another object.
π― Friends
template<typename, typename, typename...> class inout_ptr_ttemplate<typename, typename, typename...> class out_ptr_t
π Related Symbols
(Note that these are not member symbols.)
- Stream output operator for
unique_ptr.
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.
- Swap overload 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 π οΈ
πΌ unique_ptr ()
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. it's giving empty hands, full heart π
πΌ unique_ptr (pointer __p)
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 ofelement_type
The deleter will be value-initialized.
πΌ unique_ptr (pointer __p, const deleter_type & __d)
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 ofelement_type__dβ A reference to a deleter.
The deleter will be initialized with __d
πΌ unique_ptr (pointer __p, __enable_if_t<!is_lvalue_reference< _Del >::value, _Del && > __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 ofelement_type__dβ An rvalue reference to a (non-reference) deleter.
The deleter will be initialized with std::move(__d)
πΌ unique_ptr (nullptr_t )
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.
πΌ unique_ptr (unique_ptr< _Tp, _Dp > && )
template<typename _Tp, typename _Dp = default_delete<_Tp>> std::unique_ptr<
_Tp, _Dp >::unique_ptr (unique_ptr< _Tp, _Dp > && ) [default]
Move constructor.
πΌ unique_ptr (unique_ptr< _Up, _Ep > && __u)
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.
πΌ unique_ptr (auto_ptr< _Up > && __u)
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.
πΌ ~unique_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 β¨
okay bestie, this is the good part β all the actual things you do with your unique_ptr. buckle up, there's like twelve of them and every single one matters π
π«Ά get()
template<typename _Tp, typename _Dp = default_delete<_Tp>>
pointer std::unique_ptr< _Tp, _Dp >::get () const
tags on this one: [inline], [constexpr], [noexcept]
It just hands you back the stored pointer. that's it. that's the whole vibe. the unique_ptr still owns it, you're just looking at it π
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>().
π get_deleter() (const version)
template<typename _Tp, typename _Dp = default_delete<_Tp>>
const deleter_type & std::unique_ptr< _Tp, _Dp >::get_deleter () const
tags: [inline], [constexpr], [noexcept]
Returns a reference to the stored deleter. yes, the little guy who's gonna do the cleanup β you get to peek at him π
πΈ get_deleter() (non-const version)
template<typename _Tp, typename _Dp = default_delete<_Tp>>
deleter_type & std::unique_ptr< _Tp, _Dp >::get_deleter ()
tags: [inline], [constexpr], [noexcept]
Same deal β a reference to the stored deleter, except this time it's not const so you can actually poke at him, bestie. main character energy, the deleter gets to change π₯
Referenced by std::unique_ptr< _State >::~unique_ptr(), and std::unique_ptr< _State >::operator=().
π operator bool()
template<typename _Tp, typename _Dp = default_delete<_Tp>>
std::unique_ptr< _Tp, _Dp >::operator bool () const
tags: [inline], [explicit], [constexpr], [noexcept]
Returns true if the stored pointer is not null. so like, if (my_ptr) = "do I have something? slay." and if (!my_ptr) = "empty, nothing here, we're sobbing" π
π¦ operator*()
template<typename _Tp, typename _Dp = default_delete<_Tp>>
add_lvalue_reference< element_type >::type std::unique_ptr< _Tp, _Dp >::operator* () const
tags: [inline], [constexpr], [noexcept]
Dereferences the stored pointer. that's your *ptr, the "gimme the actual object" move β
π operator->()
template<typename _Tp, typename _Dp = default_delete<_Tp>>
pointer std::unique_ptr< _Tp, _Dp >::operator-> () const
tags: [inline], [constexpr], [noexcept]
Returns the stored pointer β so ptr->member just works and you never have to think about it. iconic behaviour tbh β¨
π operator=(nullptr_t)
template<typename _Tp, typename _Dp = default_delete<_Tp>>
unique_ptr & std::unique_ptr< _Tp, _Dp >::operator= (nullptr_t )
tags: [inline], [constexpr], [noexcept]
Resets the unique_ptr to empty, and invokes the deleter if necessary. so it's not just vibes β if you owned something, it gets cleaned up fr fr π§Ή
π operator=(unique_ptr< _Tp, _Dp > && )
template<typename _Tp, typename _Dp = default_delete<_Tp>>
unique_ptr & std::unique_ptr< _Tp, _Dp >::operator= (unique_ptr< _Tp, _Dp > && )
tag: [default]
The move assignment operator. Invokes the deleter if this object owns a pointer. so you hand your pointer over, and anything you were holding onto before gets yeeted responsibly π
π operator=(unique_ptr< _Up, _Ep > && __u)
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)
tags: [inline], [constexpr], [noexcept]
Assignment from another type. the glow-up conversion moment where one kind of unique_ptr becomes another π¦
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. (same rule as always, bestie β clean up before you take the new thing)
π§ release()
template<typename _Tp, typename _Dp = default_delete<_Tp>>
pointer std::unique_ptr< _Tp, _Dp >::release ()
tags: [inline], [constexpr], [noexcept]
Releases ownership of any stored pointer. you get the raw pointer back and the unique_ptr stops caring about it. you are responsible now, no cap π
π reset()
template<typename _Tp, typename _Dp = default_delete<_Tp>>
void std::unique_ptr< _Tp, _Dp >::reset (pointer __p = pointer())
tags: [inline], [constexpr], [noexcept]
Replaces 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=().
π«Ά swap()
template<typename _Tp, typename _Dp = default_delete<_Tp>>
void std::unique_ptr< _Tp, _Dp >::swap (unique_ptr< _Tp, _Dp > & __u)
tags: [inline], [constexpr], [noexcept]
Exchanges the pointer and the deleter with another object. full body swap, both of them, no take-backs π
π AUTHOR
Generated automatically by Doxygen for libstdc++ from the source code. not a human, not a girl, just a robot doing its little documentation dance π€β¨
okay bestie that's the whole page!! source + version: libstdc++, and this is the std::uniqu... _Tp, _Dp >(3) man page (the date stamp on the footer is whatever libstdc++ put there when it generated it β i'm not making one up, i would never lie to you π). go forth and own your pointers responsibly. periodt. π
ππ¦
π¦ where this page comes from
- arch rolling: gcc 16.2.1+r23+gd564253eb6c8-1 Β· GPL-3.0-or-later WITH GCC-exception-3.1 AND GFDL-1.3-or-later
- fedora 44: libstdc++-docs 16.2.1-2.fc44
- fedora 44: libstdc++-docs 16.2.1-3.fc44 Β· GPL-3.0-or-later AND LGPL-3.0-or-later AND (GPL-3.0-or-later WITH GCC-exception-3.1) AND (GPL-3.0-or-later WITH Texinfo-exception) AND (LGPL-2.1-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GCC-exception-2.0) AND (GPL-2.0-or-later WITH GNU-compiler-exception) AND BSL-1.0 AND GFDL-1.3-or-later AND Linux-man-pages-copyleft-2-para AND SunPro AND BSD-1-Clause AND BSD-2-Clause AND BSD-2-Clause-Views AND BSD-3-Clause AND BSD-4-Clause AND BSD-Source-Code AND Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 WITH LLVM-Exception) AND ZPL-2.1 AND ISC AND LicenseRef-Fedora-Public-Domain AND HP-1986 AND curl AND Martin-Birgmeier AND HPND-Markus-Kuhn AND dtoa AND SMLNJ AND AMD-newlib AND OAR AND HPND-merchantability-variant AND HPND-Intel
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms