unique_ptr is not getting init with default deleter

只愿长相守 提交于 2019-12-23 09:34:09

问题


When I create an unique_ptr with deleter, it works:

std::unique_ptr<Animal<Cat>, void(*)(Animal<Cat>*)> ptr(new Animal<Cat>, [](Animal<Cat> *ls) {
    delete ls;
});

But, this code is throwing error:

std::unique_ptr<Animal<Cat>, void(*)(Animal<Cat>*)> ptr;
ptr = std::unique_ptr<Animal<Cat>, void(*)(Animal<Cat>*)>(new Animal<Cat>, [](Animal<Cat> *ls) {
    delete ls;
});

The error:

/usr/bin/../lib/c++/v1/memory:2561:13: error: static_assert failed "unique_ptr constructed with null function pointer deleter"
                static_assert(!is_pointer<deleter_type>::value,
                ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in instantiation of member function 'std::__1::unique_ptr<Animal<Cat>, void (*)(Animal<Cat> *)>::unique_ptr' requested here
            std::unique_ptr<Animal<Cat>, void(*)(Animal<Cat>*)> ptr;
                                                                ^

This is my compiler version:

Apple clang version 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.4.0
Thread model: posix

The Animal and Cat classes are trivial. This is the entire code.


回答1:


It as apparently not possible to default-construct a unique_ptr with a pointer type for a deleter. The unique_ptr constructors are detailed in §20.7.1.2.1 [unique.ptr.single.ctor] which says, among other things:

constexpr unique_ptr() noexcept;

4 Remarks: If this constructor is instantiated with a pointer type or reference type for the template argument D [the deleter type], the program is ill-formed.

Something similar to what you want that is standard conforming:

unique_ptr<int, void(*)(int*)> ptr{nullptr,[](int*p){delete p;}};
// ...
ptr.reset(new int(42));



回答2:


After reading Casey's answer above, I changed my code to use good old Functors instead of lambda to make it work.

This is my solution:

struct Deleter {
    void operator()(Animal<Cat>* ls) {
        delete ls;
    }
};

int main() {
    std::unique_ptr<Animal<Cat>, Deleter> ptr;
    ptr = std::unique_ptr<Animal<Cat>, Deleter>(new Animal<Cat>);
    return 0;
}


来源:https://stackoverflow.com/questions/17906325/unique-ptr-is-not-getting-init-with-default-deleter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!