public member function
<memory>

std::unique_ptr::release

pointer release() noexcept;
Release pointer
Releases ownership of its stored pointer, by returning its value and replacing it with a null pointer.

This call does not destroy the managed object, but the unique_ptr object is released from the responsibility of deleting the object. Some other entity must take responsibility for deleting the object at some point.

To force the destruction of the object pointed, either use member function reset or perform an assignment operation on it.

Parameters

none

Return value

A pointer to the object managed by unique_ptr before the call.
pointer is a member type, defined as the pointer type that points to the type of object managed.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// unique_ptr::release example
#include <iostream>
#include <memory>

int main () {
  std::unique_ptr<int> auto_pointer (new int);
  int * manual_pointer;

  *auto_pointer=10;

  manual_pointer = auto_pointer.release();
  // (auto_pointer is now empty)

  std::cout << "manual_pointer points to " << *manual_pointer << '\n';

  delete manual_pointer;

  return 0;
}


Output:

manual_pointer points to 10

See also