Boost logo

Boost Users :

Subject: Re: [Boost-users] Using Boost::Bind
From: Michael Caisse (boost_at_[hidden])
Date: 2011-06-28 19:10:59

On 06/28/2011 03:38 PM, Igor R wrote:
>> But what if I want to use a primitive variable, or a pointer to one of
>> my classes? Do I need to make a placeholder for each type of parameter
>> I want to use?
> No, you can use bind placeholders _1, _2, _3 etc.
> _______________________________________________

That wont work. Those are placeholders and the caller doesn't have the
caller "primitive variable, or a pointer to one of my classes".

David -

You will want to learn a bit more about Boost.Bind. You can bind your
pointer or variable at the bind call. Parameters at the time of bind are
copied by value. If you need a reference use the boost::ref wrapper.

Expanding your example:

void DClient::resolveHandler( const boost::system::error_code& error,
                               int value );


int special_number = 42;

hostResolver->async_resolve( *nameQuery,
                              boost::bind(&DClient::resolveHandler, this,
                                          special_number ) );


In the above, when the handler is invoked it will pass the error value
(via the placeholder) and the value of special_number during the bind...
which is 42.

You should also take a look at using shared_from_this with async
handlers to make lifetime management easier.

More tips can be found in my BoostCon'10 Asio talk:


hth -

Michael Caisse
Object Modeling Designs

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at