auto_ptr

M

Mark

can someone clear up the following for me.

I've read somewhere that auto_ptrs cannot be used with standard containers
as they
don't meet the requirements for equivalence. That is "copies are not
equivalent".
First what does that mean.

I know this much: auto_ptrs maintain an ownship flag which is set/unset
during a copy
operation so that the following code

auto_ptr<myclass> pCopy2 = pCopy1;

would leavel pCopy2 owning the pointer to the class, and pCopy1 not owning
it.
Only pCopy2 would have the right to delete the object pointed to, which is
enforced in the destructor for auto_ptr.

What I don't understand is how this prevents us from using it in a standard
container
such as a map. Is it this? When a copy of an auto_ptr is inserted into a
map, although
the copy inside the map becomes the rightful owner of the pointer ( which
would
initially seem fine ) anytime we retrieve the pointer from the map, that
pointer becomes
invalidated meaning that we would have to reinsert the pointer back into the
map after
using it to transfer ownship back into the map. While this does'nt break
anything, if we
remember to do it is inconvenient.

Any insights appreciated.

Mark
 
A

Alf P. Steinbach

* Mark:
can someone clear up the following for me.

I've read somewhere that auto_ptrs cannot be used with
standard containers as they don't meet the requirements
for equivalence. That is "copies are not equivalent".

The main point is that you cannot copy a std::auto_ptr directly,
using direct assignment or copy construction: the attempted
copy operation destroys the original. Therefore there is
no std::auto_ptr copy constructor that takes a const std::auto_ptr
as argument. Standard containers rely on the objects inside the
containers to be copyable via assignment and copy construction,
with const argument. But even if the compiler should accept a
container of std::auto_ptr elements the container's code will likely
be based on assumptions about non-destructive copying. So the standard
simply requires that an element is Assignable and CopyConstructable.
 
J

Jonathan Turkanis

Mark said:
can someone clear up the following for me.
I know this much: auto_ptrs maintain an ownship flag which is
set/unset during a copy operation so that the following code

auto_ptr<myclass> pCopy2 = pCopy1;

would leavel pCopy2 owning the pointer to the class, and pCopy1 not
owning it. Only pCopy2 would have the right to delete the object pointed to,
which is enforced in the destructor for auto_ptr.

auto_ptr went through 2 major revisions before inclusion in the 1998 standard.
You appear to be using a pre-standard library which implements version 2.

There's a lot of good material here:

http://www.josuttis.com/libbook/auto_ptr.html

Also see the works cited here:

http://home.comcast.net/~jturkanis/move_ptr/libs/move_ptr/doc/index.html?path=14

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top