Title
shared_ptr::use_count() is efficient
Status
c++17
Section
[util.smartptr.shared.obs]
Submitter
Stephan T. Lavavej

Created on 2014-10-01.00:00:00 last changed 90 months ago

Messages

Date: 2014-11-08.16:43:57

Proposed resolution:

This wording is relative to N3936.

  1. Change [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: use_count() == 1.

    -10- [Note: unique() may be faster than use_count(). If you are using unique() to implement copy on write, do not rely on a specific value when get() == 0. — end note]

  2. Change [util.smartptr.weak.obs] p1-p4 as depicted:

    long use_count() const noexcept;
    

    -1- Returns: 0 if *this is empty; otherwise, the number of shared_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 than use_count(). — end note]

Date: 2014-11-08.16:43:57

[ Urbana 2014-11-07: Move to Ready ]

Date: 2014-10-01.00:00:00

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.

History
Date User Action Args
2017-07-30 20:15:43adminsetstatus: wp -> c++17
2015-05-22 18:31:21adminsetstatus: ready -> wp
2014-11-08 16:43:57adminsetmessages: + msg7181
2014-11-08 16:43:57adminsetstatus: new -> ready
2014-10-07 19:50:01adminsetmessages: + msg7127
2014-10-01 00:00:00admincreate