C++ 参考手册

位置:首页 > C++ 参考手册 >动态内存管理 >std::unique_ptr > std::unique_ptr<T,Deleter>::get_deleter

      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...