【问题标题】:Can I replace boost::bind with bind1st/2nd?我可以用 bind1st/2nd 替换 boost::bind 吗?
【发布时间】:2011-10-26 15:32:45
【问题描述】:

为了更好地理解,我可以将以下示例中对 boost::bind 的调用替换为 std::bind1st/2nd 吗?还是因为返回引用而无法实现?

示例(缩短):

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 );
...

【问题讨论】:

    标签: c++ boost bind bind2nd


    【解决方案1】:

    这是不可能的,因为 bind1stbind2nd 都不会像 bind 那样重载 operator==(以产生另一个函子)。如果不想使用bind,则需要自己编写仿函数,或者使用lambda。

    【讨论】:

    • 谢谢。在使用函数对象进行测量后,结果证明对于目标系统来说是最快的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 2011-07-17
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 2018-09-15
    • 2016-04-13
    相关资源
    最近更新 更多