簡體   English   中英

如何使用Deleter調用unique_ptr構造函數?

[英]How to call unique_ptr constructor with deleter?

我嘗試初始化我的std::unique_ptr但是編譯失敗:

error: no matching function for call to ‘std::unique_ptr<int, void (*)(int*)>::unique_ptr(int*, void (&)(void*) throw ())’

m_head((int*)malloc(m_capacity * sizeof(int)), std::free) {
                                                        ^

這是我的代碼:

class deque {
    const int INC_ARRAY = 2;

    int m_front, m_back;
    int m_capacity;
    std::unique_ptr<int, void (*)(int *)> m_head;

public:
    const int EMPTY_DEQUE = -1;

    /**
     * @brief Constructor
     */
    deque()
        : m_front{INC_ARRAY - 1}, m_back{INC_ARRAY},
        m_capacity{2 * INC_ARRAY},
        m_head{(int*)malloc(m_capacity * sizeof(int)), std::free} {
    }
};

我需要使用malloc ,而不是new 如何正確初始化?

PS我只在學習C ++

std::free的簽名為void free(void*) 它不需要int* 更改您的刪除器類型。

std::unique_ptr<int, void(*)(void*)> m_head;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM