[英]How do I initialize a std::unique_ptr with a deleter in a constructor?
[英]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.