std::unique_ptr::get_deleter

来自cppreference.com
< cpp‎ | memory‎ | unique ptr
 
 
 
动态内存管理
 
 
      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...