On the boost.org website, I saw an example of preventing delete px.get() for a shared_ptr (http://www.boost.org/doc/libs/1_51_0/libs/smart_ptr/sp_techniques.html#preventing_delete).
This is a good technique and I would like to apply this using std::unique_ptr in C++11, and after tooling around for a bit, I can’t quite get their example working with std::unique_ptr.
Is it possible to prevent delete px.get() from being called on a std::unique_ptr?
Here is the code from the boost.org website showing how to prevent delete px.get from being called:
class X
{
private:
~X();
class deleter;
friend class deleter;
class deleter
{
public:
void operator()(X * p) { delete p; }
};
public:
static shared_ptr<X> create()
{
shared_ptr<X> px(new X, X::deleter());
return px;
}
};
The idea remains the same for
unique_ptralso, except for the fact that the type of the deleter is part of theunique_ptr‘s type.VS2010 does not implement implicit conversion of capture-less lambda to a function pointer, so the first
create_uniquewill not work on it.