Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does shared_ptr<T>::use_count() return a long instead of an unsigned type?

Tags:

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]

like image 505
ricky m Avatar asked Apr 02 '16 06:04

ricky m


People also ask

What does Use_count return in C++?

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.

Why is Shared_ptr unique deprecated?

this function is deprecated as of C++17 because use_count is only an approximation in multi-threaded environment.


1 Answers

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.

like image 185
AnT Avatar answered Nov 29 '22 13:11

AnT