From 1928465415272671173
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109fba,df06663c0326d841
X-Google-Attributes: gid109fba,public
X-Google-Thread: f78e5,df06663c0326d841
X-Google-Attributes: gidf78e5,public
From: "Larry Brasfield" <larry_br@sea_net.com>
Subject: Re: auto_ptr copy semantics
Date: 1999/04/11
Message-ID: <M7UP2.31277$WM.2611@news.rdc1.wa.home.com>#1/1
X-Deja-AN: 465031366
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <7el5br$kjj$1@nnrp1.dejanews.com>
X-Original-Date: Sun, 11 Apr 1999 02:52:28 GMT
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: izvestia.its.unimelb.edu.au 923815142 3205 128.250.29.16 (11 Apr 1999 07:19:02 GMT)
Organization: Serendipitous Endeavors
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANxBMz+EDnX0m9pzZAQErUwF+MIVxfJSIsEGTLbX48E6aWCkhvxFQFoPE YjantUMlJiPuf6C4M5yigXTHMxW0t+aO =SaHK
X-MSMail-Priority: Normal
NNTP-Posting-Date: 11 Apr 1999 07:19:02 GMT
Newsgroups: comp.lang.c++,comp.std.c++

<sjm@cae.co.uk> wrote in message news:7el5br$kjj$1@nnrp1.dejanews.com...
> What is the rationale behind the copy semantics of auto_ptr
> transfering ownership of its contained pointer?

I don't think "copy semantics" is quite the right
phrase for what auto_ptr does.  The rationale
is all in support of making it easy to write
exception safe code that uses the freestore.
For that limited purpose, and with certain
caveats, auto_ptr works just fine.  In general,
the caveat is that copies are always for the
purpose of getting the pointer to its ultimate
point of use and destruction.

> If the auto_ptr is passed to a function by *value*, then isn't
> your referenced object extinct upon return (because of local
> copies being made which claim ownership and then release the
> contained object upon their own demise)?
>
> e.g.
>
>     auto_ptr<Thing> ptr( new Thing );
>     doStuff( ptr ); // ...by value.
>     // here, ptr no longer refers to anything.

Yes.  That is one of many reasons to avoid
passing auto_ptr's by value.  When I have
occasion to pass them, I always pass them
by reference.

> Wouldn't it be better for the *original* auto_ptr to *always*
> retain ownership, and for copies to *never* do so?

I very useful usage that would be made useless
by that rule is where some function returns an
auto_ptr.  In that case, you would not want the
object to die alongside some local variable in
that function.

auto_ptr is best used for pointers that are not
copied except as a consequence of creation.

--
--Larry Brasfield
Above opinions may be mine alone.
(Humans may reply at unundered larry_br@sea_net.com )
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



