From 5370664145085937950
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: bill@gibbons.org (Bill Gibbons)
Subject: Re: auto_ptr copy semantics
Date: 1999/04/15
Message-ID: <bill-1404991010270001@bgibbons.vip.best.com>#1/1
X-Deja-AN: 466665406
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 10:10:27 -0700
X-Authentication-Warning: backdraft.philabs.research.philips.com: smap set sender to <news@nntp1.ba.best.com> using -f
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: izvestia.its.unimelb.edu.au 924171921 19034 128.250.29.16 (15 Apr 1999 10:25:21 GMT)
Organization: -none-
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANxW+d+EDnX0m9pzZAQGG3QGAoN95X9/pvVD5wWb7h0zHkY1NKyy8T3b9 RSNd0gXMZGMnKgUSzv7K1TZO519CUol8 =EKlr
NNTP-Posting-Date: 15 Apr 1999 10:25:21 GMT
Newsgroups: comp.lang.c++,comp.std.c++

In article <7f0i8a$sc9$1@nnrp1.dejanews.com>, sirwillard@my-dejanews.com wrote:

> ... argument that auto_ptr shouldn't be passed to/from a function ....
> 
> 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.

On the contrary, careful management of memory demands that ownership be
very well defined and tracked, and sometimes that requires passing ownership
to or from a function.  That's the reason why it was made to work.
Tracking ownership manually is very error-prone.  You can use auto_ptr
to document and enforce ownership transfers and greatly reduce the errors.
Used properly this also makes code easier to understand and maintain.

If you want to pass ownership of an object to a function (for example,
because the function will save the pointer in another object and give
ownership to that object) you should pass an auto_ptr by value.

If you want to pass ownership of an object back from a function (for
example, the function creates the object) you should return an auto_ptr
by value.

If you want to track ownership manually (and risk getting it wrong) but
use auto_ptr as a simple-minded automatic delete facility, you should
never pass or return an auto_ptr by value.


-- Bill Gibbons
   bill@gibbons.org
---
[ 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              ]



