Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::shared_ptr<T>::shared_ptr

      From cppreference.com
      <cpp‎ |experimental‎ |shared ptr
       
       
       
       
       
      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)
      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)
      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 be compatible 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).

      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. IfT is an array typeU[N],Y(*)[N] must be convertible toT*. IfT is an array typeU[],Y(*)[] must be convertible toT*. Otherwise,Y* must be convertible toT*. Additionally:
      3) Uses adelete-expression (delete ptr, ifT is not an array type;delete[] ptr ifT is an array type) as the deleter.Y must be a complete type. That delete expression must be well formed, have well-defined behavior and not throw any exceptions.
      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.Deleter must beCopyConstructible, and its copy constructor and destructor must not throw exceptions.
      6,7) Same as(4,5), but additionally uses a copy ofalloc for allocation of data for internal use.Alloc must be aAllocator, and its copy constructor and destructor must not throw exceptions.
      8) Thealiasing constructor: constructs ashared_ptr which shares ownership information withr, but holds an unrelated and unmanaged pointerptr. Even if thisshared_ptr is the last of the group to go out of scope, it will call the destructor for the object originally managed byr. However, callingget() on this 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().
      9) Constructs ashared_ptr which shares ownership of the object managed byr. Ifr manages no object,*this manages no object too. The template overload doesn't participate in overload resolution ifY* is notcompatible withT*.
      10) Move-constructs ashared_ptr fromr. After the construction,*this contains a copy of the previous state ofr,r is empty. The template overload doesn't participate in overload resolution ifY* is notcompatible withT*.
      11) Constructs ashared_ptr which shares ownership of the object managed byr.Y* must becompatible withT*. Note thatr.lock() may be used for the same purpose: the difference is that this constructor throws an exception if the argument is empty, whileweak_ptr<T>::lock() constructs an emptyshared_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 ifY* is notcompatible withT*.
      IfD is a reference type, equivalent toshared_ptr(r.release(),std::ref(r.get_deleter()). Otherwise, equivalent toshared_ptr(r.release(), r.get_deleter()).

      Contents

      [edit]Notes

      When constructing ashared_ptr from a raw pointer to an object of a type derived fromstd::experimental::enable_shared_from_this, the constructors ofshared_ptr update the privateweak_ptr member of thestd::experimental::enable_shared_from_this base so that future calls toshared_from_this() would share ownership with theshared_ptr created by this raw pointer constructor.

      The raw pointer overloads assume ownership of the pointed-to object, and so constructing ashared_ptr using the raw pointer overload for an object that is already managed by ashared_ptr may lead to undefined behavior, even if the object is of a type derived fromstd::experimental::enable_shared_from_this.

      [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]Exceptions

      3)std::bad_alloc if required additional memory could not be obtained. May throw implementation-defined exception for other errors. The applicable delete-expression (delete ptr ifT is not an array type,delete[] ptr otherwise) is called if an exception occurs.
      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]Example

      This section is incomplete
      Reason: no example

      [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]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/experimental/shared_ptr/shared_ptr&oldid=157773"

      [8]ページ先頭

      ©2009-2026 Movatter.jp