Boost logo

Boost :

From: Pavol Droba (droba_at_[hidden])
Date: 2004-09-26 14:41:47


Hi,

On Sun, Sep 26, 2004 at 05:43:44PM +0200, Pavel Vozenilek wrote:
> 39. docs question: "Which mutating algorithms are safe
> to use with pointers?"
>
>
> Maybe overloads of std::unique etc could be added
> to avoid accidental misuse. Something like:
>
> namespace std {
>
> template<...>
> xyz unique(boost::ptr_vector<T>::iterator, ...);
>
> }
>
> At least there should be exhaustive list of
> nnt-supported std and Boost algorithms.
>
>

I see this question as a good oportunity to start a discussion about one
IMO very important topic.

It is not sufficiently metioned in the docs, but the truth is, that there is
only a very limited set of algoritms, that are safe to use with mutable
ptr_iterator.

The definition can be narrowed to this:

"An algorithm must not add, or remove any element from the container,
 nor it can make a duplicates. It can only swap elements around in the container"

Problem is, that there is no explicit handling of pointer retrieved by ptr_iterator.
So:

* if a pointer is removed from the container, it will not be deleted later.
* if a pointer is copied in the container, it can be deleted multiple times.
* if a new pointer is written to the iterator, the container automaticaly takes
  an ownership of it.

In other words, only algorithms like sorting and permutations are safe.

Thorsten, please correct me if I'm wrong.

I find this behaviour quite dangerous and I don't see any great benefits it brings.
I suggest, that there should be only a const version of ptr_iterator available.

Usable algorithms can be provided as member functions and container can provide
method swap_element, that can be used by used by user-defined algorithms.

Regards,

Pavol


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk