Re: [Boost-users] Using Boost::Bind
I should clarify, this is what I want to achieve:
boost::function
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, boost::asio::placeholders::error, 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.
If I understand you correctly, then in the following code:
void DClient::resolveHandler(int param)
{
....
}
boost::function
Oops, that should have been:
void DClient::resolveHandler(int param)
{
....
}
boost::function
void DClient::resolveHandler(int param) { .... }
boost::function
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?
Correct, the function will be called with 12. You can ensure this with
this small program:
#include <iostream>
#include
On 06/29/2011 09:32 AM, David McCallum wrote:
Oops, that should have been:
void DClient::resolveHandler(int param) { .... }
boost::function
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); functionPtr(67); 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 -- Michael Caisse Object Modeling Designs www.objectmodelingdesigns.com
Aha! I had read through the Boost::bind documentation several times, but somehow failed to realize that the _1 and _2 referred to parameter positions in the function signature. Now everthing works as it should. Thank you very much!
participants (3)
-
David McCallum
-
Igor R
-
Michael Caisse