std::weak_ptr::use_count(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

std::weak_ptr::use_count β€” std::weak_ptr::use_count

Okay bestie, welcome to the std::weak_ptr::use_count(3) page from the C++ Standard Libary manual. This is lowkey the β€œhow many shared_ptrs are sharing ownership of this managed object right now?” check, and it is giving tiny-but-important details. ✨

🌸 Synopsis 🌸

long use_count() const noexcept;  (since C++11)

This icon has been around since C++11 β€” periodt. 🫢

πŸŽ€ Parameters πŸŽ€

(none) β€” literally no parameters, not even one. πŸ’…

πŸ¦‹ Return value πŸ¦‹

The number of shared_ptr instances sharing the ownership of the managed object at the instant of the call. That’s the whole tea. β˜•

πŸ”₯ Notes πŸ”₯

  • expired() may be faster than use_count(). Ngl, if you only need to know β€œis it gone?”, expired() might be the quicker vibe check. πŸ’¨
  • This function is inherently racy. 😭 If the managed object is shared among threads that might be creating and destroying copies of the shared_ptr, then the result is reliable only if it matches the number of copies uniquely owned by the calling thread, or zero; any other value may become stale before it can be used. So like… if other threads are out here making and breaking copies, don’t treat a random nonzero number as gospel, bestie. ⚠️
The β€œreliable only if…” caveat is not a suggestion β€” it’s the actual rule. Keep that energy. πŸ’€

⭐ Example ⭐

// Run this code

#include <iostream>
#include <memory>

std::weak_ptr<int> gwp;

void observe_gwp()
{
    std::cout << "use_count(): " << gwp.use_count() << "\t id: ";
    if (auto sp = gwp.lock())
        std::cout << *sp << '\n';
    else
        std::cout << "??\n";
}

void share_recursively(std::shared_ptr<int> sp, int depth)
{
    observe_gwp(); // : 2 3 4
    if (1 < depth)
        share_recursively(sp, depth - 1);
    observe_gwp(); // : 4 3 2
}

int main()
{
    observe_gwp();
    {
        auto sp = std::make_shared<int>(42);
        gwp = sp;
        observe_gwp(); // : 1
        share_recursively(sp, 3); // : 2 3 4 4 3 2
        observe_gwp(); // : 1
    }
    observe_gwp(); // : 0
}

This example is main character energy: it observes gwp, locks it, and prints either the pointed-to int or ??. The comments show the use counts along the way β€” // : 2 3 4, // : 4 3 2, and // : 2 3 4 4 3 2, iykyk. 🧚

πŸ“€ Output πŸ“€

use_count(): 0   id: ??
use_count(): 1   id: 42
use_count(): 2   id: 42
use_count(): 3   id: 42
use_count(): 4   id: 42
use_count(): 4   id: 42
use_count(): 3   id: 42
use_count(): 2   id: 42
use_count(): 1   id: 42
use_count(): 0   id: ??

πŸ‘€ See also πŸ‘€

  • expired β€” checks whether the referenced object was already deleted (public member function). Iconic duo, fr fr. πŸŽ€

Mwah πŸ’‹ β€” source/version: http://cppreference.com; date: 2024.06.10. Stay slay, bestie, and may your weak_ptrs never go stale without a vibe check. πŸŒˆπŸ‘‘

πŸ“¦ where this page comes from