当我创建一个unique_ptr
with 时deleter
,它可以工作:
std::unique_ptr<Animal<Cat>, void(*)(Animal<Cat>*)> ptr(new Animal<Cat>, [](Animal<Cat> *ls) {
delete ls;
});
但是,这段代码抛出错误:
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;
});
错误:
/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;
^
这是我的编译器版本:
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
Animal 和 Cat 类是微不足道的。这是整个代码。