Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-09-21 16:37:29


From: "David Abrahams" <dave_at_[hidden]>
> > > > The problem is that without swap(), it's not possible to implement a
> > > > copyable class with a scoped_ptr member. With swap(), it's trivial.
> > >
> > > Huh?
> > >
> > > struct body {};
> > > struct handle {
> > > scoped_ptr<body> impl;
> > > handle() : impl(new body) {}
> > > handle(handle const& rhs)
> > > : impl(new body(*rhs.get())) {}
> > > };
> > >
> > > Maybe you meant s/assignable/copyable/?
> >
> > Maybe. These emails are almost a year old. :-)
>
> Furthermore:
>
> handle& operator=(handle const& rhs)
> {
> impl.reset(new body(*rhs.impl));
> }
>
> It even gives the strong guarantee.

You are right, and this seems to apply to Herb's article, too.

Even with two scoped_ptr members, it's possible to avoid swap:

handle& operator=(handle const& rhs)
{
    auto_ptr<body1> temp(new body1(*rhs.impl1));
    impl2.reset(new body2(*rhs.impl2));
    impl1.reset(temp.release());
}

> What /is/ the rationale for swap?
> I think it's for nothrow swappability of the handle class.
> That's the only thing it seems to make possible.

Seems like it.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk