vendredi 17 avril 2015

Using auto verses typedef for lambda inside asio reactor

I have some code like this:



typedef std::function<void (void)> const& basic_callback_t;

void Receive::async_basic(fragmenting_socket& socket, const ID id, basic_callback_t fn)
{
int buffer[1024];
socket.async_receive(buffer, sizeof(buffer), [&](const boost::system::error_code& ec, size_t bytes)
{
....

if (fn)
{
fn();
} else
{
THROW("async_receive callback: Could not call fn()");
}
});

}


and socket.async_receive() is calling boost::asio::ip::udp::socket::async_receive_from()


If I call Receive::async_basic() with fn as functor declared like:



protocol::basic_callback_t f = [&] () ...
protocol::Receive::async_basic(m_camera_socket, protocol::ID::QUERY, f);


everything works ok and f gets called. However, if I pass an anonymous lambda into async_basic or declare f as



auto f = [&] () ....


then the asio reactor treats fn inside async_receive as out of scope and the THROW() statement is called. Does anyone know why this might be happening?


I'm compiling with g++ 4.8.2 with the -std=c++11 flag set and -fPIC.


Cheers Ben


Aucun commentaire:

Enregistrer un commentaire