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 が非nullポインタを格納しているか、すなわち get ( ) ! = nullptr であるかをチェックします。

目次

パラメータ

(なし)

戻り値

true * this がポインタを保持している場合、 false がそれ以外の場合。

注記

空のshared_ptr( use_count ( ) == 0 )は、 get() でアクセス可能な非nullポインタを保持する場合があります。例えば、エイリアシングコンストラクタを使用して作成された場合などです。

#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

関連項目

格納されたポインタを返す
(公開メンバ関数)