Boost logo

Boost :

From: Kevin Atkinson (kevinatk_at_[hidden])
Date: 2000-03-06 05:40:35


Dave Abrahams wrote:
>
> on 3/6/00 12:55 AM, Kevin Atkinson at kevinatk_at_[hidden] wrote:
>
> > I want two propose two pseudo smart points which I have found extremely
> > useful in my code.
> >
> > CopyPtr ptr primary used to avoid unnecessary interdependencies and to
> > support the pimpl idiom. A typical usage would be:
> >
> > class Foo {
> > class Pimpl;
> > CopyPtr<Pimpl> pimpl_;
> > };
> >
> > or
> >
> > class Bar;
> > class Foo {
> > CopyPtr<Bar> bar_;
> > };
> >
> > As is usage when ever one CopyPtr gets assigned to another a copy is
> > made.
>
> Clearly, to get the benefits of removing interdependencies, you must define
> an out-of-line copy constructor and assignment operator. This means you must
> remember to write:
>

No. Just don't include the "*-t.hh" header files. Include them
elsewhere where the definition of Pimpl is known and if nessary
instanstae the copy ptr for Pimpl.

> Comparing these with what's required if auto_ptr<> is used instead of
> CopyPtr, I don't see much savings.
>
> Foo::Foo(const Foo& rhs)
> : pimpl_(new Pimpl(*rhs.pimpl_))
>
> Foo& Foo::operator=(const Foo& rhs)
> {
> //assign bases, other members here
> pimpl_ = std::auto_ptr<Pimpl>(new Pimpl(*rhs.pimpl_));
> }

Here you are unessary makeing a new copy with new. This is unnessearly
expensive. When
a *pimpl_ = *other.pimpl_ will do if there were pure pointers and in
fact this is what CopyPtr does with the assignment method.

> Okay, I'll concede that the incantation for assignment is simpler, but the
> swap-based version is actually identical.

No its not.

> Also, the idea that some CopyPtrs can own their target while others do not
> is worrisome to me. It certainly seems like an unneccessary complication and
> a good hole to drive bugs through.

Well it works VERY well for me. Perhapes not for others.
>
> Finally, your #include guards are names reserved to the C++ implementation.

Minor point so fix it before final inclusion, if it does get included.

-- 
Kevin Atkinson
kevinatk_at_[hidden]
http://metalab.unc.edu/kevina/

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