Boost scoped_ptr / scoped_array with custom deletion

I do not see how to get scoped_ptr or scoped_array to use a custom div. Perhaps there is another implementation that allows controlled deletion like shared_ptr ?

Btw, why does shared_ptr allow a custom debugger, but scoped_ptr doesn't? Just curious.

+7
source share
4 answers

I don't see how to get scoped_ptr or scoped_array to use custom deleter

You can not.

Perhaps there is another implementation that allows you to control deletion like shared_ptr ?

If your compiler supports rvalue references and the standard library implementation implements std::unique_ptr , you can use this.

Otherwise, the implementation of boost::scoped_ptr very simple. 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 divider (either static using a template parameter, or dynamic via a function or functor provided at runtime).

+5
source

scoped_ptr does not allow a custom debugger. The main reason I can assume is that its size will not be equal to sizeof(T*) if it saves boost::function<> as shared_ptr .

I think the most portable options are to use shared_ptr or write your own scoped_ptr that will support deleted files.

+3
source

You can overload the boost :: checked_delete function, for example

 namespace boost { template<> void checked_delete (Foo* x) { .... } } // namespace boost 

After overloading, scoped_ptr will call checked_delete, not delete.

+3
source

Another implementation of copied pointer and copied array is found in Qt

http://doc.qt.io/qt-5/qscopedpointer.html

It allows you to execute a custom debugger.

+1
source

All Articles