首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::shared_ptr::unique

bool unique() const noexcept;

?

(deprecated)

检查*this是唯一shared_ptr实例管理当前对象,即use_count() == 1...

参数

%280%29

返回值

true如果*this是唯一shared_ptr实例管理当前对象,false否则。

例外

noexcept规格:

noexcept

注记

此函数在C++17中不再受欢迎,因为use_count只是多线程环境中的近似。

二次

代码语言:javascript
复制
#include <memory> 
#include <iostream> 
 
int main() 
{ 
    auto sp1 = std::make_shared<int>(5);
    std::cout << std::boolalpha;
    std::cout << "sp1.unique() == " << sp1.unique() << '\n'; 
 
    std::shared_ptr<int> sp2 = sp1; 
    std::cout << "sp1.unique() == " << sp1.unique() << '\n'; 
}

二次

产出:

二次

代码语言:javascript
复制
sp1.unique() == true
sp1.unique() == false

二次

另见

use_count

returns the number of shared_ptr objects referring to the same managed object (public member function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com