为什么升压例子再次呼吁`shared_from_this()`而不是使用关闭变量 [英] Why the boost example calls `shared_from_this()` again instead of using the closure variable

查看:213
本文介绍了为什么升压例子再次呼吁`shared_from_this()`而不是使用关闭变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在在<一个连接对象href=\"http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/example/cpp11/http/server/connection.cpp\"相对=nofollow>提振ASIO HTTP服务器的例子方法 do_read do_write shared_from_this()捕捉到地址的连接对象的寿命问题,请作为被pviously回答$ p $ 。它目前还不清楚为什么线67和88 code调用 shared_from_this()再次,而不是使用

  40自动自我(shared_from_this());
41 socket_.async_read_some(提高:: ASIO ::缓​​冲液(buffer_)
42本,个体经营](升压::系统::错误_ code EC,性病::为size_t bytes_transferred)
43 {
....
67 connection_manager_.stop(shared_from_this());
```


解决方案

有是没有实际的原因(我猜它只是从被重构,以C ++ 11年长风格C ++ 03例如吃剩的)。使用将是preferable,因为它已经无论如何抓获。

唯一的教育的原因,我能想到的可能是证明明确捕获被存储在lambda内,即使它的使用。

In the connection object at the boost asio HTTP server example in methods do_read and do_write the shared_from_this() is captured to address the connection object lifespan issue, as been answered previously. It is still not clear why on lines 67 and 88 the code calls shared_from_this() again, instead of using self:

40  auto self(shared_from_this());
41  socket_.async_read_some(boost::asio::buffer(buffer_),
42      [this, self](boost::system::error_code ec, std::size_t bytes_transferred)
43      {
....
67          connection_manager_.stop(shared_from_this());
```

解决方案

There is no practical reason for that (I guess it's just a leftover from older C++03 example that was refactored to C++11 style). Using self would be preferable, as it's already captured anyway.

The only "educational" reason I can think of could be to demonstrate that the explicitly captured self is stored within the lambda, even if it's unused.

这篇关于为什么升压例子再次呼吁`shared_from_this()`而不是使用关闭变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