shared_ptr observers 20.8.2.2.5 C++14 Final Draft (n4296)
long use_count() const noexcept;
Returns: the number of
shared_ptr
objects,*this
included, that share ownership with*this
, or 0 when*this
is empty.[Note:
use_count()
is not necessarily efficient. — end note]
std::shared_ptr<T>::use_count Returns the number of different shared_ptr instances (this included) managing the current object. If there is no managed object, 0 is returned.
this function is deprecated as of C++17 because use_count is only an approximation in multi-threaded environment.
According to this page
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1450.html
The return type of use_count is signed to avoid pitfalls such as p.use_count() > -1 evaluating to false.
with a reference to
John Lakos, Large-Scale C++ Software Design, section 9.2.2, page 637, Addison-Wesley, July 1996, ISBN 0-201-63362-0.
Basically, it looks like a nanny-grade decision, tailored for freshman year software developers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With