• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <memory>

    std::weak_ptr::use_count

    long use_count() const noexcept;
    

    概要

    監視しているshared_ptrオブジェクトの所有者数を取得する。

    戻り値

    *thisshared_ptrオブジェクトを監視していない空の状態なら、0を返す。

    そうでなければ、shared_ptrオブジェクトの所有者数(shared_ptr::use_count())を返す。

    #include <iostream>
    #include <memory>
    
    int main()
    {
      std::weak_ptr<int> wp;
      {
        std::shared_ptr<int> sp(new int(3));
    
        // shared_ptrオブジェクトspを監視する
        wp = sp;
    
        std::cout << wp.use_count() << std::endl;
      }
    
      std::cout << wp.use_count() << std::endl;
    }
    

    出力

    1
    0
    

    バージョン

    言語

    • C++11

    処理系