std::unique_ptr<T,Deleter>::get_deleter

来自cppreference.com
< cpp‎ | memory‎ | unique ptr
 
 
 
动态内存管理
未初始化存储
(C++17)
垃圾收集支持
杂项
(C++20)
(C++11)
(C++11)
C 库
低层内存管理
 
 
      Deleter& get_deleter() noexcept;
(C++11 起)
const Deleter& get_deleter() const noexcept;
(C++11 起)

返回会用于析构被管理对象的删除器对象。

参数

(无)

返回值

存储的删除器对象。

示例

#include <iostream>
#include <memory>
 
struct Foo
{
    Foo() { std::cout << "Foo...\n"; }
    ~Foo() { std::cout << "~Foo...\n"; }
};
 
struct D
{
    void bar() { std::cout << "Call deleter D::bar()...\n"; }
    void operator()(Foo* p) const
    {
        std::cout << "Call delete for Foo object...\n";
        delete p;
    }
};
 
int main()
{
    std::unique_ptr<Foo, D> up(new Foo(), D());
    D& del = up.get_deleter();
    del.bar();
}

输出:

Foo...
Call deleter D::bar()...
Call delete for Foo object...
~Foo...