A weak_ptr is technically a means to bind to the reference counter of the shared_ptr set, which manages some shared object. When the last shared_ptr is destroyed, the object is destroyed, but its reference counter works as long as it has weak_ptr . Thus, with any remaining weak_ptr you can check if the object exists or has been destroyed.
If it still exists, then from weak_ptr you can get shared_ptr , which allows you to reference the object.
The main use of this is loop interruption.
In particular, the object may contain weak_ptr , which holds its own reference counter, which allows you to get the shared_ptr object for the object from the object itself. That is, a shared_ptr that uses the same reference counter as another shared_ptr for this object. How enable_shared_from_this works.
unique_ptr does not have any reference counter, so it makes no sense to hang on this nonexistent counter.
source share