Article View: comp.std.c++
Article #2450Re: auto_ptr<T> as function return type
From: law
Date: Tue, 07 Nov 1995 13:52
Date: Tue, 07 Nov 1995 13:52
35 lines
2402 bytes
2402 bytes
In <47l6im$qu1@watnews2.watson.ibm.com>, jjb@watson.ibm.com (John Barton) write s: > 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. I'm confused, John. You say "nicely put" but he said that objects should be put into an auto_ptr<> immediately after creation. Your code certainly doesn't do that. Your code is also a prescription for undeleted objects (despite the comment). What's crazy about returning an auto_ptr. aside from the unnecessary restriction that it can't be used to assign to or construct another auto_ptr directly? And why is lifetime control by a "temporary" fundamentally different than lifetime control by some named object? The semantics of the two objects is the same, isn't it? They just have different lifetimes. Bill Law --- [ 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:
<47nfem$48d@shellx.best.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> <47l6im$qu1@watnews2.watson.ibm.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 07 Nov 95 13:52:25 GMT Approved: fjh@cs.mu.oz.au From: law@solution-frameworks.com Newsgroups: comp.std.c++ Subject: Re: auto_ptr<T> as function return type X-Original-Date: 7 Nov 1995 11:20:54 GMT Organization: Solution Frameworks, San Jose CA Message-ID: <47nfem$48d@shellx.best.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> <47l6im$qu1@watnews2.watson.ibm.com> Reply-To: law@solution-frameworks.com (Bill Law) X-Newsreader: IBM NewsReader/2 v1.2 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMJ9kuOEDnX0m9pzZAQHInAF+MBMvWbBiAYEbrkk6mCS7bNY0fAQxIIlT qVIPu0PRzmH+O7rQzjtNKJGjDAKeskHF =R3DO