shared_ptr::use_count()
is efficientSection: 20.3.2.2.6 [util.smartptr.shared.obs] Status: C++17 Submitter: Stephan T. Lavavej Opened: 2014-10-01 Last modified: 2017-07-30
Priority: 0
View all other issues in [util.smartptr.shared.obs].
View all issues with C++17 status.
Discussion:
shared_ptr
and weak_ptr
have Notes that their use_count()
might be inefficient.
This is an attempt to acknowledge reflinked implementations (which can be used by Loki smart pointers, for
example). However, there aren't any shared_ptr
implementations that use reflinking, especially
after C++11 recognized the existence of multithreading. Everyone uses atomic refcounts, so use_count()
is just an atomic load.
[Urbana 2014-11-07: Move to Ready]
Proposed resolution:
This wording is relative to N3936.
Change 20.3.2.2.6 [util.smartptr.shared.obs] p7-p10 as depicted:
long use_count() const noexcept;-7- Returns: the number of
shared_ptr
objects,*this
included, that share ownership with*this
, or 0 when*this
is empty.-8- [Note:use_count()
is not necessarily efficient. — end note]bool unique() const noexcept;-9- Returns:
-10- [Note:use_count() == 1
.If you are usingunique()
may be faster thanuse_count()
.unique()
to implement copy on write, do not rely on a specific value whenget() == 0
. — end note]
Change 20.3.2.3.6 [util.smartptr.weak.obs] p1-p4 as depicted:
long use_count() const noexcept;-1- Returns: 0 if
*this
is empty; otherwise, the number ofshared_ptr
instances that share ownership with*this
.-2- [Note:use_count()
is not necessarily efficient. — end note]bool expired() const noexcept;-3- Returns:
use_count() == 0
.-4- [Note:expired()
may be faster thanuse_count()
. — end note]