Share via


shared_ptr Class

Wraps a reference-counted smart pointer around a dynamically allocated object.

template<class Ty>
   class shared_ptr {
public:
    typedef Ty element_type;

    shared_ptr();
    shared_ptr(nullptr_t); 
    shared_ptr(const shared_ptr& sp);
    shared_ptr(shared_ptr&& sp);
    template<class Other>
        explicit shared_ptr(Other * ptr);
    template<class Other, class D>
        shared_ptr(Other * ptr, D dtor);
    template<class D>
        shared_ptr(nullptr_t, D dtor);
    template<class Other, class D, class A>
        shared_ptr(Other *ptr, D dtor, A alloc);
    template<class D, class A>
        shared_ptr(nullptr_t, D dtor, A alloc);
    template<class Other>
        shared_ptr(const shared_ptr<Other>& sp);
    template<class Other>
        shared_ptr(const shared_ptr<Other>&& sp);
    template<class Other>
        explicit shared_ptr(const weak_ptr<Other>& wp);
    template<class Other>
        shared_ptr(auto_ptr<Other>& ap);
    template<class Other, class D>
        shared_ptr(unique_ptr<Other, D>&& up);
    template<class Other>
        shared_ptr(const shared_ptr<Other>& sp, Ty *ptr);
    ~shared_ptr();
    shared_ptr& operator=(const shared_ptr& sp);
    template<class Other> 
        shared_ptr& operator=(const shared_ptr<Other>& sp);
    shared_ptr& operator=(shared_ptr&& sp);
    template<class Other> 
        shared_ptr& operator=(shared_ptr<Other>&& sp);
    template<class Other> 
        shared_ptr& operator=(auto_ptr< Other >&& ap);
    template <class Other, class D> 
        shared_ptr& operator=(const unique_ptr< Other, D>& up) = delete;
    template <class Other, class D>
        shared_ptr& operator=(unique_ptr<Other, D>&& up);
    void swap(shared_ptr& sp);
    void reset();
    template<class Other>
        void reset(Other *ptr);
    template<class Other, class D>
        void reset(Other *ptr, D dtor);
    template<class Other, class D, class A>
        void reset(Other *ptr, D dtor, A alloc);
    Ty *get() const;
    Ty& operator*() const;
    Ty *operator->() const;
    long use_count() const;
    bool unique() const;
    operator bool() const;

    template<class Other>
        bool owner_before(shared_ptr<Other> const& ptr) const;
    template<class Other>
        bool owner_before(weak_ptr<Other> const& ptr) const;
    template<class D, class Ty> 
        D* get_deleter(shared_ptr<Ty> const& ptr);
};

Parameters

  • Ty
    The type controlled by the shared pointer.

  • Other
    The type controlled by the argument pointer.

  • ptr
    The pointer to copy.

  • D
    The type of the deleter.

  • A
    The type of the allocator.

  • dtor
    The deleter.

  • alloc
    The allocator.

  • sp
    The smart pointer to copy or move.

  • wp
    The weak pointer to copy or move.

  • ap
    The auto pointer to copy or move.

  • up
    The unique pointer to move.

Remarks

The template class describes an object that uses reference counting to manage resources. A shared_ptr object effectively holds a pointer to the resource that it owns or holds a null pointer. A resource can be owned by more than one shared_ptr object; when the last shared_ptr object that owns a particular resource is destroyed, the resource is freed.

A shared_ptr stops owning a resource when it is reassigned or reset.

The template argument Ty might be an incomplete type except as noted for certain member functions.

When a shared_ptr<Ty> object is constructed from a resource pointer of type G* or from a shared_ptr<G>, the pointer type G* must be convertible to Ty*. If it is not, the code will not compile. For example:

class F {};
class G : public F {};

shared_ptr<G> sp0(new G);   // okay, template parameter G and argument G*
shared_ptr<G> sp1(sp0);     // okay, template parameter G and argument shared_ptr<G>
shared_ptr<F> sp2(new G);   // okay, G* convertible to F*
shared_ptr<F> sp3(sp0);     // okay, template parameter F and argument shared_ptr<G>
shared_ptr<F> sp4(sp2);     // okay, template parameter F and argument shared_ptr<F>
shared_ptr<int> sp4(new G); // error, G* not convertible to int*
shared_ptr<int> sp5(sp2);   // error, template parameter int and argument shared_ptr<F>

A shared_ptr object owns a resource:

  • if it was constructed with a pointer to that resource,

  • if it was constructed from a shared_ptr object that owns that resource,

  • if it was constructed from a weak_ptr Class object that points to that resource, or

  • if ownership of that resource was assigned to it, either with shared_ptr::operator= or by calling the member function shared_ptr::reset.

The shared_ptr objects that own a resource share a control block. The control block holds:

  • the number of shared_ptr objects that own the resource,

  • the number of weak_ptr objects that point to the resource,

  • the deleter for that resource if it has one,

  • the custom allocator for the control block if it has one.

A shared_ptr object that is initialized by using a null pointer has a control block and is not empty. After a shared_ptr object releases a resource, it no longer owns that resource. After a weak_ptr object releases a resource, it no longer points to that resource.

When the number of shared_ptr objects that own a resource becomes zero, the resource is freed, either by deleting it or by passing its address to a deleter, depending on how ownership of the resource was originally created. When the number of shared_ptr objects that own a resource is zero, and the number of weak_ptr objects that point to that resource is zero, the control block is freed, using the custom allocator for the control block if it has one.

An empty shared_ptr object does not own any resources and has no control block.

A deleter is a function object that has a member function operator(). Its type must be copy constructible, and its copy constructor and destructor must not throw exceptions. It accepts one parameter, the object to be deleted.

Some functions take an argument list that defines properties of the resulting shared_ptr<Ty> or weak_ptr<Ty> object. You can specify such an argument list in several ways:

no arguments -- the resulting object is an empty shared_ptr object or an empty weak_ptr object.

ptr -- a pointer of type Other* to the resource to be managed. Ty must be a complete type. If the function fails (because the control block cannot be allocated) it evaluates the expression delete ptr.

ptr, dtor -- a pointer of type Other* to the resource to be managed and a deleter for that resource. If the function fails (because the control block cannot be allocated), it calls dtor(ptr), which must be well defined.

ptr, dtor, alloc -- a pointer of type Other* to the resource to be managed, a deleter for that resource, and an allocator to manage any storage that must be allocated and freed. If the function fails (because the control block can't be allocated) it calls dtor(ptr), which must be well defined.

sp -- a shared_ptr<Other> object that owns the resource to be managed.

wp -- a weak_ptr<Other> object that points to the resource to be managed.

ap -- an auto_ptr<Other> object that holds a pointer to the resource to be managed. If the function succeeds it calls ap.release(); otherwise it leaves ap unchanged.

In all cases, the pointer type Other* must be convertible to Ty*.

Thread Safety

Multiple threads can read and write different shared_ptr objects at the same time, even when the objects are copies that share ownership.

Requirements

Header: <memory>

Namespace: std

See Also

Reference

weak_ptr Class

Thread Safety in the Standard C++ Library

Other Resources

<memory> Members