Article View: comp.std.c++
Article #2539Re: auto_ptr<T> as function return type
From: Sean A Corfield
Date: Mon, 13 Nov 1995 16:09
Date: Mon, 13 Nov 1995 16:09
43 lines
2394 bytes
2394 bytes
In article <47sm8h$4eh@gabi.gabi-soft.fr>,
kanze@gabi-soft.fr (J. Kanze) wrote:
|> Just curious: in what way is this any safer than using a const_cast in
|> the implementation of auto_ptr<T>::operator=()?
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=. The committee have not
decided exactly what to do about fixing auto_ptr yet but they intend to do
_something_ in Scotts Valley...probably just make the parameter of the copy
constructor const and force the implementation to work around it with a
cast (ugh!).
Sean A. Corfield
Object Consultancy Services -- voting member of X3J16
C++ - Beyond the ARM http://uptown.turnpike.net/~scorf/cplusext.html
---
[ 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:
<ACCCFC94966811A3CC@corf.demon.co.uk>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> <47nfem$48d@shellx.best.com> <47qp77$f48@oznet03.ozemail.com.au> <47sm8h$4eh@gabi.gabi-soft.fr>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 13 Nov 95 16:09:05 GMT Approved: fjh@cs.mu.oz.au From: Sean A Corfield <sean@corf.demon.co.uk> Newsgroups: comp.std.c++ Subject: Re: auto_ptr<T> as function return type X-Original-Date: Mon, 13 Nov 1995 13:45:56 +0000 Organization: OCS Message-ID: <ACCCFC94966811A3CC@corf.demon.co.uk> 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> X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMKdt2uEDnX0m9pzZAQEhdwGAhAmMQAzwxoOutbtoL3xsaieQSAacY0dm ZPP4hN96GQ5k/CjVTNYy1F7AZy4SR2tu =KgBN