STD::UNIQUE_PTR(3cxx)

πŸ’… std::unique_ptr< _Tp, _Dp > πŸ’…

okay bestie, this is the manual page for std::unique_ptr β€” manual section 3cxx, and yes it's giving main character energy because this is the smart pointer. Buckle up 🫢


✨ NAME ✨

std::unique_ptr< _Tp, _Dp > β€” a move-only smart pointer that manages unique ownership of a resource. that's it. that's the whole vibe. one owner, no sharing, no cap πŸ”₯


✨ SYNOPSIS ✨

#include <memory>

🌸 Public Types

  • using deleter_type = _Dp β€” the deleter's type, icon behavior
  • using element_type = _Tp β€” the thing it points at
  • using pointer = typename __uniq_ptr_impl< _Tp, _Dp >::pointer β€” the pointer type

πŸ¦‹ Public Member Functions

  • template<typename _Del = _Dp, typename = _DeleterConstraint<_Del>> constexpr unique_ptr () noexcept β€” default constructor, creates a unique_ptr that owns nothing. it's born unbothered πŸ’…
  • 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 β€” nope. no copying. not today. 🚫
  • 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 β€” straight up deleted, do not attempt 😭
  • unique_ptr (unique_ptr &&)=default β€” move constructor. this is how it travels ✈️
  • 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. it will clean up after itself 🧹
  • 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. (yes there's two of these, one const and one not, they're twins πŸ’œ)
  • 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 β€” copy assignment? deleted. obviously. πŸ™ˆ
  • 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. you're on your own now, buddy πŸ‘‹
  • 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.
heads up bestie: these are not member symbols. they just hang out in the same friend group.
  • 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. literally what it says on the tin, and it slays at exactly that πŸ’…

Since β€” C++11 ✨


🧚 Constructor & Destructor Documentation 🧚

okay this is the part everyone skips and then wonders why their code explodes. read it. i'm serious. 🫢

⭐ the default one

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. iconic empty-handed entrance πŸ’…

⭐ the "i'll take that pointer" one β€” unique_ptr (pointer __p)

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. yes bestie it is [explicit] β€” no sneaky implicit conversions, we don't do that here.

Parameters

  • __p β€” A pointer to an object of element_type

The deleter will be value-initialized. πŸŽ€

⭐ the "pointer + a copyable deleter" one β€” 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>>>
constexpr std::unique_ptr< _Tp, _Dp >::unique_ptr (pointer __p, const deleter_type & __d)
[inline], [constexpr], [noexcept]

Takes ownership of a pointer, and you hand it a deleter too. balanced, as all things should be βš–οΈ

Parameters

  • __p β€” A pointer to an object of element_type
  • __d β€” A reference to a deleter.

The deleter will be initialized with __d πŸ’–

⭐ the "pointer + a moved deleter" one β€” unique_ptr (pointer __p, _Del && __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. same energy as the last one, but this time the deleter gets moved in, not copied. fancy πŸ’…

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

⭐ the "nullptr_t" one

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. handing it a nullptr_t and it's like "cool, i own zero things, love that for me" πŸ¦‹

⭐ the move constructor

template<typename _Tp , typename _Dp  = default_delete<_Tp>>
std::unique_ptr< _Tp, _Dp >::unique_ptr (unique_ptr< _Tp, _Dp > &&) [default]

Move constructor. the only way a unique_ptr is allowed to be handed to someone else β€” by moving. there is no copy option. periodt. πŸ”₯

⭐ the converting constructor from another type

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. here are the receipts, bestie β€” it requires that:

  • the pointer owned by __u is convertible to the type of pointer owned by this object βœ…
  • __u does not own an array 🚫
  • __u has a compatible deleter type πŸ’œ

⭐ the auto_ptr converting constructor (a blast from the past πŸ’€)

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. yes, auto_ptr. it's giving vintage. we don't talk about it. πŸ™ˆ

⭐ the destructor

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. it does its own cleanup and it does it quietly β€” no exceptions, no drama, just vibes and a noexcept πŸ§ΉπŸ‘‘

References β€” std::unique_ptr< _Tp, _Dp >::get_deleter(), and std::move().


okay that's the constructors and destructors handled β€” part 2 has the rest of the story, don't go anywhere bestie πŸ’–

πŸ’– Member Function Documentation πŸ’–

okay bestie, this is the main character section β€” every member function gets its moment, and we are not skipping a single qualifier. πŸ’…


🌟 get() β€” peek at the stored pointer

template<typename _Tp , typename _Dp  = default_delete<_Tp>> constexpr
    pointer std::unique_ptr< _Tp, _Dp >::get () const [inline],
    [constexpr],  [noexcept]

Returns the stored pointer. That’s it, that’s the vibe β€” no cap. ✨

It’s referenced by a whole friend group:

  • std::unique_ptr< _Tp, _Dp >::make_unique_for_overwrite()
  • std::unique_ptr< _Tp, _Dp >::operator bool()
  • std::unique_ptr< _Tp, _Dp >::operator*()
  • std::unique_ptr< _Tp, _Dp >::operator->()
  • std::operator<()
  • std::operator<()
  • std::operator<()
  • std::operator==()
  • std::operator>()
  • std::operator>()

πŸŽ€ get_deleter() β€” const edition

template<typename _Tp , typename _Dp  = default_delete<_Tp>> constexpr const
    deleter_type & std::unique_ptr< _Tp, _Dp >::get_deleter () const
    [inline],  [constexpr],  [noexcept]

Returns a reference to the stored deleter. Lowkey iconic. πŸ’œ


πŸ¦‹ get_deleter() β€” non-const edition

template<typename _Tp , typename _Dp  = default_delete<_Tp>> constexpr
    deleter_type & std::unique_ptr< _Tp, _Dp >::get_deleter () [inline],
    [constexpr],  [noexcept]

Also returns a reference to the stored deleter β€” but this one’s the non-const version, so it’s giving main character energy. 🌈

Referenced by:

  • std::unique_ptr< _Tp, _Dp >::~unique_ptr()
  • std::unique_ptr< _Tp, _Dp >::operator=()

πŸ”₯ operator bool() β€” is there a pointer in the building?

template<typename _Tp , typename _Dp  = default_delete<_Tp>> constexpr
    std::unique_ptr< _Tp, _Dp >::operator bool () const [inline],
    [explicit],  [constexpr],  [noexcept]

Returns true if the stored pointer is not null. That’s the whole vibe check. βœ…

References:

  • std::unique_ptr< _Tp, _Dp >::get()

πŸ’« operator*() β€” dereference o’clock

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]

