#
The
shared_ptr class template stores a pointer, usually obtained
via
new. shared_ptr implements semantics of shared ownership;
the last remaining owner of the pointer is responsible for destroying
the object, or otherwise releasing the resources associated with the stored pointer
. A
shared_ptr is said to be empty if it does not own a pointer
.namespace std {
template<class T> class shared_ptr {
public:
using element_type = remove_extent_t<T>;
using weak_type = weak_ptr<T>;
constexpr shared_ptr() noexcept;
constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
template<class Y>
constexpr explicit shared_ptr(Y* p);
template<class Y, class D>
constexpr shared_ptr(Y* p, D d);
template<class Y, class D, class A>
constexpr shared_ptr(Y* p, D d, A a);
template<class D>
constexpr shared_ptr(nullptr_t p, D d);
template<class D, class A>
constexpr shared_ptr(nullptr_t p, D d, A a);
template<class Y>
constexpr shared_ptr(const shared_ptr<Y>& r, element_type* p) noexcept;
template<class Y>
constexpr shared_ptr(shared_ptr<Y>& r, element_type* p) noexcept;
constexpr shared_ptr(const shared_ptr& r) noexcept;
template<class Y>
constexpr shared_ptr(const shared_ptr<Y>& r) noexcept;
constexpr shared_ptr(shared_ptr&& r) noexcept;
template<class Y>
constexpr shared_ptr(shared_ptr<Y>& r) noexcept;
template<class Y>
constexpr explicit shared_ptr(const weak_ptr<Y>& r);
template<class Y, class D>
constexpr shared_ptr(unique_ptr<Y, D>& r);
constexpr ~shared_ptr();
constexpr shared_ptr& operator=(const shared_ptr& r) noexcept;
template<class Y>
constexpr shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
constexpr shared_ptr& operator=(shared_ptr&& r) noexcept;
template<class Y>
constexpr shared_ptr& operator=(shared_ptr<Y>& r) noexcept;
template<class Y, class D>
constexpr shared_ptr& operator=(unique_ptr<Y, D>& r);
constexpr void swap(shared_ptr& r) noexcept;
constexpr void reset() noexcept;
template<class Y>
constexpr void reset(Y* p);
template<class Y, class D>
constexpr void reset(Y* p, D d);
template<class Y, class D, class A>
constexpr void reset(Y* p, D d, A a);
constexpr element_type* get() const noexcept;
constexpr T& operator*() const noexcept;
constexpr T* operator->() const noexcept;
constexpr element_type& operator[](ptrdiff_t i) const;
constexpr long use_count() const noexcept;
constexpr explicit operator bool() const noexcept;
template<class U>
constexpr bool owner_before(const shared_ptr<U>& b) const noexcept;
template<class U>
constexpr bool owner_before(const weak_ptr<U>& b) const noexcept;
size_t owner_hash() const noexcept;
template<class U>
constexpr bool owner_equal(const shared_ptr<U>& b) const noexcept;
template<class U>
constexpr bool owner_equal(const weak_ptr<U>& b) const noexcept;
};
template<class T>
shared_ptr(weak_ptr<T>) -> shared_ptr<T>;
template<class T, class D>
shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>;
}
Specializations of
shared_ptr shall be
contextually convertible to
bool,
allowing their use in boolean expressions and declarations in conditions
. The template parameter
T of
shared_ptr
may be an incomplete type
. [
Note 1:
T can be a function type
. —
end note]
[
Example 1:
if (shared_ptr<X> px = dynamic_pointer_cast<X>(py)) {
}
—
end example]
For purposes of determining the presence of a data race, member functions shall
access and modify only the
shared_ptr and
weak_ptr objects
themselves and not objects they refer to
. Changes in
use_count() do not
reflect modifications that can introduce data races
.For the purposes of
[smartptr],
a pointer type
Y* is said to be
compatible with
a pointer type
T* when either
Y* is convertible to
T* or
Y is
U[N] and
T is
cv U[].