std::shared_ptr::operator bool - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::shared_ptr::operator bool

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

explicit operator bool() const; |  |

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

### [edit] Parameters

(none)

### [edit] Return value

true if *this manages an object, false otherwise.

### [edit] Exceptions

`noexcept` specification:
noexcept

### [edit] 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

### [edit] See also

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

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

- unconditionally noexcept