Dereferences the stored pointer. Respectfully, she ate. πŸ’…

References:

  • std::unique_ptr< _Tp, _Dp >::get()

🧚 operator->() β€” pointer access, but make it arrow

template<typename _Tp , typename _Dp  = default_delete<_Tp>> constexpr
    pointer std::unique_ptr< _Tp, _Dp >::operator-> () const [inline],
    [constexpr],  [noexcept]

Returns the stored pointer. Same pointer, different access path. Slay. ✨

References:

  • std::unique_ptr< _Tp, _Dp >::get()

πŸ“ operator=(nullptr_t) β€” reset to empty

template<typename _Tp , typename _Dp  = default_delete<_Tp>> constexpr
    unique_ptr & std::unique_ptr< _Tp, _Dp >::operator= (nullptr_t)
    [inline],  [constexpr],  [noexcept]

Resets the unique_ptr to empty, invoking the deleter if necessary. 🫢

References:

  • std::unique_ptr< _Tp, _Dp >::reset()

πŸ‘‘ operator=(unique_ptr&&) β€” move assignment

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. Periodt. πŸ’₯


🌸 operator=(unique_ptr<_Up, _Ep>&&) β€” assignment from another type

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. Very demure, very mindful. πŸŽ€

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()
  • std::unique_ptr< _Tp, _Dp >::reset()

πŸ’€ release() β€” letting go, dramatically

template<typename _Tp , typename _Dp  = default_delete<_Tp>> constexpr
    pointer std::unique_ptr< _Tp, _Dp >::release () [inline],  [constexpr],
    [noexcept]

Releases ownership of any stored pointer. Release her. πŸ’…


🌈 reset() β€” replace the stored pointer

template<typename _Tp , typename _Dp  = default_delete<_Tp>> constexpr void
    std::unique_ptr< _Tp, _Dp >::reset (pointer __p = pointer()) [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. Important bestie behavior. 🫢

References:

  • std::move()

Referenced by:

  • std::unique_ptr< _Tp, _Dp >::operator=()
  • std::unique_ptr< _Tp, _Dp >::operator=()

⭐ swap() β€” trade places, no drama

template<typename _Tp , typename _Dp  = default_delete<_Tp>> constexpr void
    std::unique_ptr< _Tp, _Dp >::swap (unique_ptr< _Tp, _Dp > & __u)
    [inline],  [constexpr],  [noexcept]

Exchanges the pointer and deleter with another object. Mutual swap, mutual slay. πŸ”βœ¨


πŸ‘‘ Author πŸ‘‘

Generated automatically by Doxygen for libstdc++ from the source code. So yes, the docs wrote themselves β€” automated icon behavior. πŸ€–πŸ’–


and that’s the whole page, bestie! πŸŽ€ this one comes from libstdc++ β€” it’s the std::unique_ptr< _Tp, _Dp >(3cxx) page, mwah πŸ’‹

πŸ“¦ where this page comes from