The C++ draft standard mandates the following constructors for weak_ptr
:
weak_ptr(const weak_ptr& r) noexcept;
template<class Y> weak_ptr(const weak_ptr<Y>& r) noexcept;
template<class Y> weak_ptr(const shared_ptr<Y>& r) noexcept;
weak_ptr(weak_ptr&& r) noexcept;
template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept;
I'm surprised to not see the following constructor:
template<class Y> weak_ptr(shared_ptr<Y>&& r) noexcept;
Of course the existing constructor from const reference to shared_ptr
works functionally in any context where the rvalue reference version would work. But I believe the latter would allow saving at least two atomic operations in the case where the caller wants to destructively convert a shared reference to a weak one by using a shared_ptr
rvalue to initialize a weak_ptr
.
A weak_ptr<>
holds a non-owning pointer to an object managed by a shared_ptr<>
. The object is deleted when the last shared_ptr<>
reference to it is deleted.
If the constructor you miss would exist, it could happen that you pass the last existing shared_ptr<>
as parameter, which would destroy the object in the process of the move. So you would be left with a weak_ptr<>
that points to an already deleted object.
Does not make much sense, right?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.