繁体   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