During C++03 we did not have unique_ptr
, so I had used the tr1::shared_ptr
instead.
Now, in C++11, in such cases, I am replacing calls like this:
tr1::shared_ptr<X> o(new X);
with
std::unique_ptr<X> o(new X);
Unfortunately, for some reason, I cannot replace cases containg a deleter, where the deleter is a function:
void special_delete(Y *p) { ... }
tr1::shared_ptr<Y> o(new Y(), special_delete);
std::unique_ptr<Y> o(new Y(), special_delete); // does not compile
std::unique_ptr<Y, std::function<void(Y*)> > o(new Y(), special_delete); // this compiles
Why does this happen? Is there a homogeneous way I can replace all shared_ptr constructors with unique_ptr constructors ?
I have created this, but I am not really happy about it..
template <class Y>
using unique_ptr_with_deleter = std::unique_ptr<Y, std::function<void(Y*)> >;
unique_ptr_with_deleter<Y> o(new Y(), special_delete); // this compiles