std::shared_ptr<T>::use_count (3) - Linux Manuals
std::shared_ptr<T>::use_count: std::shared_ptr<T>::use_count
NAME
std::shared_ptr<T>::use_count - std::shared_ptr<T>::use_count
Synopsis
long use_count() const noexcept;
Returns the number of different shared_ptr instances (this included) managing the current object. If there is no managed object, 0 is returned.
In multithreaded environment, the value returned by use_count is approximate (typical implementations use a memory_order_relaxed load)
Parameters
(none)
Return value
the number of shared_ptr instances managing the current object or 0 if there is no managed object.
Notes
Common use cases include
* comparison with 0. If use_count returns zero, the shared pointer is empty and manages no objects (whether or not its stored pointer is null). In multithreaded environment, this does not imply that the destructor of the managed object has completed.
* comparison with 1. If use_count returns 1, there are no other owners. (The deprecated member function unique() is provided for this use case.) In multithreaded environment, this does not imply that the object is safe to modify because accesses to the managed object by former shared owners may not have completed, and because new shared owners may be introduced concurrently, such as by std::weak_ptr::lock.
Example
// Run this code
Output:
See also
unique checks whether the managed object is managed only by the current shared_ptr instance
(until C++20)