|
Boost : |
From: Beman Dawes (bdawes_at_[hidden])
Date: 2002-02-04 17:51:52
At 09:20 AM 2/4/2002, Brey, Edward D wrote:
>I think that an auto_vector class would be a great addition to boost.
>Coincidentally, I just made an auto_vector for myself just a week
ago. It
>wasn't nearly complete enough (and in some ways possibly had too much)
for
>Boost, but rather just for my current project's needs. In any case, the
>concept is definitely useful.
>
>I can share a bit of my experience vis a vis George's suggestion
regarding
>returning references. Before I made my auto_vector, I had made an
earlier
>incantation where the iterators performed the dereference for you. That
>worked out fine until I needed to copy the pointers to another container
>(the destination container just held pointers, but didn't own any
objects).
>The problem was that I couldn't go backwards from a reference/pointer to
an
>iterator. At this point I decided that there wasn't enough value in
hiding
>the indirection level to be worth the trouble it causes.
>
>This predicament points out a general lacking in STL that I'd like to
hear
>other's opinions of. That is the concept of transforming an iterator to
a
>pointer...
Be very careful. See library issue 198
http://std.dkuug.dk/jtc1/sc22/wg21/docs/lwg-active.html#198
Unless the container gives guarantees beyond those of the standard's
container and iterator requirements, you could be headed for undefined
behavior.
Although some of the standard library's containers do give stronger
guarantees, in general the object an iterator points to may be cached, and
go away when the last iterator to the object goes away. Thus it is
generally unsafe to hang onto pointers or references which escape from
iterators.
--Beman
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk