Article View: comp.std.c++
Article #2639Re: auto_ptr<T> as function return type
From: Pablo Halpern
Date: Sat, 18 Nov 1995 20:10
Date: Sat, 18 Nov 1995 20:10
75 lines
3284 bytes
3284 bytes
Sean A Corfield <sean@corf.demon.co.uk> wrote:
>In Tokyo last week, Bill Gibbons suggested using a proxy nested class to
>alleviate this problem. It works by having auto_ptr constructable from the
>proxy object and assignable from the proxy object then having auto_ptr
>convert to the proxy. Something like this:
>
> template<typename T> class auto_ptr {
> struct proxy {
> auto_ptr* p;
> proxy(auto_ptr* ap) : p(ap) { }
> };
> public:
> auto_ptr(const proxy& prox) : p(prox.p->p) { prox.p->p = 0; }
> // etc
> operator proxy() { return proxy(this); }
> // etc
> private:
> T* p;
> };
>
>
> auto_ptr<X> f();
> auto_ptr<X> p(f());
> // equivalent to:
> auto_ptr<X> p(f().operator proxy());
>
>Similar code changes could be done for operator=.
Wouldn't it be more efficient to code the so-called "copy constructor"
using pass by value to avoid the extra unnamed temporary:
auto_ptr(proxy prox) : p(prox.p->p) { prox.p->p = 0; }
This way, the generated code should be exactly as efficient as passing a
non-const auto_ptr by reference. My concern with both approaches is that
the WP does not actually consider the above to be true copy constructors
(although the ARM did). Thus, the compiler may generate its own copy
constructor for auto_ptr<T>, thus defeating the whole thing.
Just for completeness, I'll mention another solution, ugly as it is:
template<typename T> class auto_ptr {
public:
auto_ptr() : pp(new T*(0)) { } // pp = Pointer to null pointer
auto_ptr(const auto_ptr<T>& rhs) : pp(new T*(*rhs.pp))
{ *rhs.pp = 0; }
~auto_ptr() { delete *pp; delete pp; } // double delete
T* release() { T* p = *pp; *pp = 0; return p; }
auto_ptr<T>& operator=(const auto_ptr<T>& rhs)
{
if (this == &rhs) return *this;
delete *pp;
*pp = *rhs.pp;
*rhs.pp = 0; // pp is const pointer to non-const pointer to T
return *this;
}
private:
T** pp; // double indirection
};
Of course, this is much less efficient, with all of the heap
manipulations and double-indirections, but I think it would work.
-------------------------------------------------------------
Pablo Halpern phalpern@truffle.ultranet.com
---
[ 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:
<48l0lk$lq6@caesar.ultra.net>Path:
arc1-ca.pugleaf.net!news.mindlink.net!agate!agate!MattReferences:
<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> <47qp77$f48@oznet03.ozemail.com.au> <47sm8h$4eh@gabi.gabi-soft.fr> <ACCCFC94966811A3CC@corf.demon.co.uk>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.mindlink.net!agate!agate!Matt From: phalpern@truffle.ultranet.com (Pablo Halpern) Newsgroups: comp.std.c++ Subject: Re: auto_ptr<T> as function return type Date: 18 Nov 1995 20:10:18 GMT Organization: Ultranet Communications, Inc., Marlboro MA USA Approved: Matt@Austern Message-ID: <48l0lk$lq6@caesar.ultra.net> 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> <47qp77$f48@oznet03.ozemail.com.au> <47sm8h$4eh@gabi.gabi-soft.fr> <ACCCFC94966811A3CC@corf.demon.co.uk> NNTP-Posting-Host: austern.hip.berkeley.edu