🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: auto_ptr<T> as function return type

#2486
From: J. Kanze
Date: Thu, 09 Nov 1995 19:23
36 lines
2015 bytes
John Max Skaller (maxtal@suphys.physics.su.oz.au) wrote:

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

Just curious: in what way is this any safer than using a const_cast in
the implementation of auto_ptr<T>::operator=()?
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
              -- A la recherche d'une activité dans une region francophone

---
[ 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: <47sm8h$4eh@gabi.gabi-soft.fr>
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> <47qp77$f48@oznet03.ozemail.com.au>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: kanze@gabi-soft.fr (J. Kanze)
Newsgroups: comp.std.c++
Subject: Re: auto_ptr<T> as function return type
Date: 09 Nov 1995 19:23:13 GMT
Organization: GABI Software, Sarl.
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <47sm8h$4eh@gabi.gabi-soft.fr>
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>
NNTP-Posting-Host: physics2.berkeley.edu