🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2457

Re: auto_ptr<T> as function return type

#2457
From: John Max Skaller
Date: Wed, 08 Nov 1995 18:03
73 lines
3403 bytes
law@solution-frameworks.com wrote:
>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.

   Yes it does. "doWorkAndCreateFoo()" is a factory function
and is used directly as the argument for "my_foo"s ctor.

>Your code is also a prescription for undeleted objects (despite the
>comment).  

   Thats the point: the constraints on correct use are 
indeed _prescribed_ (documented). Thats a heck of a lot better
than a lot of commercial libraries which talk all about
functionality and forget to mention ownership.

>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?

    Temporaries are rvalues and so non-const references cannot be
bound to them by non-member functions, so they cannot be
modified (without a const_cast<>), and so they cannot 
transfer ownership (which is a destructive operation).

    A solution is to provide a member function:

    struct auto_ptr { ...
       auto_ptr<T> &ref() { return *this; }
       ......
    };

and now:

    auto_ptr<int> f();
    auto_ptr<int> p(f().ref());

This is a way of converting a non-const rvalue into
a non-const lvalue. It is invasive (requires a member function)
but it is safe, which a const_cast<> is not.

-- 
John Max Skaller               voice: 61-2-566-2189
81 Glebe Point Rd              fax:   61-2-660-0850
GLEBE NSW 2037                 email: maxtal@suphys.physics.oz.au
AUSTRALIA                      email: skaller@maxtal.com.au



---
[ 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: <47qp77$f48@oznet03.ozemail.com.au>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
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> <47nfem$48d@shellx.best.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: John Max Skaller <maxtal@suphys.physics.su.oz.au>
Newsgroups: comp.std.c++
Subject: Re: auto_ptr<T> as function return type
Date: 08 Nov 1995 18:03:11 GMT
Organization: MAXTAL
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <47qp77$f48@oznet03.ozemail.com.au>
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> <47nfem$48d@shellx.best.com>
NNTP-Posting-Host: physics2.berkeley.edu