Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I dynamically cast between vectors of pointers?

Tags:

c++

stl

boost

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.

like image 556
Neil G Avatar asked Dec 05 '22 06:12

Neil G


2 Answers

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];
}
like image 200
Johannes Schaub - litb Avatar answered Dec 07 '22 18:12

Johannes Schaub - litb


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()));

}

like image 26
Doug Avatar answered Dec 07 '22 18:12

Doug