名稱空間
變體
操作

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

來自 cppreference.com
 
 
記憶體管理庫
(僅作說明*)
未初始化記憶體演算法
(C++17)
(C++17)
(C++17)
受約束的未初始化
記憶體演算法
C 庫

分配器
記憶體資源
垃圾回收支援
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化儲存
(直到 C++20*)
(直到 C++20*)
顯式生命週期管理
 
 
Deleter& get_deleter() noexcept;
(C++11 起)
(C++23 起為 constexpr)
const Deleter& get_deleter() const noexcept;
(C++11 起)
(C++23 起為 constexpr)

返回將用於銷燬被管理物件的刪除器物件。

目錄

[編輯] 引數

(無)

[編輯] 返回值

儲存的刪除器物件。

[編輯] 示例

#include <iostream>
#include <memory>
 
struct Foo
{
    Foo() { std::cout << "Foo() 0x" << std::hex << (void*)this << '\n'; }
    ~Foo() { std::cout << "~Foo() 0x" << std::hex << (void*)this << '\n'; }
};
 
struct D
{
    int number;
 
    void bar()
    {
        std::cout << "call D::bar(), my number is: " << std::dec << number << '\n';
    }
 
    void operator()(Foo* p) const
    {
        std::cout << "call deleter for Foo object 0x" << std::hex << (void*)p << '\n';
        delete p;
    }
};
 
int main()
{
    std::cout << "main start\n";
 
    std::unique_ptr<Foo, D> up1(new Foo(), D(42));
    D& del1 = up1.get_deleter();
    del1.bar();
 
    std::unique_ptr<Foo, D> up2(new Foo(), D(43));
    D& del2 = up2.get_deleter();
    auto* released = up2.release();
    del2(released);
 
    std::cout << "main end\n";
}

輸出

main start
Foo() 0x0x90cc30
call D::bar(), my number is: 42
Foo() 0x0x90cc50
call deleter for Foo object 0x0x90cc50
~Foo() 0x0x90cc50
main end
call deleter for Foo object 0x0x90cc30
~Foo() 0x0x90cc30

[編輯] 參閱

如果擁有,則返回指定型別的刪除器
(函式模板) [編輯]