Topic: auto_ptr initialization and assignment
Author: John Nagle <nagle@animats.com>
Date: Fri, 25 Jan 2002 18:28:19 GMT Raw View
Andy wrote:
> Hi all,
>
> I tried to understand the things you can and cannot do with auto_ptr, and
> have some questions for which I'd like presize answer.
You are not supposed to understand auto_ptr.
There have been three versions of the auto_ptr
standard, and they all suck. The basic
problem is that you need to be able to copy auto_ptr
objects to get anything useful done, and you can't
do so without breaking the single-owner semantics
of auto_ptr. Currently, "=" for auto_ptr destroys
the RIGHT hand side of the assignment, maintaining
single-owner semantics. This is truly wierd, but the
best available compromise.
You have to be able to assign plain pointers
to auto_ptr objects, because new generates a
plain pointer. Logically, there should be
an auto_ptr form of "new", but you can't write
that within C++.
The C++ type system isn't powerful enough to
enforce the restrictions needed to make auto_ptr
safe. I wend round and round with this a few months
back with my "strict C++" proposal, and it just
doesn't retrofit to C++.
Hence the current mess.
John Nagle
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: Fri, 25 Jan 2002 21:43:52 GMT Raw View
John Nagle wrote:
> You are not supposed to understand auto_ptr.
That's a bit harsh.
> There have been three versions of the auto_ptr
> standard, and they all suck.
There is exactly one version of the auto_ptr standard, all
those other flavors were the various mutations on the
way to generating to the standard.
> You have to be able to assign plain pointers
> to auto_ptr objects, because new generates a
> plain pointer.
Actually, you can't do that with the standard auto pointer.
You can either initialize it with a pointer, or use reset
to set it after creation.
> Logically, there should be
> an auto_ptr form of "new", but you can't write
> that within C++.
> The C++ type system isn't powerful enough to
> enforce the restrictions needed to make auto_ptr
> safe.
You've lost me on this one.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: salnikov@mail.com (Andy)
Date: Tue, 22 Jan 2002 16:46:26 GMT Raw View
Hi all,
I tried to understand the things you can and cannot do with auto_ptr, and
have some questions for which I'd like presize answer. Suppose the following
declarations:
class A {
//...
} ;
auto_ptr<A> source() ;
Am I right that the foolowing is correct code:
auto_ptr<A> p1 ( new A ) ;
auto_ptr<A> p2 ( source() ) ;
auto_ptr<A> p3 = source() ;
p3 = source() ;
p3.reset ( new A ) ;
and the following is illegal:
auto_ptr<A> p4 = new A ;
p4 = new A ;
Sorry for this dumb question, but many people get confused by the auto_ptr,
maybe it could clarify things a bit.
Andy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: Tue, 22 Jan 2002 16:54:01 GMT Raw View
Andy wrote:
>
> Am I right that the foolowing is correct code:
>
> auto_ptr<A> p1 ( new A ) ;
Legal.
> auto_ptr<A> p2 ( source() ) ;
> auto_ptr<A> p3 = source() ;
Legal, but a bit wierd. Note that auto_ptr's copy
constructor doesn't take a const auto_ptr& so this
would work except that there is a little helper class
called auto_ptr_ref that provides an intermediate
conversion.
> p3 = source() ;
Ditto for operator=.
> p3.reset ( new A ) ;
Legal.
>
> and the following is illegal:
>
> auto_ptr<A> p4 = new A ;
This is legal.
> p4 = new A ;
Not legal because there is no operator= that takes anything other than
other auto_ptrs. The pointer can't be implicitly converted to auto_ptr
because the constructor that takes a pointer is declared explicit. Use
reset.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Andy Salnikov" <salnikov@slac.stanford.edu>
Date: Tue, 22 Jan 2002 17:48:40 GMT Raw View
"Ron Natalie" <ron@sensor.com> wrote in message
news:3C4D99D3.234473CC@sensor.com...
>
>
> Andy wrote:
> >
> > and the following is illegal:
> >
> > auto_ptr<A> p4 = new A ;
>
> This is legal.
>
Can you explain? Is there a conversion which does this?
Andy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: Tue, 22 Jan 2002 17:55:58 GMT Raw View
Andy Salnikov wrote:
>
> "Ron Natalie" <ron@sensor.com> wrote in message
> news:3C4D99D3.234473CC@sensor.com...
> >
> >
> > Andy wrote:
> > >
> > > and the following is illegal:
> > >
> > > auto_ptr<A> p4 = new A ;
> >
> > This is legal.
> >
> Can you explain? Is there a conversion which does this?
>
Sorry. You're right. Can't do copy initialization here
(explicit constructor for A*).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]