在std :: find_if中使用boost绑定编译错误

时间:2013-09-17 16:20:39

标签: boost-bind c++

请考虑以下代码段:

#include <vector>
#include <algorithm>
#include <boost/function.hpp>
#include <boost/bind.hpp>

template<typename PODType>
class SomeClass
{
public:
    SomeClass() : m_pred(boost::bind(&SomeClass<PODType>::someMethodA, this, _1))
    {
    }

    bool someMethodA(const PODType& elem)
    {
        return false;
    }

    bool someMethodB(const std::vector<PODType>& vec)
    {
        return (std::find_if(vec.begin(), vec.end(), m_pred(_1)) != vec.end());
    }

private:
    boost::function<bool(PODType)> m_pred;
};


int main(int argc, char* argv[])
{
    SomeClass<int> obj;
    std::vector<int> v;
    obj.someMethodB(v);

    return 0;
}

编译器提供

error: no match for call to '(boost::function<bool(int)>) (boost::arg<1>&)'
note:   no known conversion for argument 1 from 'boost::arg<1>' to 'int'

代码行return (std::find_if(vec.begin(), vec.end(), m_pred(_1)));

我正在尝试在成员谓词中为someMethodA来电调用find_if

1 个答案:

答案 0 :(得分:3)

只需通过m_pred,无需m_pred(_1)