Namespaces
Variants

std::shared_ptr<T>:: unique

From cppreference.net
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* )
( 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)
bool unique ( ) const noexcept ;
(C++17 起已弃用)
(C++20 起移除)

检查 * this 是否为当前管理对象的唯一 shared_ptr 实例,即是否满足 use_count ( ) == 1

目录

参数

(无)

返回值

* this 是唯一管理当前对象的 shared_ptr 实例时返回 true ,否则返回 false

注释

此函数在C++17中已弃用并在C++20中移除,因为 use_count ( ) == 1 在多线程环境中无意义(参见 use_count 中的 说明 )。

示例

#include <iostream> 
#include <memory> 
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'; 
}

输出:

sp1.unique() == true
sp1.unique() == false

参见

返回引用同一被管理对象的 shared_ptr 对象数量
(公开成员函数)