Boost logo

Boost Users :

From: Dave Slutzkin (daveslutzkin_at_[hidden])
Date: 2005-07-28 01:21:58


On Thu, 28 Jul 2005 13:22:28 +1200, "Oleg Smolsky"
<oleg.smolsky_at_[hidden]> said:
> I've just started using boost::bind and found the following problem:
>
> class Handler
> {
> public:
> bool Test1(std::string sComponent);
> bool Test2(std::string sComponent, std::string sAddress);
> };
>
> void Test()
> {
> std::vector<Handler> v;
>
> // This compiles
> std::string a, b;
> std::for_each(v.begin(), v.end(),
> boost::bind(&Handler::Test1, _1, a));

This creates a functor with one argument, _1, which calls Handler::Test1
with _1 as this and a as argument 1 of Test1.

> // This doesn't compile
> //std::for_each(v.begin(), v.end(),
> // boost::bind(&Handler::Test2, _1, _2, a, b));

This creates a functor with two arguments, _1 and _2, which calls
Handler::Test2 with _1 as this, _2 as argument 1 of Test2, a as argument
2 of Test2 and b as argument 3 of Test2. But Test2 only has two
arguments, and for_each only takes a functor with one argument.

I think what you want is:

std::for_each(v.begin(), v.end(),
              boost::bind(&Handler::Test2, _1, a, b));

To create a functor with one argument, _1, which calls Handler::Test2
with _1 as this, a as argument 1 of Test2 and b as argument 2 of Test2.

Hope that helps,

Dave.

-- 
  Dave Slutzkin
  Melbourne, Australia
  daveslutzkin_at_[hidden]

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