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 ;

Prüft, ob * this einen nicht-null Zeiger speichert, d.h. ob get ( ) ! = nullptr .

Inhaltsverzeichnis

Parameter

(keine)

Rückgabewert

true wenn * this einen Zeiger speichert, false andernfalls.

Hinweise

Ein leerer shared_ptr (bei dem use_count ( ) == 0 ) kann einen nicht-null Zeiger speichern, der über get() zugänglich ist, z.B. wenn er mit dem Aliasing-Konstruktor erstellt wurde.

Beispiel

#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);
}

Ausgabe:

ptr is not a valid pointer.
*ptr=7

Siehe auch

gibt den gespeicherten Zeiger zurück
(öffentliche Elementfunktion)