Article View: comp.std.c++
Article #1944Re: Implementing auto_ptr + type of operator->*
From: kanze
Date: Mon, 11 Sep 1995 19:04
Date: Mon, 11 Sep 1995 19:04
62 lines
3171 bytes
3171 bytes
In article <42sf4t$9im@eurybia.rz.uni-konstanz.de>
kuehl@horn.rz.uni-konstanz.de (Dietmar Kuehl) writes:
|> Rogier Goossens (goossens@cs.few.eur.nl) wrote:
|> : I have two more remarks:
|> :
|> : > template<class T>
|> : > inline void operator=(auto_ptr<T>& rhs)
|> : > { ptr = rhs.release; }
|> :
|> : Don't forget to delete the previous object ptr pointed to.
|> The standard doesn't say something about deleting the old pointer! My
|> interpretation is this: auto_ptr is not supposed to delete all pointers
|> but only the one it is holding when it gets destructed. This semantic
|> is indeed useful if it is possible that an exception is thrown while
|> nothing else takes care of the allocated object or some memory should
|> be released when exiting a function. In the meantime the programmer has
|> still to care for the pointer if it is replaced.
The draft standard does say that the semantics of assignment include a
call to reset. It doesn't say anything about the semantics of reset,
but the original proposal did.
>From 94-0202R1/N0289R1:
Semantics
---------
An auto_ptr<X> object holds a pointer to an object of class X, presented
here as X* px. In the following table: p and px are pointers to an object
of class X or a class derived from X for which delete(X*) is defined and
accessible, or else null; d is an auto_ptr<D> where D is X or a class
derived from X; a is an auto_ptr<X>; and m is a member of X.
Expression Value Effect
------------------ --------------- ---------------------
auto_ptr<X> a(p) a.px = p
auto_ptr<X> a(d) a.px = d.release()
a.~auto_ptr<X>() delete a.px
a = d reference to a a.reset(d.release())
*a *a.px
a->m a.px->m
a.get() a.px
a.release() a.px a.px = 0
a.reset(p) delete a.px, a.px = p
IMHO, these are the only semantics which make sense; if I *don't*
delete the old pointer in assignment, who will?
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
---
[ 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:
<MATT.95Sep11120429@physics2.Berkeley.EDU>Path:
arc1-ca.pugleaf.net!128.32.155.1!mattReferences:
<9509080952.AA15973@jodium.cs.few.eur.nl> <42sf4t$9im@eurybia.rz.uni-konstanz.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt From: kanze <kanze@lts.sel.alcatel.de> Newsgroups: comp.std.c++ Subject: Re: Implementing auto_ptr + type of operator->* Date: 11 Sep 1995 19:04:29 GMT Organization: <none> Approved: matt@physics2.Berkeley.EDU (Matt Austern) Message-ID: <MATT.95Sep11120429@physics2.Berkeley.EDU> References: <9509080952.AA15973@jodium.cs.few.eur.nl> <42sf4t$9im@eurybia.rz.uni-konstanz.de> NNTP-Posting-Host: physics2.berkeley.edu