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

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::unique_ptr::operator bool

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

explicit operator bool() const; |  | (since C++11)

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

### [edit] Parameters

(none)

### [edit] Return value

true if *this owns an object, false otherwise.

### [edit] Exceptions

`noexcept` specification:
noexcept

### [edit] Example

Run this code

    #include <iostream>
    #include <memory>

    int main()
    {
        std::unique_ptr<int> ptr(new int(42));

        if (ptr) std::cout << "before reset, ptr is: " << *ptr << '\n';
        ptr.reset();
        if (ptr) std::cout << "after reset, ptr is: " << *ptr << '\n';
    }

Output:

    before reset, ptr is: 42

### [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/unique_ptr/operator_bool&oldid=63348"
Category:

- unconditionally noexcept