Article View: comp.std.c++
Article #2442Re: auto_ptr<T> as function return type
From: John Barton
Date: Tue, 07 Nov 1995 04:33
Date: Tue, 07 Nov 1995 04:33
33 lines
2305 bytes
2305 bytes
In article <9511030943.AA23251@lts.sel.alcatel.de>, James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> writes: |> [delete some stuff] |> ... The whole point of auto_ptr |> is that the object is put into an auto_ptr immediatly after creation, |> and is in an auto_ptr at all times. Any tricks involving removing it |> from one auto_ptr, and later putting it in another, are just that: |> tricks, and introduce an element of danger. The purpose of auto_ptr |> is precisely to eliminate this danger. Nicely put. Strict ownership is fundamentally incompatible with auto_ptr<T> as function return type. The latter implies ownership by the returned temporary: lifetime control by a temporary is craziness. (I have tried to beat this game myself several times). The way to use auto_ptr<T> successfully is to limit it to client code. Service providers write: // Callers own the lifetime of returned object; pick it up in an auto_ptr. Foo* doWorkAndCreateAFoo(); Clients pick up the result in auto del pointers: auto_ptr<Foo> my_foo(doWorkAndCreateFoo()); Anyone who fails to read and heed the comment loses. -- John. John J. Barton jjb@watson.ibm.com (914)784-6645 <http://www.research.ibm.com/xw-SoftwareTechnology> H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598 --- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<47l6im$qu1@watnews2.watson.ibm.com>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<47616o$lvr@bcarh8ab.bnr.ca> <VANDEVOD.95Oct31222003@juicer.cs.rpi.edu> <9530600.21749@mulga.cs.mu.OZ.AU> <47b1um$fvs@infoserv.rug.ac.be> <9511030943.AA23251@lts.sel.alcatel.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 07 Nov 95 04:33:15 GMT Approved: fjh@cs.mu.oz.au From: jjb@watson.ibm.com (John Barton) Newsgroups: comp.std.c++ Subject: Re: auto_ptr<T> as function return type X-Original-Date: 6 Nov 1995 14:37:10 GMT Organization: IBM T.J. Watson Research Center Message-ID: <47l6im$qu1@watnews2.watson.ibm.com> References: <47616o$lvr@bcarh8ab.bnr.ca> <VANDEVOD.95Oct31222003@juicer.cs.rpi.edu> <9530600.21749@mulga.cs.mu.OZ.AU> <47b1um$fvs@infoserv.rug.ac.be> <9511030943.AA23251@lts.sel.alcatel.de> X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMJ7hr+EDnX0m9pzZAQFxugF/ZcuvCs99pGX3/gnjLJSYZpSEeOT9nb8q RB2EsalXE+4ArID9jOjISJ3QFfb7q7Yu =GvLu