在 lambda 函数中使用自动自(shared_from_this()) 变量的原因是什么?
What's the reason of using auto self(shared_from_this()) variable in lambda function?
我阅读了boost asio http服务器示例代码(请参阅 http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/example/cpp11/http/server/connection.cpp),发现auto self(shared_from_this());
变量已在捕获范围内使用([this, self]
)。但是 self 变量没有在 lambda 函数中使用。那么这样做有什么好处呢?
这样做
是为了确保connection
对象比异步操作更持久:只要 lambda 处于活动状态(即异步操作正在进行中),connection
实例也处于活动状态。
相关文章:
- Visual C++: MSVC vs. GCC+CLANG: 处理 lambda 捕获类成员变量,正确的方法是什么?
- 存储变量的更有效方法是什么?
- 变量 BitMask 在函数 CeilLog2 中的实际效果是什么?
- 这个变量在 C++ 中的范围是什么?
- 使用 gtest 框架在单元测试代码中检查目标对象的私有变量的最佳实践是什么?
- 将共享指针传递给函数参数 - 将其分配给局部变量的正确方法是什么
- 作为副本传递的 std::move()'d 变量的效果是什么?
- 在C++中,变量声明左侧的大括号是什么意思?
- 追问:C++14/C++17 中的变量到底是什么?
- 在多个对象中初始化预定义成员变量的正确方法是什么?
- C++ LibcURL IMAP 获取包含主题行的变量是什么?
- 使用与号后跟下划线命名的变量是什么意思?
- 这个变量是什么
- 在C++中,CMailItem::D isplay(VARIANT &Modal)它正在寻找的VARIANT 变量是什么?
- 里面的常量变量是什么?
- 在函数中破坏局部变量是什么意思
- "数据变量"是什么意思?
- 虽然中的"cin >>变量"是什么意思?
- &Type::变量是什么意思?
- 在知道变量是什么数据类型之前如何声明它