Boost logo

Boost Users :

Subject: [Boost-users] [Iterators] Filtering and conversion
From: Ruediger Berlich (ruediger.berlich_at_[hidden])
Date: 2009-12-23 20:04:39

Hi there,

I'm playing with the iterators library at the moment. Say I have a
std::vector<boost::shared_ptr<base> > , with base being the base class of
derived1 and derived2. Pointers to derived1 and derived2 are stored in
arbitrary order in the vector.

Would it be possible to create an iterator that filters out the derived1
objects (including implicit conversion and possibly dereferencing of the
resulting shared_ptr<derivedx>) ?

So what I'm looking for is something like this:

main() {
  vector<base> x;
  for (int i=0; i<10; i++) {
        x.push_back(shared_ptr<derived1>(new derived1(i)));
        x.push_back(shared_ptr<derived2>(new derived2(i)));

  conversion_iterator<derived1> it(x.end());

  // ++it will skip over derived2 objects and set itself
  // to x.end() if no derived1 is remaining
  for(it=x.begin(); it!=x.end(); ++it) {
    // Implies implicit conversion and dereferencing
    // of shared_ptr<base>

The filter_iterator already does part of what I'd want here, but cannot help
with the conversion part.

I'm not asking for a solution to the above problem, just whether you think
this can be done at all. I had the impression that the iterators library
assumes that the types of the sequence being iterated over and the type
being returned are identical.

Also, while it=x.begin() can be handled by a conversion constructor.
it!=x.end() does not appear to be accessible with the predefined comparison

In any case thanks and have a nice Christmas time,

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at