Question in usage of boost::bind
Below is an example that fails to compile. I have a vector of pointers to a
templated class and I need to use find_if. My understanding of boost::bind. is
that the first argument, when binding member function, is an explicit "this"
pointer,
that should be A* in my case.
Any help in explaining what I am doing wrong will be greatly appreciated.
#include <algorithm>
#include <string>
#include <vector>
#include
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. At 12:29 PM 2/2/2009, you wrote:
Below is an example that fails to compile. I have a vector of pointers to a templated class and I need to use find_if. My understanding of boost::bind. is that the first argument, when binding member function, is an explicit "this" pointer, that should be A* in my case.
Any help in explaining what I am doing wrong will be greatly appreciated.
template<typename T> class A { T _val; public: A(T val) : _val(val){}; T Val() {return _val;}
bool CompareByVal(A<T>* a, int iType, T ofval) { return ( (a != NULL) && (iType == 1) && (a->Val() == ofval) ); } };
int _tmain(int argc, _TCHAR* argv[]) { std::vector a;
std::find_if (a.begin(), a.end(), boost::bind(&A<int>::CompareByVal, _1, _2, 1,1)); }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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
participants (3)
-
Alan M. Carroll
-
Archie14
-
Roland Bock