Boost logo

Boost :

From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2004-01-21 19:07:55

Howard Hinnant wrote:

> On Jan 21, 2004, at 10:01 AM, Daniel Wallin wrote:
>> Please present _real_ common use cases where this is needed,
>> and where move_ptr can't be replaced with shared_ptr.
> Ok, this isn't common, but I'm going to share a little bit of the
> Metrowerks std::tr1::shared_ptr implementation as it demonstrates one
> aspect of move_ptr<T, D> that I find very attractive:
> template<class T>
> template<class Y, class D>
> shared_ptr<T>::shared_ptr(Y* p, D d)
> : ptr_(p)
> {
> Metrowerks::move_ptr<Y, D&> hold(p, d);
> s_ = new detail::shared_ptr_deleter<Y, D>(p, d);
> hold.release();
> ...
> }
> This is the shared_ptr constructor that takes a pointer and a deleter.
> It accepts ownership of the pointer no matter what ... even if the
> construction fails with a bad_alloc. In order to accomplish this
> exception safety I create a temporary move_ptr<T, D&> to establish
> ownership in a no-throw manner. Note the "D&"!

Yes it looks very neat, but it doesn't look like ownership transfer. At
least not in terms of move_ptr. It looks like a scopeguard.

    scopeguard hold = bind(ref(d), p);

> template <class T, class D>
> foo::bar(T* p)
> {
> // claim ownership of p
> move_ptr<T, D&> hold(p, d_);
> ...
> // transfer ownership of p to foo
> hold.release();
> }

Again, I don't see the ownership transfer. I only see the exception
safety utility.

I do get the point though, and I agree that it's nice to be able to
store to deleter by reference.

Daniel Wallin

Boost list run by bdawes at, gregod at, cpdaniel at, john at