I'm having some difficulty passing parameters to boost::bind. The approach taken by ASIO is to pass a placeholder to bind, like so: hostResolver->async_resolve(*nameQuery, boost::bind(&DClient::resolveHandler, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator)); This works fine if I want to use an error or iterator as a parameter. 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? I looked in ASIO's placeholders.hpp file, and I must confess I can't make sense of it at all! Any help here is appreciated.
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, 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. 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: [slides] http://objectmodelingdesigns.com/boostcon10/ [video] http://blip.tv/boostcon/michael-caisse-an-asio-based-flash-xml-server-419035... hth - michael -- Michael Caisse Object Modeling Designs www.objectmodelingdesigns.com
That wont work. Those are placeholders and the caller doesn't have the caller "primitive variable, or a pointer to one of my classes".
Well, my understanding was that the OP wanted to define and bind another function, analogous to asio handlers, with arguments of different types... If he means to pass additional parameters to the real asio handlers, then, certainly, I was wrong.
participants (3)
-
David McCallum
-
Igor R
-
Michael Caisse