STD::SHARED_PTR(3cxx)
πΈ NAME πΈ
okay so this is the std::shared_ptr< _Tp >(3cxx) page, bestie π and it's literally about std::shared_ptr< _Tp > β a smart pointer with reference-counted copy semantics. iconic behavior, no cap β¨
β¨ SYNOPSIS β¨
#include <memory>
Inherits std::__shared_ptr< _Tp, _Lp >. π«Ά
π Public Types π
using element_type = typename __shared_ptr< _Tp >::element_typeβ the type pointed to by the stored pointer,remove_extent_t<_Tp>πusing weak_type = weak_ptr< _Tp >β the correspondingweak_ptrtype for thisshared_ptr, periodt β¨
π Public Member Functions π
constexpr shared_ptr () noexceptβ construct an emptyshared_ptrπ«§template<typename _Yp , typename = _Constructible<_Yp*>> shared_ptr (_Yp *__p)β construct ashared_ptrthat owns the pointer__pπ₯template<typename _Yp , typename _Deleter , typename = _Constructible<_Yp*, _Deleter>> shared_ptr (_Yp *__p, _Deleter __d)β construct ashared_ptrthat owns the pointer__pand the deleter__dπtemplate<typename _Yp , typename _Deleter , typename _Alloc , typename = _Constructible<_Yp*, _Deleter, _Alloc>> shared_ptr (_Yp *__p, _Deleter __d, _Alloc __a)β construct ashared_ptrthat owns the pointer__pand the deleter__d(allocator__ais right there in the signature, iykyk) πtemplate<typename _Yp , typename = _Constructible<auto_ptr<_Yp>>> shared_ptr (auto_ptr< _Yp > &&__r)β this overload is literally listed with no extra description, bestie πshared_ptr (const shared_ptr &) noexcept=defaultβ copy constructor πtemplate<typename _Yp , typename = _Constructible<const shared_ptr<_Yp>&>> shared_ptr (const shared_ptr< _Yp > &__r) noexceptβ if__ris empty, constructs an emptyshared_ptr; otherwise constructs ashared_ptrthat shares ownership with__rβ¨template<typename _Yp > shared_ptr (const shared_ptr< _Yp > &__r, element_type *__p) noexceptβ constructs ashared_ptrinstance that stores__pand shares ownership with__rπtemplate<typename _Yp , typename = _Constructible<const weak_ptr<_Yp>&>> shared_ptr (const weak_ptr< _Yp > &__r)β constructs ashared_ptrthat shares ownership with__rand stores a copy of the pointer stored in__rπ¦template<typename _Deleter > shared_ptr (nullptr_t __p, _Deleter __d)β construct ashared_ptrthat owns a null pointer and the deleter__dπtemplate<typename _Deleter , typename _Alloc > shared_ptr (nullptr_t __p, _Deleter __d, _Alloc __a)β construct ashared_ptrthat owns a null pointer and the deleter__dπ«constexpr shared_ptr (nullptr_t) noexceptβ construct an emptyshared_ptrπ«Άshared_ptr (shared_ptr &&__r) noexceptβ move-constructs ashared_ptrinstance from__rπ₯template<typename _Yp , typename = _Constructible<shared_ptr<_Yp>>> shared_ptr (shared_ptr< _Yp > &&__r) noexceptβ move-constructs ashared_ptrinstance from__rπtemplate<typename _Yp > shared_ptr (shared_ptr< _Yp > &&__r, element_type *__p) noexceptβ constructs ashared_ptrinstance that stores__pand shares ownership with__rπΈtemplate<typename _Tp1 , typename > shared_ptr (std::auto_ptr< _Tp1 > &&__r)β this overload is listed with no extra description, periodt πtemplate<typename _Yp , typename _Del , typename = _Constructible<unique_ptr<_Yp, _Del>>> shared_ptr (unique_ptr< _Yp, _Del > &&__r)β this overload is also listed with no extra description, no cap πelement_type * get () const noexceptβ return the stored pointer πoperator bool () const noexceptβ returntrueif the stored pointer is not null βelement_type & operator* () const noexceptβ dereference vibes, literally listed like this πelement_type * operator-> () const noexceptβ arrow access vibes, also literally listed like this β¨template<typename _Yp > _Assignable< auto_ptr< _Yp > > operator= (auto_ptr< _Yp > &&__r)β assignment fromauto_ptrrvalue πshared_ptr & operator= (const shared_ptr &) noexcept=defaultβ copy assignment, defaulted π«Άtemplate<typename _Yp > _Assignable< const shared_ptr< _Yp > & > operator= (const shared_ptr< _Yp > &__r) noexceptβ assignment fromconst shared_ptr<_Yp>&π¦shared_ptr & operator= (shared_ptr &&__r) noexceptβ move assignment πtemplate<class _Yp > _Assignable< shared_ptr< _Yp > > operator= (shared_ptr< _Yp > &&__r) noexceptβ assignment fromshared_ptr<_Yp>&&π₯template<typename _Yp , typename _Del > _Assignable< unique_ptr< _Yp, _Del > > operator= (unique_ptr< _Yp, _Del > &&__r)β assignment fromunique_ptr<_Yp, _Del>&&πvoid reset () noexceptβ reset, no extra prose on this one πtemplate<typename _Yp > _SafeConv< _Yp > reset (_Yp *__p)β reset with__pπΈtemplate<typename _Yp , typename _Deleter > _SafeConv< _Yp > reset (_Yp *__p, _Deleter __d)β reset with__pand deleter__dπtemplate<typename _Yp , typename _Deleter , typename _Alloc > _SafeConv< _Yp > reset (_Yp *__p, _Deleter __d, _Alloc __a)β reset with__p, deleter__d, and allocator__aπvoid swap (__shared_ptr< _Tp, _Lp > &__other) noexceptβ exchange both the owned pointer and the stored pointer πbool unique () const noexceptβ returntrueifuse_count() == 1βοΈlong use_count () const noexceptβ if*thisowns a pointer, return the number of owners, otherwise zero π’template<typename _Tp1 > bool owner_before (__shared_ptr< _Tp1, _Lp > const &__rhs) const noexceptβ define an ordering based on ownership π«Άtemplate<typename _Tp1 > bool owner_before (__weak_ptr< _Tp1, _Lp > const &__rhs) const noexceptβ define an ordering based on ownership π«Ά
π Friends π
template<typename _Yp , typename _Alloc > shared_ptr< _BoundedArray< _Yp > > allocate_shared (const _Alloc &)β friendallocate_sharedπtemplate<typename _Yp , typename _Alloc , typename... _Args> shared_ptr< _NonArray< _Yp > > allocate_shared (const _Alloc &, _Args &&...)β friendallocate_sharedβ¨template<typename _Yp , typename _Alloc > shared_ptr< _BoundedArray< _Yp > > allocate_shared (const _Alloc &, const remove_extent_t< _Yp > &)β friendallocate_sharedπtemplate<typename _Yp , typename _Alloc > shared_ptr< _UnboundedArray< _Yp > > allocate_shared (const _Alloc &, size_t)β friendallocate_sharedπ¦template<typename _Yp , typename _Alloc > shared_ptr< _UnboundedArray< _Yp > > allocate_shared (const _Alloc &, size_t, const remove_extent_t< _Yp > &)β friendallocate_sharedπtemplate<typename _Yp , typename _Alloc > shared_ptr< _NotUnboundedArray< _Yp > > allocate_shared_for_overwrite (const _Alloc &)β friendallocate_shared_for_overwriteπ₯template<typename _Yp , typename _Alloc > shared_ptr< _UnboundedArray< _Yp > > allocate_shared_for_overwrite (const _Alloc &, size_t)β friendallocate_shared_for_overwriteπtemplate<typename _Yp > shared_ptr< _BoundedArray< _Yp > > make_shared ()β friendmake_sharedπΈtemplate<typename _Yp , typename... _Args> shared_ptr< _NonArray< _Yp > > make_shared (_Args &&...)β friendmake_sharedπtemplate<typename _Yp > shared_ptr< _BoundedArray< _Yp > > make_shared (const remove_extent_t< _Yp > &)β friendmake_sharedπtemplate<typename _Yp > shared_ptr< _UnboundedArray< _Yp > > make_shared (size_t)β friendmake_sharedβ¨template<typename _Yp > shared_ptr< _UnboundedArray< _Yp > > make_shared (size_t, const remove_extent_t< _Yp > &)β friendmake_sharedπtemplate<typename _Yp > shared_ptr< _NotUnboundedArray< _Yp > > make_shared_for_overwrite ()β friendmake_shared_for_overwriteπ«Άtemplate<typename _Yp > shared_ptr< _UnboundedArray< _Yp > > make_shared_for_overwrite (size_t)β friendmake_shared_for_overwriteπclass weak_ptr< _Tp >β friend classweak_ptr< _Tp >π
β Related Symbols β
note: these are not member symbols, bestie β they just hang out in the same orbit π«
template<typename _Del , typename _Tp > _Del * get_deleter (const shared_ptr< _Tp > &__p) noexceptβ20.7.2.2.10 shared_ptr get_deleterπtemplate<typename _Ch , typename _Tr , typename _Tp , _Lock_policy _Lp> std::basic_ostream< _Ch, _Tr > & operator<< (std::basic_ostream< _Ch, _Tr > &__os, const __shared_ptr< _Tp, _Lp > &__p)β write the stored pointer to anostreamπ¨οΈtemplate<typename _Tp , typename _Up > bool operator== (const shared_ptr< _Tp > &__a, const shared_ptr< _Up > &__b) noexceptβ equality comparison πtemplate<typename _Tp > bool operator== (const shared_ptr< _Tp > &__a, nullptr_t) noexceptβshared_ptrcomparison withnullptrπ«Άtemplate<typename _Tp > void swap (shared_ptr< _Tp > &__a, shared_ptr< _Tp > &__b) noexceptβ swap overload forshared_ptrπtemplate<typename _Tp , typename _Up > shared_ptr< _Tp > static_pointer_cast (const shared_ptr< _Up > &__r) noexceptβ convert type ofshared_ptr, viastatic_castβ¨template<typename _Tp , typename _Up > shared_ptr< _Tp > const_pointer_cast (const shared_ptr< _Up > &__r) noexceptβ convert type ofshared_ptr, viaconst_castπtemplate<typename _Tp , typename _Up > shared_ptr< _Tp > dynamic_pointer_cast (const shared_ptr< _Up > &__r) noexceptβ convert type ofshared_ptr, viadynamic_castπ¦template<typename _Tp , typename _Up > shared_ptr< _Tp > reinterpret_pointer_cast (const shared_ptr< _Up > &__r) noexceptβ convert type ofshared_ptr, viareinterpret_castπ₯template<typename _Tp , typename _Up > shared_ptr< _Tp > static_pointer_cast (shared_ptr< _Up > &&__r) noexceptβ convert type ofshared_ptrrvalue, viastatic_castπΈtemplate<typename _Tp , typename _Up > shared_ptr< _Tp > const_pointer_cast (shared_ptr< _Up > &&__r) noexceptβ convert type ofshared_ptrrvalue, viaconst_castπtemplate<typename _Tp , typename _Up > shared_ptr< _Tp > dynamic_pointer_cast (shared_ptr< _Up > &&__r) noexceptβ convert type ofshared_ptrrvalue, viadynamic_castπtemplate<typename _Tp , typename _Up > shared_ptr< _Tp > reinterpret_pointer_cast (shared_ptr< _Up > &&__r) noexceptβ convert type ofshared_ptrrvalue, viareinterpret_castπtemplate<typename _Tp , typename _Alloc , typename... _Args> shared_ptr< _NonArray< _Tp > > allocate_shared (const _Alloc &__a, _Args &&... __args)β create an object that is owned by ashared_ptrπ«Άtemplate<typename _Tp , typename... _Args> shared_ptr< _NonArray< _Tp > > make_shared (_Args &&... __args)β create an object that is owned by ashared_ptrπtemplate<typename _Tp , typename _Alloc > shared_ptr< _UnboundedArray< _Tp > > allocate_shared (const _Alloc &__a, size_t __n)β create an object that is owned by ashared_ptrπtemplate<typename _Tp , typename _Alloc > shared_ptr< _BoundedArray< _Tp > > allocate_shared (const _Alloc &__a)β create an object that is owned by ashared_ptrπ
π Detailed Description π
template<typename _Tp>
class std::shared_ptr< _Tp >
A smart pointer with reference-counted copy semantics. π«Ά
Since: C++11 β¨
A shared_ptr object is either empty or owns a pointer passed to the constructor. Copies of a shared_ptr share ownership of the same pointer. When the last shared_ptr that owns the pointer is destroyed or reset, the owned pointer is freed β either by delete or by invoking a custom deleter that was passed to the constructor. ππ₯
A shared_ptr also stores another pointer, which is usually (but not always) the same pointer as it owns. The stored pointer can be retrieved by calling the get() member function. π
The equality and relational operators for shared_ptr only compare the stored pointer returned by get(), not the owned pointer. To test whether two shared_ptr objects share ownership of the same pointer, see std::shared_ptr::owner_before and std::owner_less. ππ
part 2 has the rest, bestie β don't touch that dial π
π Member Typedef Documentation π
Ok bestie, these are the little type aliases living inside shared_ptr β they're giving "shortcut names so you don't have to type the scary template soup" β¨
element_typeβtemplate<typename _Tp > using std::shared_ptr< _Tp >::element_type = typename __shared_ptr<_Tp>::element_typeIt's the type pointed to by the stored pointer, i.e.remove_extent_t<_Tp>. Literally the "what am I actually pointing at" type π―weak_typeβtemplate<typename _Tp > using std::shared_ptr< _Tp >::weak_type = weak_ptr<_Tp>The correspondingweak_ptrtype for thisshared_ptr. So convenient, so cute.- Since β C++17 π¦
π Constructor & Destructor Documentation π
This is the main character section fr fr β every single way to build a shared_ptr. Buckle up, there are a LOT of them and every one matters π
1. The empty one π«§
template<typename _Tp > constexpr std::shared_ptr< _Tp >::shared_ptr ()
[inline], [constexpr], [noexcept]
Construct an empty shared_ptr. Nothing owned, nothing stored, just vibes.
- Postcondition β
use_count()==0 && get()==0
2. The copy constructor π
template<typename _Tp > std::shared_ptr< _Tp >::shared_ptr (const
shared_ptr< _Tp > &) [default], [noexcept]
Copy constructor. Yes it's [default]ed and [noexcept], no drama. π
3. The "take ownership of this raw pointer" one π₯
template<typename _Tp > template<typename _Yp , typename =
_Constructible<_Yp*>> std::shared_ptr< _Tp >::shared_ptr (_Yp * __p)
[inline], [explicit]
Construct a shared_ptr that owns the pointer __p.
- Parameters
__pβ A pointer that is convertible toelement_type*.
- Postcondition β
use_count() == 1 && get() == __p - Exceptions β
std::bad_alloc, in which casedelete __pis called. π (yep, it cleans up for you, iconic)
4. Pointer + custom deleter π§
template<typename _Tp > template<typename _Yp , typename _Deleter , typename
= _Constructible<_Yp*, _Deleter>> std::shared_ptr< _Tp >::shared_ptr
(_Yp * __p, _Deleter __d) [inline]
Construct a shared_ptr that owns the pointer __p and the deleter __d.
- Parameters
__pβ A pointer.__dβ A deleter.
- Postcondition β
use_count() == 1 && get() == __p - Exceptions β
std::bad_alloc, in which case__d(__p)is called. - Requirements:
_Deleter's copy constructor and destructor must not throw β οΈ
__shared_ptr will release __p by calling __d(__p)
5. Null pointer + custom deleter π³οΈ
template<typename _Tp > template<typename _Deleter > std::shared_ptr< _Tp
>::shared_ptr (nullptr_t __p, _Deleter __d) [inline]
Construct a shared_ptr that owns a null pointer and the deleter __d.
- Parameters
__pβ A null pointer constant.__dβ A deleter.
- Postcondition β
use_count() == 1 && get() == __p - Exceptions β
std::bad_alloc, in which case__d(__p)is called. - Requirements:
_Deleter's copy constructor and destructor must not throw β οΈ
The last owner will call __d(__p)
6. Pointer + deleter + allocator π
template<typename _Tp > template<typename _Yp , typename _Deleter , typename
_Alloc , typename = _Constructible<_Yp*, _Deleter, _Alloc>>
std::shared_ptr< _Tp >::shared_ptr (_Yp * __p, _Deleter __d, _Alloc __a)
[inline]
Construct a shared_ptr that owns the pointer __p and the deleter __d.
- Parameters
__pβ A pointer.__dβ A deleter.__aβ An allocator.
- Postcondition β
use_count() == 1 && get() == __p - Exceptions β
std::bad_alloc, in which case__d(__p)is called. - Requirements:
_Deleter's copy constructor and destructor must not throw._Alloc's copy constructor and destructor must not throw. β οΈ
__shared_ptr will release __p by calling __d(__p)
7. Null pointer + deleter + allocator π«Ά
template<typename _Tp > template<typename _Deleter , typename _Alloc >
std::shared_ptr< _Tp >::shared_ptr (nullptr_t __p, _Deleter __d, _Alloc
__a) [inline]
Construct a shared_ptr that owns a null pointer and the deleter __d.
- Parameters
__pβ A null pointer constant.__dβ A deleter.__aβ An allocator.
- Postcondition β
use_count() == 1 && get() == __p - Exceptions β
std::bad_alloc, in which case__d(__p)is called. - Requirements:
_Deleter's copy constructor and destructor must not throw._Alloc's copy constructor and destructor must not throw. β οΈ
The last owner will call __d(__p)
8. The aliasing constructor (lvalue edition) πͺ
template<typename _Tp > template<typename _Yp > std::shared_ptr< _Tp
>::shared_ptr (const shared_ptr< _Yp > & __r, element_type * __p)
[inline], [noexcept]
Constructs a shared_ptr instance that stores __p and shares ownership with __r. So you get a pointer to a piece, but the ownership count is shared with the whole β lowkey genius.
- Parameters
__rβ Ashared_ptr.__pβ A pointer that will remain valid while*__ris valid.
- Postcondition β
get() == __p && use_count() == __r.use_count()
This can be used to construct a shared_ptr to a sub-object of an object managed by an existing shared_ptr. The complete object will remain valid while any shared_ptr owns it, even if they don't store a pointer to the complete object. π
shared_ptr<pair<int,int>> pii(new pair<int,int>());
shared_ptr<int> pi(pii, &pii->first);
assert(pii.use_count() == 2);
9. The aliasing constructor (rvalue edition) π
template<typename _Tp > template<typename _Yp > std::shared_ptr< _Tp
>::shared_ptr (shared_ptr< _Yp > && __r, element_type * __p) [inline],
[noexcept]
Constructs a shared_ptr instance that stores __p and shares ownership with __r.
- Parameters
__rβ Ashared_ptr.__pβ A pointer that will remain valid while*__ris valid.
- Postcondition β
get() == __p && !__r.use_count() && !__r.get() - Since β C++17 π¦
This can be used to construct a shared_ptr to a sub-object of an object managed by an existing shared_ptr. The complete object will remain valid while any shared_ptr owns it, even if they don't store a pointer to the complete object.
shared_ptr<pair<int,int>> pii(new pair<int,int>());
shared_ptr<int> pi1(pii, &pii->first);
assert(pii.use_count() == 2);
shared_ptr<int> pi2(std::move(pii), &pii->second);
assert(pii.use_count() == 0);
10. Converting copy constructor π«
template<typename _Tp > template<typename _Yp , typename =
_Constructible<const shared_ptr<_Yp>&>> std::shared_ptr< _Tp
>::shared_ptr (const shared_ptr< _Yp > & __r) [inline], [noexcept]
If __r is empty, constructs an empty shared_ptr; otherwise constructs a shared_ptr that shares ownership with __r.
- Parameters
__rβ Ashared_ptr.
- Postcondition β
get() == __r.get() && use_count() == __r.use_count()
11. Move constructor (same type) πββοΈ
template<typename _Tp > std::shared_ptr< _Tp >::shared_ptr (shared_ptr< _Tp
> && __r) [inline], [noexcept]
Move-constructs a shared_ptr instance from __r.
- Parameters
__rβ Ashared_ptrrvalue.
- Postcondition β
*thiscontains the old value of__r,__ris empty.
12. Move constructor (converting) πββοΈπ¨
template<typename _Tp > template<typename _Yp , typename =
_Constructible<shared_ptr<_Yp>>> std::shared_ptr< _Tp >::shared_ptr
(shared_ptr< _Yp > && __r) [inline], [noexcept]
Move-constructs a shared_ptr instance from __r.
- Parameters
__rβ Ashared_ptrrvalue.
- Postcondition β
*thiscontains the old value of__r,__ris empty.
13. From a weak_ptr π§΅
template<typename _Tp > template<typename _Yp , typename =
_Constructible<const weak_ptr<_Yp>&>> std::shared_ptr< _Tp >::shared_ptr
(const weak_ptr< _Yp > & __r) [inline], [explicit]
Constructs a shared_ptr that shares ownership with __r and stores a copy of the pointer stored in __r.
- Parameters
__rβ Aweak_ptr.
- Postcondition β
use_count() == __r.use_count() - Exceptions β
bad_weak_ptrwhen__r.expired(), in which case the constructor has no effect.
Heads up bestie: if the weak_ptr already expired, this throws bad_weak_ptr and the constructor does literally nothing. No half-built object, no mess. π
14. The nullptr_t one π
template<typename _Tp > constexpr std::shared_ptr< _Tp >::shared_ptr
(nullptr_t) [inline], [constexpr], [noexcept]
Construct an empty shared_ptr.
- Postcondition β
use_count() == 0 && get() == nullptr
β Member Function Documentation β
Real talk β these are marked [inherited], so they come down from std::__shared_ptr< _Tp, _Lp > like a family heirloom π
get() π
template<typename _Tp , _Lock_policy _Lp> element_type * std::__shared_ptr<
_Tp, _Lp >::get () const [inline], [noexcept], [inherited]
Return the stored pointer. That's it, that's the function. Simple, clean, iconic β¨
operator bool() β
template<typename _Tp , _Lock_policy _Lp> std::__shared_ptr< _Tp, _Lp
>::operator bool () const [inline], [explicit], [noexcept],
[inherited]
Return true if the stored pointer is not null. So if (ptr) actually does something sensible, thank goodness π
owner_before() β the __shared_ptr overload π
template<typename _Tp , _Lock_policy _Lp> template<typename _Tp1 > bool
std::__shared_ptr< _Tp, _Lp >::owner_before (__shared_ptr< _Tp1, _Lp >
const & __rhs) const [inline], [noexcept], [inherited]
Define an ordering based on ownership. This function defines a strict weak ordering between two shared_ptr or weak_ptr objects, such that one object is less than the other unless they share ownership of the same pointer, or are both empty. π§ (yes, it's the thing you need for ordering them in sets and maps, iykyk)
owner_before() β the __weak_ptr overload π
template<typename _Tp , _Lock_policy _Lp> template<typename _Tp1 > bool
std::__shared_ptr< _Tp, _Lp >::owner_before (__weak_ptr< _Tp1, _Lp >
const & __rhs) const [inline], [noexcept], [inherited]
Define an ordering based on ownership. This function defines a strict weak ordering between two shared_ptr or weak_ptr objects, such that one object is less than the other unless they share ownership of the same pointer, or are both empty. π§
swap() π
template<typename _Tp , _Lock_policy _Lp> void std::__shared_ptr< _Tp, _Lp
>::swap (__shared_ptr< _Tp, _Lp > & __other) [inline], [noexcept],
[inherited]
Exchange both the owned pointer and the stored pointer. Both. Not one. Both. π«Ά
unique() 1οΈβ£
template<typename _Tp , _Lock_policy _Lp> bool std::__shared_ptr< _Tp, _Lp
>::unique () const [inline], [noexcept], [inherited]
Return true if use_count() == 1. You're the only owner, main character energy π
use_count() π’
template<typename _Tp , _Lock_policy _Lp> long std::__shared_ptr< _Tp, _Lp
>::use_count () const [inline], [noexcept], [inherited]
If *this owns a pointer, return the number of owners, otherwise zero.
πΈ Author πΈ
Generated automatically by Doxygen for libstdc++ from the source code. (Not a human, just a very diligent robot π§)
And that's the whole page bestie!! π You made it to the end, I'm so proud of you π«Ά
This gem is from libstdc++, and the footer beeps out std::shared_ptr< _Tp >(3cxx) β that's the section 3 C++ library page name, no date attached to this one, ngl. Go forth and share your ownership responsibly πβ¨
π¦ where this page comes from
- ubuntu noble: libstdc++-14-doc 14-20240412-0ubuntu1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms