I have a std::vector of Boost shared pointers to objects, and would like to get a vector of shared pointers to the same objects casted down to a more specific type:
//using boost::shared_ptr, std::vector;
vector<shared_ptr<A> > originalVec;
vector<shared_ptr<B> > targetVec( originalVec.size() ); // B extends A
For a single element shared_ptr<A> elem
, it is possible to cast it using boost::static_pointer_cast<B>(elem)
, whose syntax is reproduced below (from Boost shared_ptr doc):
template<class T, class U>
shared_ptr<T> static_pointer_cast(shared_ptr<U> const & r); // never throws
I don't know how to use it with std::transform
. Mi tries include:
//using boost::bind, boost::static_pointer_cast
transform( originalVec.begin(), originalVec.end(), targetVec.begin(), bind( static_pointer_cast<B>, _1) )
transform( originalVec.begin(), originalVec.end(), targetVec.begin(), bind( static_pointer_cast<B,A>, _1) )
Getting in both cases a "no matching function for call to bind(<unresolved overloaded function type>, boost::arg<1>& )"
Any ideas?
EDIT: The problem could be related with an ambiguity, since a similar function template is defined for intrusive pointer class, with syntax:
template<class T, class U>
intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & r); // never throws
The question, in this case, is how to specify the type of the first argument so that the compiler knows which method to select.