是什么在lambda函数使用自动自我(shared_from_this())变量的原因是什么? [英] What's the reason of using auto self(shared_from_this()) variable in lambda function?
问题描述
我读了升压ASIO http服务器例如code(请参阅http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/example/cpp11/http/server/connection.cpp)并找到自动自我(shared_from_this());
变量已经在捕捉范围内使用( [本,个体经营]
),但自变量没有在lambda函数使用。那么什么是这样做的好处?
I read the boost asio http server example code (see http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/example/cpp11/http/server/connection.cpp) and find the auto self(shared_from_this());
variable is been used in the capture scope ([this, self]
). But the self variable is not been used in the lambda function. Then what's the benefit of doing so?
推荐答案
这是为了确保连接
对象会超越异步操作完成拉姆达是活的(即异步。操作过程中),在连接
实例是活的为好。
This is done in order to make sure that connection
object outlives the asynchronous operation: as long as the lambda is alive (i.e. the async. operation is in progress), the connection
instance is alive as well.
这篇关于是什么在lambda函数使用自动自我(shared_from_this())变量的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!