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 <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;} 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<int>*> a; std::find_if (a.begin(), a.end(), boost::bind(&A<int>::CompareByVal, _1, _2, 1,1)); }

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<int>*> 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 <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
participants (3)
-
Alan M. Carroll
-
Archie14
-
Roland Bock