std::shared_ptr::use_count

long use_count() const noexcept;

返回管理当前对象的不同 shared_ptr 实例(包含 this )数量。若无管理对象,则返回 0

多线程环境下, use_count 返回的值是近似的(典型实现使用 memory_order_relaxed 加载)

目录

参数

(无)

返回值

管理当前对象的 shared_ptr 实例数量,或若无被管理对象则为 0

注意

常用使用包括

示例

#include <memory> 
#include <iostream> 
 
void fun(std::shared_ptr<int> sp)
{
    std::cout << "fun: sp.use_count() == " << sp.use_count() << '\n'; 
}
 
int main() 
{ 
    auto sp1 = std::make_shared<int>(5);
    std::cout << "sp1.use_count() == " << sp1.use_count() << '\n'; 
 
    fun(sp1);
}

输出:

sp1.use_count() == 1
fun: sp.use_count() == 2

参阅

(弃用)
检查所管理对象是否仅由当前 shared_ptr 的实例管理
(公开成员函数)

版本历史

  • (当前 | 先前 2017年9月3日 (日) 21:44Fruderica讨论 | 贡献. . (1,667字节) (+392). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:57P12bot讨论 | 贡献 . . (1,275字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:07P12bot讨论 | 贡献 . . (1,275字节) (-25). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 14:36P12bot讨论 | 贡献 . . (1,300字节) (+313). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:41P12讨论 | 贡献 . . (987字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (987字节) (-32). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:55P12讨论 | 贡献 . . (1,019字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,019字节) (+1,019). . (Translated from the English version using Google Translate)