Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::shared_ptr<T>::use_count

      From cppreference.com
      <cpp‎ |memory‎ |shared ptr
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
      std::shared_ptr
      Member functions
      Modifiers
      Observers
      shared_ptr::use_count
      (until C++20*)
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      functions(until C++26*)
      Helper classes
      Deduction guides(C++17)
       
      long use_count()constnoexcept;

      Returns the number of differentshared_ptr instances (*this included) managing the current object. If there is no managed object,0 is returned.

      In multithreaded environment, use_count atomically retrieves the number of instances (typical implementations use amemory_order_relaxed load).

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      The number ofstd::shared_ptr instances managing the current object or0 if there is no managed object.

      [edit]Notes

      Common use cases include

      • comparison with0. Ifuse_count returns zero, the shared pointer isempty and manages no objects (whether or not its stored pointer isnullptr).
      • comparison with1. Ifuse_count returns 1, there are no other owners.Thedeprecated(since C++17) member functionunique() is provided for this use case.(until C++20)

      In multithreaded environment

      The value returned byuse_count should be considered approximate, as the number of shared owners might change in other threads between the atomic retrieval and meaningful use of the value. Whenuse_count returns 1, it does not imply that the object is safe to modify because accesses to the managed object by former shared owners may not have completed, and because new shared owners may be introduced concurrently, such as bystd::weak_ptr::lock. Only whenuse_count returns 0 is the count accurate.

      [edit]Example

      Run this code
      #include <iostream>#include <memory> void fun(std::shared_ptr<int> sp){std::cout<<"in fun(): sp.use_count() == "<< sp.use_count()<<" (object @ "<< sp<<")\n";} int main(){auto sp1=std::make_shared<int>(5);std::cout<<"in main(): sp1.use_count() == "<< sp1.use_count()<<" (object @ "<< sp1<<")\n";     fun(sp1);}

      Possible output:

      in main(): sp1.use_count() == 1 (object @ 0x20eec30)in fun(): sp.use_count() == 2 (object @ 0x20eec30)

      [edit]See also

      (until C++20)
      checks whether the managed object is managed only by the currentshared_ptr object
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/shared_ptr/use_count&oldid=174896"

      [8]ページ先頭

      ©2009-2025 Movatter.jp