Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::weak_ptr<T>::expired

      From cppreference.com
      <cpp‎ |memory‎ |weak 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)
       
       
      bool expired()constnoexcept;
      (since C++11)

      Equivalent touse_count()==0. The destructor for the managed object may not yet have been called, but this object's destruction is imminent (or may have already happened).

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      true if the managed object has already been deleted,false otherwise.

      [edit]Notes

      If the managed object is shared among threads, it is only meaningful whenexpired() returns true.

      [edit]Example

      Demonstrates howexpired is used to check validity of the pointer.

      Run this code
      #include <iostream>#include <memory> std::weak_ptr<int> gw; void f(){if(!gw.expired())std::cout<<"gw is valid\n";elsestd::cout<<"gw is expired\n";} int main(){{auto sp=std::make_shared<int>(42);gw= sp; f();}     f();}

      Output:

      gw is validgw is expired

      [edit]See also

      creates ashared_ptr that manages the referenced object
      (public member function)[edit]
      returns the number ofshared_ptr objects that manage the object
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/weak_ptr/expired&oldid=174841"

      [8]ページ先頭

      ©2009-2025 Movatter.jp