
Hi, I found this example for use the async_accept() method: void accept_handler(const boost::system::error_code& error) { if (!error) { // Accept succeeded. } } ... boost::asio::ip::tcp::acceptor acceptor(io_service); ... boost::asio::ip::tcp::socket socket(io_service); acceptor.async_accept(socket, accept_handler); It is possible use the async_accept with a (c++0x) lambda function instead of the function accept_handler()? -- View this message in context: http://boost.2283326.n4.nabble.com/ASIO-async-accept-and-c-2011-lambas-tp369... Sent from the Boost - Users mailing list archive at Nabble.com.

Hi Claude!
Yes, it posible.
acceptor.async_accept(socket,
[](const boost::system::error_code& e) {
}
);
niXman.
2011/7/27 Claude
Hi, I found this example for use the async_accept() method:
void accept_handler(const boost::system::error_code& error) { if (!error) { // Accept succeeded. } }
...
boost::asio::ip::tcp::acceptor acceptor(io_service); ... boost::asio::ip::tcp::socket socket(io_service); acceptor.async_accept(socket, accept_handler);
It is possible use the async_accept with a (c++0x) lambda function instead of the function accept_handler()?
-- View this message in context: http://boost.2283326.n4.nabble.com/ASIO-async-accept-and-c-2011-lambas-tp369... Sent from the Boost - Users mailing list archive at Nabble.com. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

I found this example for use the async_accept() method:
void accept_handler(const boost::system::error_code& error) { if (!error) { // Accept succeeded. } }
...
boost::asio::ip::tcp::acceptor acceptor(io_service); ... boost::asio::ip::tcp::socket socket(io_service); acceptor.async_accept(socket, accept_handler);
It is possible use the async_accept with a (c++0x) lambda function instead of the function accept_handler()?
Sure, you can. ...async_accept(socket, [=] (const boost::system::error_code &error) {doSomething();}); Or, in a more readable form: auto handler = [=] (const boost::system::error_code &error) { doSomething(); }; ...async_accept(socket, handler);

Thanks! But it is possible to use the "error" variable outside the lambda functions? I would like to get something like this: async_accept(socket, [=] (const boost::system::error_code &error) { // }); if (!error) { //here there are a valid connection } -- View this message in context: http://boost.2283326.n4.nabble.com/ASIO-async-accept-and-c-2011-lambas-tp369... Sent from the Boost - Users mailing list archive at Nabble.com.

I would like to get something like this:
async_accept(socket, [=] (const boost::system::error_code &error) { // });
if (!error) { //here there are a valid connection }
"error" is just an argument of the anonymous function (lambda), and it's accessible only inside that function (like any other function argument). What do you try to achieve?

Ok..I want to avoid using other functions. It is possible t copy the value of error in a lambda external variable? const boost::system::error_code myError; acceptor.async_accept(*socket,[&myError] (const boost::system::error_code &error) { myError = error; //??? } ); -- View this message in context: http://boost.2283326.n4.nabble.com/ASIO-async-accept-and-c-2011-lambas-tp369... Sent from the Boost - Users mailing list archive at Nabble.com.

It is possible t copy the value of error in a lambda external variable?
const boost::system::error_code myError; acceptor.async_accept(*socket,[&myError] (const boost::system::error_code &error) { myError = error; //??? } );
Yes, you can capture a global object by reference and modify it (if it non-const) inside a lambda. But this is realy out of boost scope. For details, please read the following: http://msdn.microsoft.com/en-us/library/dd293608.aspx http://msdn.microsoft.com/en-us/library/dd293603.aspx

On Wednesday, July 27, 2011 5:19 AM, Claude wrote:
But it is possible to use the "error" variable outside the lambda functions?
I would like to get something like this:
async_accept(socket, [=] (const boost::system::error_code &error) { // });
if (!error) { //here there are a valid connection }
I'm not sure this can be made to do what you want. When async_accept returns, you do NOT have a valid connection. The connection will actually be accepted long after async_accept returns. By "long after", I mean any time span from milliseconds to weeks. You truly want to test the "error" variable inside the lambda because it is highly likely that whatever function originally called async_accept has already returned and gone out of scope.
participants (4)
-
Andrew Holden
-
Claude
-
Igor R
-
niXman