🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: auto_ptr: :reset and auto_ptr::operator=

#2087
From: Lars Farm
Date: Mon, 02 Oct 1995 15:46
54 lines
2169 bytes
In article <9527400.19710@mulga.cs.mu.OZ.AU>,
fjh@munta.cs.mu.OZ.AU (Fergus Henderson) wrote:

>I think the assignment operator and copy-constructor of auto_ptr
>ought to be declared private.  If you want to copy an auto_ptr,
>then instead of writing
>
>	auto_ptr<T> y = x;	// copy constructor
>	y = x;			// assignment operator
>
>
>you should have to write
>
>	auto_ptr<T> y = x.release();
>	y = x.release();
>
>This makes it clear that x is being modified.

Nowhere else in a c++ program will a valid ptr_a become invalid by:

  ptr_b = ptr_a;

Nowhere else in a c++ (or any other) program will the rhs be altered by
assignment.

If I really want to transfer ownership I can do
ptr_b.reset(ptr_a.release()); If I'm unaware of the danger, the compiler
will point me in the right direction. As it stands it will be quite hard to
explain to students why this must behave so counterintuitively. IMHO the
simpler version with private copy/op= will also be easier to use.

Also:

void f( auto_ptr<X> ); // this might not make sense (to you), 
                       // but you must be aware of the dangers 
                       // with auto_ptr to know... many wont be
void g()
{
   auto_ptr<X> x( new X );
   ...
   x->m     // x is valid
   f( x );
   x->m;    // oops... compiler would catch this with private copy-ctor

I think they should be private.

--
Lars Farm, lars.farm@nts.mh.se

---
[ 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: <AC95AF519668119E08@sleipner.nts.mh.se>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
References: <199509111753.AA15077@xs1.xs4all.nl> <gregorDEtM0t.Hw2@netcom.com> <MATT.95Sep22101332@physics11.Berkeley.EDU> <9527400.19710@mulga.cs.mu.OZ.AU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: lars.farm@nts.mh.se (Lars Farm)
Newsgroups: comp.std.c++
Subject: Re: auto_ptr: :reset and auto_ptr::operator=
Date: 02 Oct 1995 15:46:07 GMT
Organization: pv
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <AC95AF519668119E08@sleipner.nts.mh.se>
References: <199509111753.AA15077@xs1.xs4all.nl> <gregorDEtM0t.Hw2@netcom.com> <MATT.95Sep22101332@physics11.Berkeley.EDU> <9527400.19710@mulga.cs.mu.OZ.AU>
NNTP-Posting-Host: physics2.berkeley.edu