std::unique_ptr::swap(3)
std::unique_ptr::swap π β¨
Manual name check: C++ Standard Libary, section (3) β yes bestie, that spelling is what the page says, and we're keeping it. This is the std::unique_ptr::swap page.
β¨ NAME β¨
std::unique_ptr::swap β std::unique_ptr::swap. That's literally the name section doing name things. Iconic.
πΈ Synopsis πΈ
void swap( unique_ptr& other ) noexcept; (since C++11)
Swaps the managed objects and associated deleters of *this and another unique_ptr object other. Like, both the managed object and the associated deleter get swapped between the two unique_ptrs. The signature is marked noexcept, and the (since C++11) note is right there.
π Parameters π
otherβ anotherunique_ptrobject to swap the managed object and the deleter with.
π Return value π
(none) β no return value, bestie. It's giving void.
π¦ Example π¦
Run this code, bestie:
// 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...
Sign-off tea β: source http://cppreference.com, version/date 2024.06.10, page tag std::unique_ptr::swap(3). Bye bestie, go swap those managed objects and deleters! ππ«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms