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 인지 여부를 확인합니다.

목차

매개변수

(없음)

반환값

true 만약 * this 가 현재 객체를 관리하는 유일한 shared_ptr 인스턴스라면, false 그렇지 않으면.

참고 사항

이 함수는 C++17에서 사용 중단(deprecated)되었고 C++20에서 제거되었습니다. 왜냐하면 use_count ( ) == 1 가 다중 스레드 환경에서 의미가 없기 때문입니다 ( Notes 에서 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 객체의 수를 반환합니다
(public member function)