This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++17 status.

2434. shared_ptr::use_count() is efficient

Section: 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.

  1. 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: 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 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 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]