Lack of release() in scoped_ptr and scoped_array

I finally decided to switch to boost from my own implementation of some helper classes, and I noticed that scoped_ptr and scoped_array in boost lack the release method(). I had to fall back to std::auto_ptr. However, this seems like such a basic and fundamental problem that I have started to fear that I am missing something obvious... The usage I got used to was something like this: { scoped_ptr<A> temp( new A() ); verify( temp.get() ); // might throw store_in_a_very_special_place( temp.get() ); // might throw; and I should keep ownership if thrown temp.release(); } I though this was good practice to make code exception safe. Sure I could do it with try-catch but that gets messy if I allocate multiple instances etc. So the question is: 1) is there something wrong with the concept of release()? 2) if release() is not evil, is there any other reason why is it not in boost::scoped_*? Regards, Milutin Jovanovic

Milutin Jovanovic wrote:
I finally decided to switch to boost from my own implementation of some helper classes, and I noticed that scoped_ptr and scoped_array in boost lack the release method(). I had to fall back to std::auto_ptr.
However, this seems like such a basic and fundamental problem that I have started to fear that I am missing something obvious... The usage I got used to was something like this:
{ scoped_ptr<A> temp( new A() ); verify( temp.get() ); // might throw store_in_a_very_special_place( temp.get() ); // might throw; and I should keep ownership if thrown temp.release(); }
I though this was good practice to make code exception safe. Sure I could do it with try-catch but that gets messy if I allocate multiple instances etc.
Try writing it as: { shared_ptr<A> temp( new A() ); verify( temp ); // might throw store_in_a_very_special_place( temp ); // might throw; and I should keep ownership if thrown } It's less code and it's safer. But if you must use scoped_ptr another alternative would be: { scoped_ptr<A> temp( new A() ); verify( *temp ); // might throw store_in_a_very_special_place( *temp ); // might throw; and I should keep ownership if thrown }
So the question is:
1) is there something wrong with the concept of release()? 2) if release() is not evil, is there any other reason why is it not in boost::scoped_*?
See the FAQ for this at the bottom of the scoped_ptr docs http://boost.org/libs/smart_ptr/scoped_ptr.htm. -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - grafikrobot/yahoo

On Aug 2, 2006, at 1:59 PM, Milutin Jovanovic wrote:
I finally decided to switch to boost from my own implementation of some helper classes, and I noticed that scoped_ptr and scoped_array in boost lack the release method(). I had to fall back to std::auto_ptr.
However, this seems like such a basic and fundamental problem that I have started to fear that I am missing something obvious... The usage I got used to was something like this:
{ scoped_ptr<A> temp( new A() ); verify( temp.get() ); // might throw store_in_a_very_special_place( temp.get() ); // might throw; and I should keep ownership if thrown temp.release(); }
I though this was good practice to make code exception safe. Sure I could do it with try-catch but that gets messy if I allocate multiple instances etc.
So the question is:
1) is there something wrong with the concept of release()?
There's nothing wrong with release().
2) if release() is not evil, is there any other reason why is it not in boost::scoped_*?
scoped_ptr wasn't designed for this use case. Just use std::auto_ptr. C++0X will have an improved version of it called std::unique_ptr (I hope). If you want, you can google around for some emulations of it. But I'd just use auto_ptr for this use case for now. -Howard
participants (3)
-
Howard Hinnant
-
Milutin Jovanovic
-
Rene Rivera