🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Cleaning auto_ptr copy semantics.

#3247
From: John Max Skaller
Date: Mon, 05 Feb 1996 00:09
41 lines
2088 bytes
Eugene Lazutkin wrote:
[auto_ptr]

> It's good if you are going to create a STL
> container of auto_ptr's.  

	EGADS! NEVER DO THIS!!!!

	The following will CRASH FOR SURE:

	void f(vector<auto_ptr<X> >){}
	vector<auto_ptr<X> > vx; vx.push_front(new X);
	f(vx);
	vx[0]; // CRASH

Copying the container transfers ownership to the NEW container.
You must NOT use FIFO/block structure here: if the new
container is destroyed before the old one, all the auto_ptrs
in the old container dangle.

Even worse, if you have an input iterator onto the container
which returns an rvalue, dereferencing it steals ownership
from the container by creating a copy -- the destruction
of which will delete the object, leaving the container's
auto_ptr dangling. 

[Correct use of the idiom requires the newest copy of
an auto_ptr outlive use of older ones. This is often
achieved by assigning the newest value back to the object
it was copied from.]

-- 
John Max Skaller               voice: 61-2-566-2189
81 Glebe Point Rd              fax:   61-2-660-0850
GLEBE NSW 2037                 web: http://www.maxtal.com.au/~skaller/
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://reality.sgi.com/employees/austern_mti/std-c++/policy.html
]
Technical Details
Message-ID: <31153AC5.1776@suphys.physics.su.oz.au>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
References: <01BAF15D.08B8EEE0@dino.int.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: John Max Skaller <maxtal@suphys.physics.su.oz.au>
Newsgroups: comp.std.c++
Subject: Re: Cleaning auto_ptr copy semantics.
Date: 5 Feb 1996 00:09:26 GMT
Organization: MAXTAL
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <31153AC5.1776@suphys.physics.su.oz.au>
References: <01BAF15D.08B8EEE0@dino.int.com>
NNTP-Posting-Host: taumet.eng.sun.com
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Nntp-Posting-Host: slsyd8p04.ozemail.com.au
X-Mailer: Mozilla 2.0b6a (WinNT; I)
Content-Length: 1171
Originator: clamage@taumet