Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::shared_ptr<T>::shared_ptr

      From cppreference.com
      <cpp‎ |memory‎ |shared ptr
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
      std::shared_ptr
      Member functions
      shared_ptr::shared_ptr
      Modifiers
      Observers
      (until C++20*)
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      functions(until C++26*)
      Helper classes
      Deduction guides(C++17)
       
      constexpr shared_ptr()noexcept;
      (1)
      constexpr shared_ptr(std::nullptr_t)noexcept;
      (2)
      template<class Y>
      explicit shared_ptr( Y* ptr);
      (3)
      template<class Y,class Deleter>
      shared_ptr( Y* ptr, Deleter d);
      (4)
      template<class Deleter>
      shared_ptr(std::nullptr_t ptr, Deleter d);
      (5)
      template<class Y,class Deleter,class Alloc>
      shared_ptr( Y* ptr, Deleter d, Alloc alloc);
      (6)
      template<class Deleter,class Alloc>
      shared_ptr(std::nullptr_t ptr, Deleter d, Alloc alloc);
      (7)
      template<class Y>
      shared_ptr(const shared_ptr<Y>& r, element_type* ptr)noexcept;
      (8)
      template<class Y>
      shared_ptr( shared_ptr<Y>&& r, element_type* ptr)noexcept;
      (8)(since C++20)
      shared_ptr(const shared_ptr& r)noexcept;
      (9)
      template<class Y>
      shared_ptr(const shared_ptr<Y>& r)noexcept;
      (9)
      shared_ptr( shared_ptr&& r)noexcept;
      (10)
      template<class Y>
      shared_ptr( shared_ptr<Y>&& r)noexcept;
      (10)
      template<class Y>
      explicit shared_ptr(conststd::weak_ptr<Y>& r);
      (11)
      template<class Y>
      shared_ptr(std::auto_ptr<Y>&& r);
      (12)(removed in C++17)
      template<class Y,class Deleter>
      shared_ptr(std::unique_ptr<Y, Deleter>&& r);
      (13)

      Constructs newshared_ptr from a variety of pointer types that refer to an object to manage.

      For the purposes of the description below, a pointer typeY* is said to becompatible with a pointer typeT* if eitherY* is convertible toT* orY is the array typeU[N] andT isU cv [] (where cv is some set of cv-qualifiers).

      (since C++17)
      1,2) Constructs ashared_ptr with no managed object, i.e. emptyshared_ptr.
      3-7) Constructs ashared_ptr withptr as the pointer to the managed object.

      For(3,4,6),Y* must be convertible toT*.

      (until C++17)

      IfT is an array typeU[N],(3,4,6) do not participate in overload resolution ifY(*)[N] is an invalid type or not convertible toT*. IfT is an array typeU[],(3,4,6) do not participate in overload resolution ifY(*)[] is an invalid type or not convertible toT*. Otherwise,(3,4,6) do not participate in overload resolution ifY* is not convertible toT*.

      (since C++17)
      Additionally:
      3) Uses thedelete-expressiondelete ptr ifT is not an array type;delete[] ptr ifT is an array type(since C++17) as the deleter.Y must be a complete type. The delete expression must be well-formed, have well-defined behavior and not throw any exceptions.This constructor additionally does not participate in overload resolution if the delete expression is not well-formed.(since C++17)
      4,5) Uses the specified deleterd as the deleter. The expressiond(ptr) must be well formed, have well-defined behavior and not throw any exceptions. The construction ofd and of the stored deleter copied from it must not throw exceptions.

      Deleter must beCopyConstructible.

      (until C++17)

      These constructors additionally do not participate in overload resolution if the expressiond(ptr) is not well-formed, or ifstd::is_move_constructible_v<D> isfalse.

      (since C++17)
      6,7) Same as(4,5), but additionally uses a copy ofalloc for allocation of data for internal use.Alloc must be anAllocator.
      8) Thealiasing constructor: constructs ashared_ptr which shares ownership information with the initial value ofr, but holds an unrelated and unmanaged pointerptr. If thisshared_ptr is the last of the group to go out of scope, it will call the stored deleter for the object originally managed byr. However, callingget() on thisshared_ptr will always return a copy ofptr. It is the responsibility of the programmer to make sure that thisptr remains valid as long as this shared_ptr exists, such as in the typical use cases whereptr is a member of the object managed byr or is an alias (e.g., downcast) ofr.get()For the second overload taking an rvalue,r is empty andr.get()== nullptr after the call.(since C++20)
      9) Constructs ashared_ptr which shares ownership of the object managed byr. Ifr manages no object,*this manages no object either. The template overload doesn't participate in overload resolution ifY* is notimplicitly convertible to(until C++17)compatible with(since C++17)T*.
      10) Move-constructs ashared_ptr fromr. After the construction,*this contains a copy of the previous state ofr,r is empty and its stored pointer is null. The template overload doesn't participate in overload resolution ifY* is notimplicitly convertible to(until C++17)compatible with(since C++17)T*.
      11) Constructs ashared_ptr which shares ownership of the object managed byr.Y* must be implicitly convertible toT*.(until C++17)This overload participates in overload resolution only ifY* is compatible withT*.(since C++17) Note thatr.lock() may be used for the same purpose: the difference is that this constructor throws an exception if the argument is empty, whilestd::weak_ptr<T>::lock() constructs an emptystd::shared_ptr in that case.
      12) Constructs ashared_ptr that stores and owns the object formerly owned byr.Y* must be convertible toT*. After construction,r is empty.
      13) Constructs ashared_ptr which manages the object currently managed byr. The deleter associated withr is stored for future deletion of the managed object.r manages no object after the call.
      This overload doesn't participate in overload resolution ifstd::unique_ptr<Y, Deleter>::pointer is notcompatible withT*.Ifr.get() is a null pointer, this overload is equivalent to the default constructor(1).(since C++17)
      IfDeleter is a reference type, it is equivalent toshared_ptr(r.release(),std::ref(r.get_deleter()). Otherwise, it is equivalent toshared_ptr(r.release(), std::move(r.get_deleter())).

      WhenT is not an array type, the overloads(3,4,6) enableshared_from_this withptr, and the overload(13) enablesshared_from_this with the pointer returned byr.release().

      Contents

      [edit]Parameters

      ptr - a pointer to an object to manage
      d - a deleter to use to destroy the object
      alloc - an allocator to use for allocations of data for internal use
      r - another smart pointer to share the ownership to or acquire the ownership from

      [edit]Postconditions

      1,2)use_count() equals0 andget() equalsnullptr.
      3-7)use_count() equals1 andget() equalsptr.
      8)get() equalsptr. For the second overload,r is empty andr.get() equalsnullptr.
      9)get() equalsr.get() anduse_count() equalsr.use_count().
      10)r shall be empty andr.get() shall equalnullptr, and*this shall be the old value ofr.
      11)use_count() equalsr.use_count().
      12)use_count() equals1 andr.get() equalsnullptr.

      [edit]Exceptions

      3)std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors. If an exception occurs, this callsdelete ptr ifT is not an array type, and callsdelete[] ptr otherwise(since C++17).
      4-7)std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors.d(ptr) is called if an exception occurs.
      11)std::bad_weak_ptr ifr.expired()==true. The constructor has no effect in this case.
      12)std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors. This constructor has no effect if an exception occurs.
      13) If an exception is thrown, the constructor has no effects.

      [edit]Notes

      A constructorenablesshared_from_this with a pointerptr of typeU* means that it determines ifU has anunambiguous and accessible(since C++17) base class that is a specialization ofstd::enable_shared_from_this, and if so, the constructor evaluatesif(ptr!= nullptr&& ptr->weak_this .expired())
          ptr->weak_this =std::shared_ptr<std::remove_cv_t<U>>
              (*this,const_cast<std::remove_cv_t<U>*>(ptr));
      .

      The assignment to theweak_this is not atomic and conflicts with any potentially concurrent access to the same object. This ensures that future calls toshared_from_this() would share ownership with thestd::shared_ptr created by this raw pointer constructor.

      The testptr->weak_this .expired() in the code above makes sure thatweak_this is not reassigned if it already indicates an owner. This test is required as of C++17.

      The raw pointer overloads assume ownership of the pointed-to object. Therefore, constructing ashared_ptr using the raw pointer overload for an object that is already managed by ashared_ptr, such as byshared_ptr(ptr.get()) is likely to lead to undefined behavior, even if the object is of a type derived fromstd::enable_shared_from_this.

      Because the default constructor isconstexpr, static shared_ptrs are initialized as part ofstatic non-local initialization, before any dynamic non-local initialization begins. This makes it safe to use a shared_ptr in a constructor of any static object.

      In C++11 and C++14 it is valid to construct astd::shared_ptr<T> from astd::unique_ptr<T[]>:

      std::unique_ptr<int[]> arr(newint[1]);std::shared_ptr<int> ptr(std::move(arr));

      Since theshared_ptr obtains its deleter (astd::default_delete<T[]> object) from thestd::unique_ptr, the array will be correctly deallocated.

      This is no longer allowed in C++17. Instead the array formstd::shared_ptr<T[]> should be used.

      [edit]Example

      Run this code
      #include <iostream>#include <memory> struct Foo{int id{0};    Foo(int i=0): id{i}{std::cout<<"Foo::Foo("<< i<<")\n";}    ~Foo(){std::cout<<"Foo::~Foo(), id="<< id<<'\n';}}; struct D{void operator()(Foo* p)const{std::cout<<"Call delete from function object. Foo::id="<< p->id<<'\n';        delete p;}}; int main(){{std::cout<<"1) constructor with no managed object\n";std::shared_ptr<Foo> sh1;} {std::cout<<"2) constructor with object\n";std::shared_ptr<Foo> sh2(new Foo{10});std::cout<<"sh2.use_count(): "<< sh2.use_count()<<'\n';std::shared_ptr<Foo> sh3(sh2);std::cout<<"sh2.use_count(): "<< sh2.use_count()<<'\n';std::cout<<"sh3.use_count(): "<< sh3.use_count()<<'\n';} {std::cout<<"3) constructor with object and deleter\n";std::shared_ptr<Foo> sh4(new Foo{11}, D());std::shared_ptr<Foo> sh5(new Foo{12},[](auto p){std::cout<<"Call delete from lambda... p->id="<< p->id<<'\n';            delete p;});}}

      Output:

      1) constructor with no managed object2) constructor with objectFoo::Foo(10)sh2.use_count(): 1sh2.use_count(): 2sh3.use_count(): 2Foo::~Foo(), id=103) constructor with object and deleterFoo::Foo(11)Foo::Foo(12)Call delete from lambda... p->id=12Foo::~Foo(), id=12Call delete from function object. Foo::id=11Foo::~Foo(), id=11

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3548C++11the constructor fromunique_ptr copy-constructed the deletermove-constructs instead

      [edit]See also

      creates a shared pointer that manages a new object
      (function template)[edit]
      creates a shared pointer that manages a new object allocated using an allocator
      (function template)[edit]
      allows an object to create ashared_ptr referring to itself
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/shared_ptr/shared_ptr&oldid=183081"

      [8]ページ先頭

      ©2009-2025 Movatter.jp