Namespaces
Variants

std::shared_ptr<T>:: operator bool

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)
explicit operator bool ( ) const noexcept ;

* this 가 널이 아닌 포인터를 저장하고 있는지 확인합니다. 즉, get ( ) ! = nullptr 인지 여부를 확인합니다.

목차

매개변수

(없음)

반환값

true 만약 * this 가 포인터를 저장하고 있는 경우, false 그렇지 않은 경우.

참고 사항

빈 shared_ptr(즉 use_count ( ) == 0 인 경우)는 별칭 생성자를 사용하여 생성된 경우와 같이 get() 을 통해 접근 가능한 널이 아닌 포인터를 저장할 수 있습니다.

예제

#include <iostream>
#include <memory>
void report(std::shared_ptr<int> ptr) 
{
    if (ptr)
        std::cout << "*ptr=" << *ptr << "\n";
    else
        std::cout << "ptr is not a valid pointer.\n";
}
int main()
{
    std::shared_ptr<int> ptr;
    report(ptr);
    ptr = std::make_shared<int>(7);
    report(ptr);
}

출력:

ptr is not a valid pointer.
*ptr=7

참고 항목

저장된 포인터를 반환합니다
(public member function)