|
Boost Users : |
From: Howard Hinnant (hinnant_at_[hidden])
Date: 2006-08-02 18:30:40
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
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net