std::shared_ptr::unique(3)

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

πŸ’– NAME πŸ’–

okay so bestie, this is the C++ Standard Libary manual page, section 3, for std::shared_ptr::unique. And the original NAME line was literally std::shared_ptr::unique - std::shared_ptr::unique, so we're honoring that iconic duplication: the function is std::shared_ptr::unique, and it is also std::shared_ptr::unique. no notes. πŸŽ€


✨ Synopsis ✨

bool unique() const noexcept;  (deprecated in C++17)
                               (removed in C++20)

This member function checks whether *this is the only shared_ptr instance managing the current object β€” like, the actual question is whether use_count() == 1. that's the tea. πŸ“


🌸 Parameters 🌸

  • (none) β€” no parameters. not a single one. empty. 🫢

πŸ’– Return value πŸ’–

  • true if *this is the only shared_ptr instance managing the current object
  • false otherwise.

πŸ¦‹ Notes πŸ¦‹

This function was deprecated in C++17 and removed in C++20 because use_count is only an approximation in multithreaded environment (see Notes in use_count). so yeah, she's a legacy icon, not a current-era baddie. 😭

⚠️ technical caveat, bestie: the deprecation and removal are real. use_count is only an approximation in multithreaded environment β€” that's why the function got retired. don't @ me, @ the standard. πŸ’€

🧚 Example 🧚

Run this code:

// Run this code

#include <iostream>
#include <memory>

int main()
{
    auto sp1 = std::make_shared<int>(5);
    std::cout << std::boolalpha;
    std::cout << "sp1.unique() == " << sp1.unique() << '\n';

    std::shared_ptr<int> sp2 = sp1;
    std::cout << "sp1.unique() == " << sp1.unique() << '\n';
}

πŸ’… Output πŸ’…

sp1.unique() == true
sp1.unique() == false

🌈 See also 🌈

  • use_count β€” returns the number of shared_ptr objects referring to the same managed object (public member function).

okay that's the whole page, bestie β€” sourced from http://cppreference.com, dated 2024.06.10. mwah πŸ’‹

πŸ“¦ where this page comes from