tags:

views:

183

answers:

2

I have:

class T {};

class S: public T {};

vector<T*> v;
vector<S*> w;

transform(v.begin(), v.end(), dynamic_cast_iterator<S*>(w.begin()));

But, of course, dynamic_cast_iterator doesn't exist.

+12  A: 

Here is one solution (using boost lambda):

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/casts.hpp>
#include <algorithm>
#include <iterator>
#include <iostream>

namespace bll = boost::lambda;

struct A { virtual ~A() { } };
struct B : A { void f() { std::cout << "hello, world" << std::endl; } };

int main() {
    std::vector<A*> a; a.push_back(new B);
    std::vector<B*> b;
    std::transform(a.begin(), a.end(), std::back_inserter(b), 
                   bll::ll_dynamic_cast<B*>(bll::_1));
    b[0]->f();
    delete a[0];
}
Johannes Schaub - litb
+1 neat solution!
Diego Sevilla
fantastic! Thanks.
Neil G
+3  A: 

You could write you own dynamic_cast_iterator using boost::transform_iterator. It has to be applied to the source iterators in a call to transform, though, not the target iterator.


#include <algorithm>
#include <vector>
#include <boost/iterator/transform_iterator.hpp>

//First define a unary functor object that dynamic_casts.
//(This is easier than using boost::lambda, because writing the type of
//boost::lambda::ll_dynamic_cast is fairly tricky.)
template <typename Target>
struct dynamic_caster
{
    typedef Target result_type;
    template <typename Source>
    Target operator()(Source s) const
    {
         return dynamic_cast<Target>(s);
    }
};

template <typename Target, typename Iterator>
boost::transform_iterator<dynamic_caster<Target>, Iterator>
make_dynamic_cast_iterator(Iterator it)
{
    return boost::make_transform_iterator(it, dynamic_caster<Target>());
}

//Then use like this:
int main()
{
    std::vector<T*> v; 
    //(Populate v)


    std::vector<S*> w;
    std::transform(make_dynamic_cast_iterator<S *>(v.begin()), 
              make_dynamic_cast_iterator<S *>(v.end()), 
              w.begin());
    //Or initialise a vector from v
    std::vector<S*> x(
        make_dynamic_cast_iterator<S *>(v.begin()), 
        make_dynamic_cast_iterator<S *>(v.end()));

}

Doug
this is also a good technique if I need to pass iterators around....
Neil G