我可以用 bind1st/2nd 替换 boost::bind 吗?
为了更好地理解,我可以用 std::bind1st/2nd 替换以下示例中对 boost::bind 的调用吗?或者因为返回引用而无法实现?
示例(缩短):
class Pos
{
public:
bool operator==( const Pos& );
...
}
class X
{
public:
const Pos& getPos() { return m_p; }
...
private:
Pos m_p;
}
...
Pos position;
std::vector<X> v;
std::vector<X>::iterator iter;
...
iter = std::find_if( v.begin(), v.end(), boost::bind( &X::getPos, _1 ) == position );
...
Just for better understanding, can I replace the call to boost::bind in the following example with std::bind1st/2nd? Or is it not possible because of returning a reference?
Example(shortened):
class Pos
{
public:
bool operator==( const Pos& );
...
}
class X
{
public:
const Pos& getPos() { return m_p; }
...
private:
Pos m_p;
}
...
Pos position;
std::vector<X> v;
std::vector<X>::iterator iter;
...
iter = std::find_if( v.begin(), v.end(), boost::bind( &X::getPos, _1 ) == position );
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是不可能的,因为
bind1st
和bind2nd
都不会像bind
那样重载operator==
(以产生另一个函子) 。如果你不想使用bind
,你需要自己编写函子,或者使用lambda。It's not possible, because neither
bind1st
norbind2nd
overloadsoperator==
likebind
does (to yield another functor). If you don't want to usebind
, you need to write the functor yourself, or use a lambda.