|
Boost : |
From: David Abrahams (abrahams_at_[hidden])
Date: 2001-04-05 12:13:02
----- Original Message -----
From: "Peter Dimov" <pdimov_at_[hidden]>
> From: "Gary Powell" <Gary.Powell_at_[hidden]>
>
>
> > Hi Peter,
> >
> > I emailed way too quick. My B.S. "C++ Programming Language" 3rd Ed. 5th
> > printing book, section 14.4.2 says "auto_ptr_ref is to implement the
> > destructive copy semantics for ordinary auto_ptrs while making it
> impossible
> > to copy a const auto_ptr."
> >
> > Is there another way to ensure this constraint?
>
> AFAIK auto_ptr_ref supports passing/returning auto_ptrs by value. I think
> that this is unnecessary for boost::auto_ptr, since (1) we have
> boost::shared_ptr for this, (2) passing auto_ptrs by value instead of
> non-const reference is dangerous (exception safety), and (3) returning
> std::auto_ptrs by value is sometimes useful since std::auto_ptr is
standard,
> but boost::auto_ptr is not, so it doesn't need this "capability."
I strongly disagree with this. My coding standard says "always pass auto_ptr
by value". Why? When I see auto_ptr<...> in a piece of code, I think
"transfer of ownership is happening here". Passing auto_ptr by const
reference undermines that meaning. If you are going to pass by const
reference, you might as well call get() and pass a raw pointer, anyway.
Passing an auto_ptr by value (or by non-const reference) is the only way to
transfer ownership into a function. I really prefer the former, because it
makes the meaning absolutely unambiguous (ownership is definitely given
away).
What are the exception-safety problems that you allude to?
-Dave
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk