std::unique_ptr::swap(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

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 β€” another unique_ptr object 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