Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::weak_ptr

      From cppreference.com
      <cpp‎ |memory
       
       
      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)
       
       
      Defined in header<memory>
      template<class T>class weak_ptr;
      (since C++11)

      std::weak_ptr is a smart pointer that holds a non-owning ("weak") reference to an object that is managed bystd::shared_ptr. It must be converted tostd::shared_ptr in order to access the referenced object.

      std::weak_ptr models temporary ownership: when an object needs to be accessed only if it exists, and it may be deleted at any time by someone else,std::weak_ptr is used to track the object, and it is converted tostd::shared_ptr to acquire temporary ownership. If the originalstd::shared_ptr is destroyed at this time, the object's lifetime is extended until the temporarystd::shared_ptr is destroyed as well.

      Another use forstd::weak_ptr is to break reference cycles formed by objects managed bystd::shared_ptr. If such cycle is orphaned (i.e., there are no outside shared pointers into the cycle), theshared_ptr reference counts cannot reach zero and the memory is leaked. To prevent this, one of the pointers in the cyclecan be made weak.

      Contents

      [edit]Member types

      Member type Definition
      element_type

      T

      (until C++17)

      std::remove_extent_t<T>

      (since C++17)

      [edit]Member functions

      creates a newweak_ptr
      (public member function)[edit]
      destroys aweak_ptr
      (public member function)[edit]
      assigns theweak_ptr
      (public member function)[edit]
      Modifiers
      releases the ownership of the managed object
      (public member function)[edit]
      swaps the managed objects
      (public member function)[edit]
      Observers
      returns the number ofshared_ptr objects that manage the object
      (public member function)[edit]
      checks whether the referenced object was already deleted
      (public member function)[edit]
      creates ashared_ptr that manages the referenced object
      (public member function)[edit]
      provides owner-based ordering of weak pointers
      (public member function)[edit]
      (C++26)
      provides owner-based hashing of weak pointers
      (public member function)[edit]
      provides owner-based equal comparison of weak pointers
      (public member function)[edit]

      [edit]Non-member functions

      specializes thestd::swap algorithm
      (function template)[edit]

      [edit]Helper classes

      atomic weak pointer
      (class template specialization)[edit]

      [edit]Deduction guides(since C++17)

      [edit]Notes

      Likestd::shared_ptr, a typical implementation ofweak_ptr stores two pointers:

      • a pointer to the control block; and
      • the stored pointer of theshared_ptr it was constructed from.

      A separate stored pointer is necessary to ensure that converting ashared_ptr toweak_ptr and then back works correctly, even for aliasedshared_ptrs. It is not possible to access the stored pointer in aweak_ptr without locking it into ashared_ptr.

      Feature-test macroValueStdFeature
      __cpp_lib_smart_ptr_owner_equality202306L(C++26)Enabling the use ofstd::weak_ptr as keys inunordered associative containers

      [edit]Example

      Demonstrates how lock is used to ensure validity of the pointer.

      Run this code
      #include <iostream>#include <memory> std::weak_ptr<int> gw; void observe(){std::cout<<"gw.use_count() == "<< gw.use_count()<<"; ";// we have to make a copy of shared pointer before usage:if(std::shared_ptr<int> spt= gw.lock())std::cout<<"*spt == "<<*spt<<'\n';elsestd::cout<<"gw is expired\n";} int main(){{auto sp=std::make_shared<int>(42);        gw= sp;         observe();}     observe();}

      Output:

      gw.use_count() == 1; *spt == 42gw.use_count() == 0; gw is expired

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3001C++17element_type was not updated for array supportupdated

      [edit]See also

      (C++11)
      smart pointer with unique object ownership semantics
      (class template)[edit]
      (C++11)
      smart pointer with shared object ownership semantics
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/weak_ptr&oldid=173517"

      [8]ページ先頭

      ©2009-2025 Movatter.jp