Reputation: 3679
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.
Upvotes: 6
Views: 5944
Reputation: 3679
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;
}
Upvotes: 11
Reputation: 42554
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));
Upvotes: 11