From -8728843727653089902
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 109fba,df06663c0326d841
X-Google-Attributes: gid109fba,public
X-Google-Thread: f78e5,df06663c0326d841
X-Google-Attributes: gidf78e5,public
From: "J.Barfurth" <techview@bfk-net.de>
Subject: Re: auto_ptr copy semantics
Date: 1999/04/15
Message-ID: <7f28nr$dmh$1@news.hamburg.pop.de>#1/1
X-Deja-AN: 466656968
Content-Transfer-Encoding: quoted-printable
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <7el5br$kjj$1@nnrp1.dejanews.com> <MPG.117c078a69a24c459896e7@client.sw.news.psi.net> <7f0i8a$sc9$1@nnrp1.dejanews.com>
X-Original-Date: Wed, 14 Apr 1999 15:47:47 +0200
Content-Type: text/plain; charset=iso-8859-1
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: izvestia.its.unimelb.edu.au 924169463 20359 128.250.29.16 (15 Apr 1999 09:44:23 GMT)
Organization: POP Point of Presence GmbH, Hamburg, Germany (http://www.pop.de)
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANxW02eEDnX0m9pzZAQE5+AF+IrNn99tf7dCUVPiSb6aZUmS8ZYJJ1gXr dPDPUrr+jgbdSOYoB13c0pfZg6SPH66A =bcqv
Mime-Version: 1.0
NNTP-Posting-Date: 15 Apr 1999 09:44:23 GMT
Newsgroups: comp.lang.c++,comp.std.c++
X-MIME-Autoconverted: from 8bit to quoted-printable by ncar.UCAR.EDU id IAA09363

sirwillard@my-dejanews.com schrieb in Nachricht
<7f0i8a$sc9$1@nnrp1.dejanews.com>...
>The long and short of it is that you should really never pass an auto_pt=
r
into
>a function (well, never is awfully strong, but if you read on you'll see
that
>it's highly unlikely that you want to ever do this).  Let's look at both
ways
>of passing an auto_ptr, by reference and by value.
>
>First, by reference:
[snip]
Agreed, can almost never be correct

>As for the other way, we've already discussed the problem.  Ownership is
>passed, and thus we're left with a "dangling auto_ptr" in the calling
>function.
>So, we're left to conclude that we should only ever pass real pointers:

We are also left with a (potentially) dangling pointer, if the specificat=
ion
of a function [say g(T*)] states, that the call transfers ownership. In t=
his
case, if the caller uses an auto_ptr<T> for exception safety, it would ha=
ve
to call it as g(ptr.release()). That's easy to forget. Using an auto_ptr =
as
the type of the parameter works properly in both cases: when either a bar=
e
pointer or an auto_ptr is used as the argument.

>Disclaimer:  There are rare circumstances when you want to pass the
>ownership, and in such circumstances it may be valid to pass an auto_ptr=
 by
>value. However, some may contend that this is bad practice, as it leads =
to
>code that's harder to understand and maintain.


IME it isn't that rare to pass ownership, but this of course is a matter =
of
(design) style. But when ownership is taken by a function, I always make
that explicit in the function's signature, by using some kind of smart
pointer as parameter type (auto_ptr being the least smart of these).
Admittedly this does make it harder to understand client code - you have =
to
look up the callee's signature to see that such a transfer is taking plac=
e.
But ownership semantics must be well documented anyways, and (as someone
else recently mentioned on this group) good naming can make up for a lot =
of
sins.

-- J=F6rg Barfurth
---
[ 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              ]



