# std::shared_ptr::operator bool

From cppreference.com
< cpp‎ | memory‎ | shared ptr

explicit operator bool() const; |  |

Checks if *this stores a non-null pointer, i.e. whether get() != nullptr.

### Parameters

(none)

### Return value

true if *this stores a pointer, false otherwise.

### Exceptions

`noexcept` specification:
noexcept

### 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.

### Example

Run this code

    #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

get | returns the stored pointer (public member function)

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/memory/shared_ptr/operator_bool&oldid=91774"
Category:

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template