Boost scoped_ptr / scoped_array with custom deleter
I don't see how to get scoped_ptr or scoped_array to use a custom deleter. Maybe there开发者_如何学Python is another implementation which allows controlled deletion similar to shared_ptr?
Btw, why does shared_ptr allow custom deleter but scoped_ptr doesn't? Just curious.
I don't see how to get
scoped_ptrorscoped_arrayto use custom deleter
You can't.
Maybe there is another implementation which allows controlled deletion similar to
shared_ptr?
If your compiler supports rvalue references and your Standard Library implementation implements std::unique_ptr, you can use that.
Otherwise, the boost::scoped_ptr implementation is very straightforward. The latest version is less than 100 lines of simple code. It would be quite simple to create your own derivative that has a custom deleter (either a static via a template parameter or dynamic via a function or functor provided at runtime).
scoped_ptr doesn't allow custom deleter. The main reason as I can suppose is that its' size will not be equal to sizeof(T*) if it would keep a boost::function<> as shared_ptr does.
I think the most portable options are to use shared_ptr or to write your own scoped_ptr that will support deleters.
You can overload boost::checked_delete function, e.g.
namespace boost {
template<>
void checked_delete (Foo* x)
{
....
}
} // namespace boost
After overloading, scoped_ptr will call checked_delete rather than delete.
Another implementation of scoped pointer and scoped array is found in Qt
http://doc.qt.io/qt-5/qscopedpointer.html
It allows for custom deleter.
加载中,请稍侯......
精彩评论