std::experi...::shared_ptr(3)
πΈ std::experi...::shared_ptr::shared_ptr πΈ
okay so bestie, you're on the std::experi...::shared_ptr(3) page from the C++ Standard Libary β yes, that's the manual name, spelling and all, we're not editing it π
. This is the constructor page for std::experimental::shared_ptr::shared_ptr, and it's lowkey a whole menu of ways to build one. Let's go. π«Ά
π NAME π
std::experimental::shared_ptr::shared_ptr β std::experimental::shared_ptr::shared_ptr. iconic constructor behavior, no notes.
π¦ Synopsis π¦
constexpr shared_ptr() noexcept; (1)
constexpr shared_ptr( std::nullptr_t ) noexcept; (2)
template< class Y > (3)
explicit shared_ptr( Y* ptr );
template< class Y, class Deleter > (4)
shared_ptr( Y* ptr, Deleter d );
template< class Deleter > (5)
shared_ptr( std::nullptr_t ptr, Deleter d );
template< class Y, class Deleter, class Alloc > (6)
shared_ptr( Y* ptr, Deleter d, Alloc alloc );
template< class Deleter, class Alloc > (7)
shared_ptr( std::nullptr_t ptr, Deleter d, Alloc alloc );
template< class Y > (8)
shared_ptr( const shared_ptr<Y>& r, element_type *ptr ) noexcept;
shared_ptr( const shared_ptr& r ) noexcept; (9)
template< class Y > (9)
shared_ptr( const shared_ptr<Y>& r ) noexcept;
shared_ptr( shared_ptr&& r ) noexcept;
(10)
template< class Y >
(10)
shared_ptr( shared_ptr<Y>&& r ) noexcept;
template< class Y >
(11)
explicit shared_ptr( const std::weak_ptr<Y>& r );
template< class Y >
(12)
shared_ptr( std::auto_ptr<Y>&& r );
template< class Y, class Deleter >
(13)
shared_ptr( std::unique_ptr<Y,Deleter>&& r );
Constructs a new shared_ptr from a variety of pointer types that refer to an object to manage. slay.
For the purposes of the description below, a pointer type Y* is said to be compatible with a pointer type T* if either Y* is convertible to T* or Y is the array type U[N] and T is U cv [] (where cv is some set of cv-qualifiers).
1,2)β Constructs ashared_ptrwith no managed object, i.e. emptyshared_ptr.3-7)β Constructs ashared_ptrwithptras the pointer to the managed object. IfTis an array typeU[N],Y(*)[N]must be convertible toT*. IfTis an array typeU[],Y(*)[]must be convertible toT*. Otherwise,Y*must be convertible toT*. Additionally:3)β Uses a delete-expression (delete ptr, ifTis not an array type;delete[] ptrifTis an array type) as the deleter.Ymust 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 deleterdas the deleter. The expressiond(ptr)must be well formed, have well-defined behavior and not throw any exceptions.Deletermust beCopyConstructible, and its copy constructor and destructor must not throw exceptions.6,7)β Same as (4,5), but additionally uses a copy ofallocfor allocation of data for internal use.Allocmust be aAllocator, and its copy constructor and destructor must not throw exceptions.
8)β The aliasing constructor: constructs ashared_ptrwhich shares ownership information withr, but holds an unrelated and unmanaged pointerptr. Even if thisshared_ptris 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 thisptrremains valid as long as thisshared_ptrexists, such as in the typical use cases whereptris a member of the object managed byror is an alias (e.g., downcast) ofr.get().9)β Constructs ashared_ptrwhich shares ownership of the object managed byr. Ifrmanages no object,*thismanages no object too. The template overload doesn't participate in overload resolution ifY*is not compatible withT*.10)β Move-constructs ashared_ptrfromr. After the construction,*thiscontains a copy of the previous state ofr,ris empty. The template overload doesn't participate in overload resolution ifY*is not compatible withT*.11)β Constructs ashared_ptrwhich shares ownership of the object managed byr.Y*must be compatible 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_ptrin that case.12)β Constructs ashared_ptrthat stores and owns the object formerly owned byr.Y*must be convertible toT*. After construction,ris empty.13)β Constructs ashared_ptrwhich manages the object currently managed byr. The deleter associated withris stored for future deletion of the managed object.rmanages no object after the call. This overload doesn't participate in overload resolution ifY*is not compatible withT*. IfDis a reference type, equivalent toshared_ptr(r.release(), std::ref(r.get_deleter()).Otherwise, equivalent toshared_ptr(r.release(), r.get_deleter()).
πΌ Notes πΌ
When constructing a shared_ptr from a raw pointer to an object of a type derived from std::experimental::enable_shared_from_this, the constructors of shared_ptr update the private weak_ptr member of the std::experimental::enable_shared_from_this base so that future calls to shared_from_this() would share ownership with the shared_ptr created by this raw pointer constructor. cute, but also serious.
π¨ bestie, warning: the raw pointer overloads assume ownership of the pointed-to object, and so constructing ashared_ptrusing the raw pointer overload for an object that is already managed by ashared_ptrmay lead to undefined behavior, even if the object is of a type derived fromstd::experimental::enable_shared_from_this. no cap, do not do this.
π Parameters π
ptrβ a pointer to an object to managedβ a deleter to use to destroy the objectallocβ an allocator to use for allocations of data for internal userβ another smart pointer to share the ownership to or acquire the ownership from
π₯ Exceptions π₯
3)βstd::bad_allocif required additional memory could not be obtained. May throw implementation-defined exception for other errors. The applicable delete-expression (delete ptrifTis not an array type,delete[] ptrotherwise) is called if an exception occurs.4-7)βstd::bad_allocif 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_ptrifr.expired() == true. The constructor has no effect in this case.12)βstd::bad_allocif 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.
π Example π
This section is incomplete Reason: no example
π it's giving "we'll circle back".
π See also π
make_sharedβ creates a shared pointer that manages a new objectmake_shared_for_overwriteβ (function template) (C++20)allocate_sharedβ creates a shared pointer that manages a new objectallocate_shared_for_overwriteβ allocated using an allocator (C++20) (function template)
π Categories π
- Noindexed pages
- Todo no example
mwah π this page is from http://cppreference.com, version/date 2024.06.10. go build something iconic, bestie. π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms