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 ;

Comprueba si * this almacena un puntero no nulo, es decir, si get ( ) ! = nullptr .

Contenidos

Parámetros

(ninguno)

Valor de retorno

true si * this almacena un puntero, false en caso contrario.

Notas

Un shared_ptr vacío (donde use_count ( ) == 0 ) puede almacenar un puntero no nulo accesible mediante get() , por ejemplo, si fue creado usando el constructor de aliasing.

Ejemplo

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

Salida:

ptr is not a valid pointer.
*ptr=7

Véase también

devuelve el puntero almacenado
(función miembro pública)