Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::weak_ptr<T>::weak_ptr

      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)
       
       
      constexpr weak_ptr()noexcept;
      (1)(since C++11)
      weak_ptr(const weak_ptr& r)noexcept;
      (2)(since C++11)
      template<class Y>
      weak_ptr(const weak_ptr<Y>& r)noexcept;
      (2)(since C++11)
      template<class Y>
      weak_ptr(conststd::shared_ptr<Y>& r)noexcept;
      (2)(since C++11)
      weak_ptr( weak_ptr&& r)noexcept;
      (3)(since C++11)
      template<class Y>
      weak_ptr( weak_ptr<Y>&& r)noexcept;
      (3)(since C++11)

      Constructs newweak_ptr that potentially shares an object withr.

      1) Default constructor. Constructs emptyweak_ptr.
      2) Constructs newweak_ptr which shares an object managed byr. Ifr manages no object,*this manages no object too. The templated overloads don't participate in the overload resolution unlessY* is implicitly convertible toT*, orY is the type "array ofNU" for some typeU and some numberN, andT is the type "array of unknown bound of (possibly cv-qualified)U"(since C++17).
      3) Move constructors. Moves a weak_ptr instance fromr into*this. After this,r is empty andr.use_count()==0. The templated overload doesn't participate in the overload resolution unlessY* is implicitly convertible toT*.

      Contents

      [edit]Parameters

      r - astd::shared_ptr orstd::weak_ptr that will be viewed by thisstd::weak_ptr

      [edit]Notes

      Because the default constructor isconstexpr, staticstd::weak_ptrs are initialized as part ofstatic non-local initialization, before any dynamic non-local initialization begins. This makes it safe to use astd::weak_ptr in a constructor of any static object.

      [edit]Example

      Run this code
      #include <iostream>#include <memory> struct Foo{}; int main(){std::weak_ptr<Foo> w_ptr; {auto ptr=std::make_shared<Foo>();        w_ptr= ptr;std::cout<<"w_ptr.use_count() inside scope: "<< w_ptr.use_count()<<'\n';} std::cout<<"w_ptr.use_count() out of scope: "<< w_ptr.use_count()<<'\n';std::cout<<"w_ptr.expired() out of scope: "<<std::boolalpha<< w_ptr.expired()<<'\n';}

      Output:

      w_ptr.use_count() inside scope: 1w_ptr.use_count() out of scope: 0w_ptr.expired() out of scope: true

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2315C++11move semantic was not enabled forweak_ptrenabled

      [edit]See also

      assigns theweak_ptr
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/weak_ptr/weak_ptr&oldid=153997"

      [8]ページ先頭

      ©2009-2025 Movatter.jp