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 ;

Vérifie si * this contient un pointeur non nul, c'est-à-dire si get ( ) ! = nullptr .

Table des matières

Paramètres

(aucun)

Valeur de retour

true si * this stocke un pointeur, false sinon.

Notes

Un shared_ptr vide (où use_count ( ) == 0 ) peut stocker un pointeur non-nul accessible via get() , par exemple s'il a été créé en utilisant le constructeur d'aliasing.

Exemple

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

Sortie :

ptr is not a valid pointer.
*ptr=7

Voir aussi

retourne le pointeur stocké
(fonction membre publique)