std::shared_ptr< _Tp >(3) Library Functions Manual std::shared_ptr< _Tp >(3) NAME std::shared_ptr< _Tp > - A smart pointer with reference-counted copy semantics. SYNOPSIS #include Inherits std::__shared_ptr< _Tp, _Lp >. Public Types using element_type The type pointed to by the stored pointer, remove_extent_t<_Tp> using weak_type The corresponding weak_ptr type for this shared_ptr. Public Member Functions constexpr shared_ptr () noexcept Construct an empty shared_ptr. template> shared_ptr (_Yp *__p) Construct a shared_ptr that owns the pointer __p. template> shared_ptr (_Yp *__p, _Deleter __d) Construct a shared_ptr that owns the pointer __p and the deleter __d. template> shared_ptr (_Yp *__p, _Deleter __d, _Alloc __a) Construct a shared_ptr that owns the pointer __p and the deleter __d. template>> shared_ptr (auto_ptr< _Yp > &&__r) shared_ptr (const shared_ptr &) noexcept=default Copy constructor. template&>> shared_ptr (const shared_ptr< _Yp > &__r) noexcept If __r is empty, constructs an empty shared_ptr; otherwise construct a shared_ptr that shares ownership with __r. template shared_ptr (const shared_ptr< _Yp > &__r, element_type *__p) noexcept Constructs a shared_ptr instance that stores __p and shares ownership with __r. template&>> shared_ptr (const weak_ptr< _Yp > &__r) Constructs a shared_ptr that shares ownership with __r and stores a copy of the pointer stored in __r. template shared_ptr (nullptr_t __p, _Deleter __d) Construct a shared_ptr that owns a null pointer and the deleter __d. template shared_ptr (nullptr_t __p, _Deleter __d, _Alloc __a) Construct a shared_ptr that owns a null pointer and the deleter __d. constexpr shared_ptr (nullptr_t) noexcept Construct an empty shared_ptr. shared_ptr (shared_ptr &&__r) noexcept Move-constructs a shared_ptr instance from __r. template>> shared_ptr (shared_ptr< _Yp > &&__r) noexcept Move-constructs a shared_ptr instance from __r. template shared_ptr (shared_ptr< _Yp > &&__r, element_type *__p) noexcept Constructs a shared_ptr instance that stores __p and shares ownership with __r. template shared_ptr (std::auto_ptr< _Tp1 > &&__r) template>> shared_ptr (unique_ptr< _Yp, _Del > &&__r) element_type * get () const noexcept Return the stored pointer. operator bool () const noexcept Return true if the stored pointer is not null. element_type & operator* () const noexcept element_type * operator-> () const noexcept template _Assignable< auto_ptr< _Yp > > operator= (auto_ptr< _Yp > &&__r) shared_ptr & operator= (const shared_ptr &) noexcept=default template _Assignable< const shared_ptr< _Yp > & > operator= (const shared_ptr< _Yp > &__r) noexcept shared_ptr & operator= (shared_ptr &&__r) noexcept template _Assignable< shared_ptr< _Yp > > operator= (shared_ptr< _Yp > &&__r) noexcept template _Assignable< unique_ptr< _Yp, _Del > > operator= (unique_ptr< _Yp, _Del > &&__r) void reset () noexcept template _SafeConv< _Yp > reset (_Yp *__p) template _SafeConv< _Yp > reset (_Yp *__p, _Deleter __d) template _SafeConv< _Yp > reset (_Yp *__p, _Deleter __d, _Alloc __a) void swap (__shared_ptr< _Tp, _Lp > &__other) noexcept Exchange both the owned pointer and the stored pointer. bool unique () const noexcept Return true if use_count() == 1. long use_count () const noexcept If *this owns a pointer, return the number of owners, otherwise zero. template bool owner_before (__shared_ptr< _Tp1, _Lp > const &__rhs) const noexcept Define an ordering based on ownership. template bool owner_before (__weak_ptr< _Tp1, _Lp > const &__rhs) const noexcept Define an ordering based on ownership. Friends template shared_ptr< _BoundedArray< _Yp > > allocate_shared (const _Alloc &) template shared_ptr< _NonArray< _Yp > > allocate_shared (const _Alloc &, _Args &&...) template shared_ptr< _BoundedArray< _Yp > > allocate_shared (const _Alloc &, const remove_extent_t< _Yp > &) template shared_ptr< _UnboundedArray< _Yp > > allocate_shared (const _Alloc &, size_t) template shared_ptr< _UnboundedArray< _Yp > > allocate_shared (const _Alloc &, size_t, const remove_extent_t< _Yp > &) template shared_ptr< _NotUnboundedArray< _Yp > > allocate_shared_for_overwrite (const _Alloc &) template shared_ptr< _UnboundedArray< _Yp > > allocate_shared_for_overwrite (const _Alloc &, size_t) template shared_ptr< _BoundedArray< _Yp > > make_shared () template shared_ptr< _NonArray< _Yp > > make_shared (_Args &&...) template shared_ptr< _BoundedArray< _Yp > > make_shared (const remove_extent_t< _Yp > &) template shared_ptr< _UnboundedArray< _Yp > > make_shared (size_t) template shared_ptr< _UnboundedArray< _Yp > > make_shared (size_t, const remove_extent_t< _Yp > &) template shared_ptr< _NotUnboundedArray< _Yp > > make_shared_for_overwrite () template shared_ptr< _UnboundedArray< _Yp > > make_shared_for_overwrite (size_t) class weak_ptr< _Tp > Related Symbols (Note that these are not member symbols.) template _Del * get_deleter (const shared_ptr< _Tp > &__p) noexcept 20.7.2.2.10 shared_ptr get_deleter template std::basic_ostream< _Ch, _Tr > & operator<< (std::basic_ostream< _Ch, _Tr > &__os, const __shared_ptr< _Tp, _Lp > &__p) Write the stored pointer to an ostream. template bool operator== (const shared_ptr< _Tp > &__a, const shared_ptr< _Up > &__b) noexcept template bool operator== (const shared_ptr< _Tp > &__a, nullptr_t) noexcept shared_ptr comparison with nullptr template void swap (shared_ptr< _Tp > &__a, shared_ptr< _Tp > &__b) noexcept Swap overload for shared_ptr. template shared_ptr< _Tp > static_pointer_cast (const shared_ptr< _Up > &__r) noexcept Convert type of shared_ptr, via static_cast template shared_ptr< _Tp > const_pointer_cast (const shared_ptr< _Up > &__r) noexcept Convert type of shared_ptr, via const_cast template shared_ptr< _Tp > dynamic_pointer_cast (const shared_ptr< _Up > &__r) noexcept Convert type of shared_ptr, via dynamic_cast template shared_ptr< _Tp > reinterpret_pointer_cast (const shared_ptr< _Up > &__r) noexcept Convert type of shared_ptr, via reinterpret_cast template shared_ptr< _Tp > static_pointer_cast (shared_ptr< _Up > &&__r) noexcept Convert type of shared_ptr rvalue, via static_cast template shared_ptr< _Tp > const_pointer_cast (shared_ptr< _Up > &&__r) noexcept Convert type of shared_ptr rvalue, via const_cast template shared_ptr< _Tp > dynamic_pointer_cast (shared_ptr< _Up > &&__r) noexcept Convert type of shared_ptr rvalue, via dynamic_cast template shared_ptr< _Tp > reinterpret_pointer_cast (shared_ptr< _Up > &&__r) noexcept Convert type of shared_ptr rvalue, via reinterpret_cast template shared_ptr< _NonArray< _Tp > > allocate_shared (const _Alloc &__a, _Args &&... __args) Create an object that is owned by a shared_ptr. template shared_ptr< _NonArray< _Tp > > make_shared (_Args &&... __args) Create an object that is owned by a shared_ptr. template shared_ptr< _UnboundedArray< _Tp > > allocate_shared (const _Alloc &__a, size_t __n) Create an object that is owned by a shared_ptr. template shared_ptr< _BoundedArray< _Tp > > allocate_shared (const _Alloc &__a) Create an object that is owned by a shared_ptr. Detailed Description template 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. Member Typedef Documentation template using std::shared_ptr< _Tp >::element_type The type pointed to by the stored pointer, remove_extent_t<_Tp> template using std::shared_ptr< _Tp >::weak_type The corresponding weak_ptr type for this shared_ptr. Since C++17 Constructor & Destructor Documentation template constexpr std::shared_ptr< _Tp >::shared_ptr () [inline], [constexpr], [noexcept] Construct an empty shared_ptr. Postcondition use_count()==0 && get()==0 template std::shared_ptr< _Tp >::shared_ptr (const shared_ptr< _Tp > &) [default], [noexcept] Copy constructor. template template> 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 to element_type*. Postcondition use_count() == 1 && get() == __p Exceptions std::bad_alloc,in which case delete __p is called. template template> 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) template template 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) template template> 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) template template 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) template template 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. Parameters __r A shared_ptr. __p A pointer that will remain valid while *__r is 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> pii(new pair()); shared_ptr pi(pii, &pii->first); assert(pii.use_count() == 2); template template 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 A shared_ptr. __p A pointer that will remain valid while *__r is 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> pii(new pair()); shared_ptr pi1(pii, &pii->first); assert(pii.use_count() == 2); shared_ptr pi2(std::move(pii), &pii->second); assert(pii.use_count() == 0); template template&>> std::shared_ptr< _Tp >::shared_ptr (const shared_ptr< _Yp > & __r) [inline], [noexcept] If __r is empty, constructs an empty shared_ptr; otherwise construct a shared_ptr that shares ownership with __r. Parameters __r A shared_ptr. Postcondition get() == __r.get() && use_count() == __r.use_count() template std::shared_ptr< _Tp >::shared_ptr (shared_ptr< _Tp > && __r) [inline], [noexcept] Move-constructs a shared_ptr instance from __r. Parameters __r A shared_ptr rvalue. Postcondition *this contains the old value of __r, __r is empty. template template>> std::shared_ptr< _Tp >::shared_ptr (shared_ptr< _Yp > && __r) [inline], [noexcept] Move-constructs a shared_ptr instance from __r. Parameters __r A shared_ptr rvalue. Postcondition *this contains the old value of __r, __r is empty. template template&>> 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 A weak_ptr. Postcondition use_count() == __r.use_count() Exceptions bad_weak_ptr when __r.expired(), in which case the constructor has no effect. template 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 template element_type * std::__shared_ptr< _Tp, _Lp >::get () const [inline], [noexcept], [inherited] Return the stored pointer. template std::__shared_ptr< _Tp, _Lp >::operator bool () const [inline], [explicit], [noexcept], [inherited] Return true if the stored pointer is not null. template template 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. template template 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. template void std::__shared_ptr< _Tp, _Lp >::swap (__shared_ptr< _Tp, _Lp > & __other) [inline], [noexcept], [inherited] Exchange both the owned pointer and the stored pointer. template bool std::__shared_ptr< _Tp, _Lp >::unique () const [inline], [noexcept], [inherited] Return true if use_count() == 1. template 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. libstdc++ std::shared_ptr< _Tp >(3)