Namespaces
Variants
Views
Actions

std::shared_ptr::operator bool

From cppreference.com
< cpp‎ | memory‎ | shared ptr
Revision as of 23:13, 31 May 2013 by P12bot (Talk | contribs)

 
 
 
 
 
explicit operator bool() const;

Checks if *this manages an object, i.e. whether get() != 0.

Contents

Parameters

(none)

Return value

true if *this manages an object, false otherwise.

Exceptions

noexcept specification:  
noexcept
  

Example

#include <iostream>
#include <memory>
 
typedef std::shared_ptr<int> IntPtr;
 
void report(IntPtr ptr) 
{
    if (ptr) {
        std::cout << "*ptr=" << *ptr << "\n";
    } else {
        std::cout << "ptr is not a valid pointer.\n";
    }
}
 
int main()
{
    IntPtr ptr;
    report(ptr);
 
    ptr = IntPtr(new int(7));
    report(ptr);
}

Output:

ptr is not a valid pointer.
*ptr=7

See also

returns a pointer to the managed object
(public member function) [edit]