Boost logo

Boost Users :

Subject: Re: [Boost-users] Using Boost::Bind
From: Michael Caisse (boost_at_[hidden])
Date: 2011-06-29 12:57:22

On 06/29/2011 09:32 AM, David McCallum wrote:
> Oops, that should have been:
> void DClient::resolveHandler(int param)
> {
> ....
> }
> boost::function<void (int)> functionPtr;
> int tempInt = 12;
> functionPtr = boost::bind(&DClient::resolveHandler, this, tempInt);
> functionPtr(67);
> The final line will call resolveHandler with a parameter of 12, rather
> than 67? Surely that can't be right?
> _______________________________________________

It is absolutely correct. You have bound a value to the first argument.

If you want to bind a 'placeholder' based on when the bound functor is
called then you use a placeholder:

functionPtr = boost::bind(&DClient::resolveHandler, this, _1);

This would call DClient::resolveHandler with 67.

In the Asio slides I previously linked to I have about 3-4 example
slides that provide various bind constructs to help show how bind works.

When you bind a method you are providing all the arguments that will be
used at call time. Some of those arguments might be values or references
and others might actually be placeholders to be determined at call time.

For example:

functionPtr = boost::bind(&DClient::resolveHandler, this, _2);
functionPtr(67,42,"hi mom");

would result in resolveHandler getting called with 42 because the
placeholder _2 says to use the second parameter at the call location.

Bind is a powerful concept. Take some time to look at the bind docs.

hth -

Michael Caisse
Object Modeling Designs

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