std::shared_ptr<T>::operator bool
From cppreference.com
 
 
 < cpp | memory | shared ptr 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
Memory management library 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
(exposition only*)
(C++11)
(C++23)
(C++11)
(C++17)
(C++11)
(C++11)
(C++20)
(C++20)
(C++17)
(C++11)
(C++17)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
 (C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
 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)
(C++11)
(C++11)
(C++17)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
(until C++17*)
(C++11)
(C++17)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
std::shared_ptr 
 
 
 
 
 
 
 
 
 Member functions
 Modifiers
 Observers
(C++17)
(until C++20*)
shared_ptr::operator bool
(C++26)
(C++26)
 Non-member functions
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
functions (until C++26*)
 Helper classes
(C++20)
 Deduction guides (C++17)
explicit operator bool() const noexcept;
 
 
Checks if *this stores a non-null pointer, i.e. whether get() != nullptr.
Contents
[edit] Parameters
(none)
[edit] Return value
true if *this stores a pointer, false otherwise.
[edit] Notes
An empty shared_ptr (where use_count() == 0) may store a non-null pointer accessible by get() , e.g. if it were created using the aliasing constructor.
[edit] Example
Run this code
#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); }
Output:
ptr is not a valid pointer. *ptr=7