boost::transform_iterator 和 boost::bind
我想将 boost::transform_iterator 与 boost::bind 一起使用来返回成员函数的结果。
例如,
class Foo
{
public:
//...
Bar& getBar();
const Bar& getBar() const;
};
我有一个一元 Function 对象来选择 getBar() 函数
struct getBar: public std::unary_function<Foo&,Bar&>
{
getBar::result_type operator()(getBar::argument_type arg ) const {
return arg.getBar()
}
};
,可以说我已经在 std::vector 中存储了几个 Foo 对象,并且我使用这样的 tranform_iterator
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;
};
但如果我想使用 boost::bind 代替getBar 函子的我该怎么做。我不确定必须将哪个模板参数传递给transform_iterator。
编辑:
boost::function 的解决方案是一个好的开始,但我并不完全满意,因此进行了一些实验并研究了 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;
,但这个解决方案还有另一个问题。 因为
iter i(v.begin(), boost::mem_fn( &Foo::getBar ));
citer ci(v.begin(), boost::mem_fn( &Foo::getBar ));
会导致以下错误,
call of overloaded mem_fn(<unresolved overloaded function type>) is ambiguous
编译器无法识别请求的是哪个 getBar 函数,我不得不帮助他一点。
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) ));
这可能并不比手工编写一个小函子快,但至少它帮助我更多地理解 boost::mem_fn 。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)