std::unique_ptr::release - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::unique_ptr::release

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

pointer release(); |  | (since C++11)

Releases the ownership of the managed object if any. get() returns nullptr after the call.

### [edit] Parameters

(none)

### [edit] Return value

Pointer to the managed object or nullptr if there was no managed object, i.e. the value which would be returned by get() before the call.

### [edit] Exceptions

`noexcept` specification:
noexcept

### [edit] Example

Run this code

    #include <memory>
    #include <iostream>

    struct Foo {
        Foo() { std::cout << "Foo\n"; }
        ~Foo() { std::cout << "~Foo\n"; }
    };

    int main()
    {
        std::cout << "Creating new Foo...\n";
        std::unique_ptr<Foo> up(new Foo());

        std::cout << "About to release Foo...\n";
        Foo* fp = up.release();

        if (up.get() == nullptr)
            std::cout << "Foo is no longer owned by unique_ptr...\n";

        delete fp;
    }

Output:

    Creating new Foo...
    Foo
    About to release Foo...
    Foo is no longer owned by unique_ptr...
    ~Foo

### [edit] See also

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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/memory/unique_ptr/release&oldid=64228"
Category:

- unconditionally noexcept