Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::unique_ptr::release(3)   C++ Standard Libary  std::unique_ptr::release(3)

NAME
       std::unique_ptr::release	- std::unique_ptr::release

Synopsis
	  pointer release() noexcept;  (since C++11)
				       (constexpr since	C++23)

	  Releases the ownership of the	managed	object,	if any.

	  get()	returns	nullptr	after the call.

	  The caller is	responsible for	deleting the object.

Parameters
	  (none)

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

Example
       // Run this code

	#include <memory>
	#include <iostream>
	#include <cassert>

	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();

	    assert (up.get() ==	nullptr);
	    assert (up == 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
	  get	returns	a pointer to the managed object
		(public	member function)
	  reset	replaces the managed object
		(public	member function)

http://cppreference.com		  2022.07.31	   std::unique_ptr::release(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::unique_ptr::release&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help