Lets get a bit more specific about what I'm up to. I have an abstract Optimizer class defining an interface for various algorithms. It works by producing and consuming unknown quantities of this data-structures called Request, hence I let it use vectors of them.
class Request {
public:
vector<double> x;
double y;
int id;
// [...]
}
class Asynch_Optimizer {
public:
virtual int generate_requests( vector<Request> & work ) = 0;
virtual int assimilate_results( const vector<Request> & result ) = 0;
protected:
virtual void convergence_test( const vector<Request> & population );
// [...]
}
It turns out that a Particle_Swarm_Optimizer can use some additional fields in the Request. (I smell my fallacy here, it uses the additional fields only internally. For the Request processing machinery it has no meaning. I sub-classed to Particle just to keep everything nicely bound together)
class Particle : public Request {
public:
vector<double> v;
vector<double> x_min;
double min_value;
// [...]
}
Now I want to call the common convergence test method, implemented in the super-class from Particle_Swarm_Optimizer, where the data is in vector of Particle. Is there a more efficient way to convert to vector of Request than the construction of a new vector and copy-casting each element individually:
void opti::Particle_Swarm_Optimizer::convergence_test( const vector<Particle> & population )
{
//TODO remove slow hack
vector<Request> cast_up;
for (size_t i = 0; i < population.size(); i++) {
cast_up.push_back( population[i] );
}
Asynch_Optimizer::convergence_test( cast_up );
}
I assume there are better ways to structure the data for this example. Still I'm curious if there exists a way to upcast the template-type of the container?