Boost logo

Boost Users :

From: Stuart Dootson (stuart.dootson_at_[hidden])
Date: 2005-07-28 01:51:36


On 7/28/05, Oleg Smolsky <oleg.smolsky_at_[hidden]> wrote:
> Hi all,
>
> 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 doesn't compile
> //std::for_each(v.begin(), v.end(),
> // boost::bind(&Handler::Test2, _1, _2, a, b));
> }
>
> Environment: boost 1.32, VC8, WinXP.
>
> Any ideas?
>
> Thanks,
> Oleg.

Oleg - the _1 and _2 represent the parameter(s) passed *to* the
boost::bind function by the caller (in this case std::for_each, which
passes a Handler object instance). Now, std::for_each only passes one
argument, so there can't be a _2 here.

What you want for the second one is:

boost::bind(&Handler::Test2, _1, a, b)

The main thing to remember is that boost::bind makes explicit the
implicit extra argument to object methods (the object instance, or
'this').

HTH
Stuart Dootson


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