Boost logo

Boost Users :

Subject: Re: [Boost-users] Question in usage of boost::bind
From: Roland Bock (rbock_at_[hidden])
Date: 2009-02-03 03:38:39


Alan M. Carroll wrote:
> 1) find_if expects a unary functor returning bool. You are passing a binary functor. _1 will be the element in the vector, but what did you think would be passed for _2?
>
> 2) CompareByVal should either be static or not have the pointer argument. There's no need to pass "this" by hand. The first argument to the bind of a method is an explicit "this" but the method is just a method, it is not aware that it has been called from Boost.bind.
>

Or, expressed in code, this is what you might want:

-----------------------------------------------------------
#include <algorithm>
#include <string>
#include <vector>
#include <boost/bind.hpp>

#include <algorithm>
#include <string>
#include <vector>

#include <boost/bind.hpp>

template<typename T> class A
{
         T _val;
public:
         A(T val) : _val(val){};
         T Val() {return _val;}

         const bool CompareByVal(const int iType, const T& ofval) const
         {
                 return ((iType == 1) && (_val == ofval));
         }
};

int main(int argc, char** argv) // My compiler did not like _TCHAR
{
         std::vector<A<int>*> a;

         std::find_if (a.begin(),
                        a.end(),
                        boost::bind(&A<int>::CompareByVal, _1, 1, 2));
}
---------------------------------------------

This finds those A objects with _val==2 under the condition that iType==1.

Btw, with _val being of type int* in the original example, I would
consider any found instances pretty dubious ;-)

Regards,

Roland


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net