std::unique...~unique_ptr(3)
π
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
- 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