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

FreeBSD Manual Pages

  
 
  

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

NAME
       std::unique_ptr::swap - std::unique_ptr::swap

Synopsis
	  void swap( unique_ptr& other ) noexcept;  (since C++11)

	  Swaps	 the  managed objects and associated deleters of *this and an-
       other unique_ptr
	  object other.

Parameters
	  other	- another unique_ptr object to swap the	managed	object and the
       deleter with

Return value
	  (none)

Example
       // Run this code

	#include <iostream>
	#include <memory>

	struct Foo {
	    Foo(int _val) : val(_val) {	std::cout << "Foo...\n"; }
	    ~Foo() { std::cout << "~Foo...\n"; }
	    int	val;
	};

	int main()
	{
	    std::unique_ptr<Foo> up1(new Foo(1));
	    std::unique_ptr<Foo> up2(new Foo(2));

	    up1.swap(up2);

	    std::cout << "up1->val:" <<	up1->val << '\n';
	    std::cout << "up2->val:" <<	up2->val << '\n';
	}

Output:
	Foo...
	Foo...
	up1->val:2
	up2->val:1
	~Foo...
	~Foo...

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

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

home | help