Boost logo

Boost :

From: Trevor Perrin (tperrin_at_[hidden])
Date: 2001-07-14 21:58:05


a question that's been bugging me: Since iterators are "like" pointers, it
seems you should be able to delete them:

delete somelist.begin(); //remove first element from somelist

Then you could have stl algorithms like delete_if() that actually removed
items, instead of the (seems to me hokey) partitioning behavior of unique()
or remove(). However it's not possible to override operator delete like
that, unless there's some trick I don't know about, so we could add an
erase() member function to them instead. Also not all containers' iterators
would be naturally eraseable but you could provide an eraseable_iterator
class:

template<class T>
class eraseable_iterator : public T::iterator
{
public:
  eraseable_iterator(T& t, T::iterator i):
    T::iterator(i), m_t(t){}
  void erase(){m_t.erase(*this);}
private:
  T& m_t;
};

And specialize appropriately so that, for example, a vector<T>::iterator
could be converted into a deletable form. Anyways, I'm sure this has been
ruminated over before. Why don't we have deletable iterators? Are they
just a stupid idea? Is there any reasonable way of providing them that
would be worthwhile in boost or the stl?

Trevor


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