Skip to content

Latest commit

 

History

History
58 lines (44 loc) · 1.26 KB

use_count.md

File metadata and controls

58 lines (44 loc) · 1.26 KB

use_count

  • memory[meta header]
  • std[meta namespace]
  • weak_ptr[meta class]
  • function[meta id-type]
  • cpp11[meta cpp]
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;
}
  • use_count()[color ff0000]

出力

1
0

バージョン

言語

  • C++11

処理系