
Alan M. Carroll <amc <at> network-geographics.com> writes:
That's not a good usage scenario for bind - you should just call the method
want to either (1) pass the functor to something else or (2) store the functor for future use. Conceptually, as a rough guide, you should think of the result of bind as a
naturally, the result of bind of anything isn't going to be equal to 1, just
equal to one. Instead you want to compare the result of invoking / dereferencing the functor, which is done with the function operator (). So your example would work if you added that, e.g.
return (boost::bind (&B::getA::test, b)() ==1);
But a more standard use case would look like (untested)
template < typename F > bool check_property(F const& func, B const& b) { return func(b); }
bool testbind() { B b; return check_property(boost::bind(&A::test, boost::bind(&B::getA, _1)) == 1, b); }
The == operator of a bind result and a int creates a functor that compares
directly. Bind is useful when you pointer, not a value. So, like you won't have a pointer the result of invoking the functor
to that int. This passes that to check_property which invokes it with the function operator.
P.S. If you want to store a Bind result for later use, look at Boost.Function. These two libraries are duals of each other - Bind to *pass* function objects to somebody else, Function to *receive* function objects from somebody else.
At 04:33 PM 3/4/2009, you wrote:
I cannot figure out how to use boost::bind in following scenario:
class A { int i_; public: int test() {return i;} };
class B { A _a; public: A& getA() {return _a;} }
bool testbind() { B b;
// here I am trying to find out if b.getA().test() equals 1 // I understand that B::getA::test is incorrect, and that's my question- // how to use bind here? return (boost::bind (&B::getA::test, b) ==1); }
_______________________________________________ Boost-users mailing list Boost-users <at> lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thank you for the quick reply and comments that I agree with. In the meantime I managed to make it work with double bind. When I looked at your suggestion I tried to implement it. Unfortunately I am getting compiled errors. Here is the complete test example (vc2008): #include "stdafx.h" #include <sstream> #include <algorithm> #include <vector> #include <boost\bind.hpp> class A { int _i; public: A(int val) : _i(val){} A(const A& another) : _i(another._i){} int value () {return _i;} }; class B { A _a; public: B (int val) : _a(val){}; B (const B& another) : _a(another._a) {} A& getA () {return _a;} }; int _tmain(int argc, _TCHAR* argv[]) { std::vector<B> lstb; lstb.push_back(B(1)); std::vector<B>::iterator it = std::find_if (lstb.begin(), lstb.end(), boost::bind (&B::getA::value, _1)() == 1); if (it == lstb.end()) return 1; return 0; } Compiler errors are: error C3083: 'getA': the symbol to the left of a '::' must be a type error C2039: 'value' : is not a member of 'B' see declaration of 'B'