I’d like to use the boost::transform_iterator together with boost::bind to return the result of a member function.
e.g.
class Foo
{
public:
//...
Bar& getBar();
const Bar& getBar() const;
};
I’ve got a unary Function object to select the getBar() function
struct getBar: public std::unary_function<Foo&,Bar&>
{
getBar::result_type operator()(getBar::argument_type arg ) const {
return arg.getBar()
}
};
and lets say i’ve stored several Foo objects inside a std::vector and Im using a tranform_iterator like that
int main()
{
typedef std::vector<Foo> VEC;
typedef boost::transform_iterator<getBar,VEC::iterator> iterator;
VEC vec;
vec.push_back( Foo ());
iterator i( vec.begin() );
//...
Bar = *i;
return 0;
};
But if i want to use boost::bind instead of the getBar functor how would i do that. I’m not sure which template parameter I would have to pass to the transform_iterator.
EDIT:
the solution with boost::function was a good start but i wasn’t completely satisfied so experimented a bit and looked into the return type of boost::mem_fn
typedef boost::transform_iterator<boost::_mfi::mf0<Bar&,Foo>,VEC::iterator> iter;
typedef boost::transform_iterator<boost::_mfi::cmf0<const Bar&,Foo>,VEC::const_iterator> citer;
but this solution has another problem.
because
iter i(v.begin(), boost::mem_fn( &Foo::getBar ));
citer ci(v.begin(), boost::mem_fn( &Foo::getBar ));
leads to the following error
call of overloaded mem_fn(<unresolved overloaded function type>) is ambiguous
the compiler is not able to identify which getBar function is requested and I had to help him a little.
iter i(v.begin(), boost::mem_fn( static_cast<Bar& (Foo::*)()>(&Foo::getBar) ));
citer ci(v.begin(), boost::mem_fn( static_cast<const Bar& (Foo::*)() const >(&Foo::getBar) ));
this is probably not faster than writing a little functor by hand but at least it helped me to understand boost::mem_fn a bit more.
1 Answer