Question

Consider this snippet:

#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;
}

The compiler gives

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'

for the line return (std::find_if(vec.begin(), vec.end(), m_pred(_1)));

I'm trying to call someMethodA within the member predicate for the find_if calls.

Was it helpful?

Solution

Just pass m_pred, no need for m_pred(_1).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top