Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 640
pic11 Avatar asked Apr 15 '11 19:04

pic11


2 Answers

I don't see how to get scoped_ptr or scoped_array to 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).

like image 97
James McNellis Avatar answered Sep 19 '22 13:09

James McNellis


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.

like image 30
Kirill V. Lyadvinsky Avatar answered Sep 16 '22 13:09

Kirill V. Lyadvinsky