std::shared_ptr<T>::unique
提供: cppreference.com
<tbody>
</tbody>
bool unique() const noexcept; |
(C++17で非推奨) (C++20で削除) |
|
*this が現在のオブジェクトを管理している唯一の shared_ptr インスタンス、すなわち use_count() == 1 かどうかを調べます。
引数
(なし)
戻り値
*this が現在のオブジェクトを管理している唯一の shared_ptr インスタンスであれば true、そうでなければ false を返します。
ノート
マルチスレッド環境では use_count が概算でしかないため (use_count のノートを参照してください)、この関数は C++17 で非推奨になり、 C++20 で削除されました。
例
Run this code
#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';
}
出力:
sp1.unique() == true
sp1.unique() == false
関連項目
同じ管理対象オブジェクトを参照している shared_ptr オブジェクトの数を返します (パブリックメンバ関数) |