std::unique...~unique_ptr(3)

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

πŸ’… std::unique_ptr::~unique_ptr πŸ’…

okay so this page is std::unique...~unique_ptr(3) from the C++ Standard Libary manual, and it’s literally about the destructor. cleanup main character energy, no cap 🧹✨

✨ NAME ✨

std::unique_ptr::~unique_ptr β€” std::unique_ptr::~unique_ptr

bestie, that’s the whole name. slay. πŸ’…

🌸 Synopsis 🌸

~unique_ptr();  (since C++11)
                (constexpr since C++23)

okay so here’s the vibe check: if get() == nullptr, there are no effects. periodt. otherwise, the owned object is destroyed via get_deleter()(get()).

and this is a hard requirement, not a suggestion: get_deleter()(get()) must not throw exceptions. like, fr fr, don’t do it. 🫢

πŸ“ Notes πŸ“

here’s the tea: although std::unique_ptr<T> with the default deleter may be constructed with an incomplete type T, the type T must be complete at the point of code where the destructor is called. so like… construction said β€œwe can work with this,” but destruction is where the type has to be fully there. no skipping, bestie. 😭✨

🧚 Example 🧚

the following program demonstrates usage of a custom deleter. it’s giving β€œwatch me clean up with style” πŸ”₯

the original has a little // Run this code marker before the program, so here’s that and then the code:

#include <iostream>
#include <memory>

int main ()
{
    auto deleter = [](int* ptr)
    {
        std::cout << "[deleter called]\n";
        delete ptr;
    };

    std::unique_ptr<int, decltype(deleter)> uniq(new int, deleter);
    std::cout << (uniq ? "not empty\n" : "empty\n");
    uniq.reset();
    std::cout << (uniq ? "not empty\n" : "empty\n");
}

πŸ’– Output πŸ’–

not empty
[deleter called]
empty

this page came from http://cppreference.com, dated 2024.06.10, and its page name is std::unique...~unique_ptr(3) β€” mwah, bye bestie πŸ«ΆπŸ’œπŸŽ€

πŸ“¦ where this page comes from