Namespaces
Variants
Views
Actions

std::unique_ptr::release

From cppreference.com
< cpp‎ | memory‎ | unique ptr
Revision as of 15:05, 2 November 2012 by P12bot (Talk | contribs)

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

Releases the ownership of the managed object if any. Template:rlpf returns nullptr after the call.

Contents

Parameters

(none)

Return value

Pointer to the managed object or nullptr if there was no managed object, i.e. the value which would be returned by Template:rlpf before the call.

Exceptions

noexcept specification:  
noexcept
  

Example

#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

See also

Template:cpp/memory/unique ptr/dcl list getTemplate:cpp/memory/unique ptr/dcl list reset