【问题标题】:Why the boost example calls `shared_from_this()` again instead of using the closure variable为什么 boost 示例再次调用 `shared_from_this()` 而不是使用闭包变量
【发布时间】:2015-04-13 19:10:36
【问题描述】:

boost asio HTTP server example 方法do_readdo_write 的连接对象中,shared_from_this() 被捕获以解决连接对象寿命问题as been answered previously。目前尚不清楚为什么在第 67 行和第 88 行代码再次调用shared_from_this(),而不是使用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());
```

【问题讨论】:

    标签: boost boost-asio


    【解决方案1】:

    没有实际的原因(我猜这只是旧 C++03 示例的剩余部分,被重构为 C++11 样式)。最好使用self,因为它已经被捕获了。

    我能想到的唯一“教育”原因可能是证明显式捕获的 self 存储在 lambda 中,即使它未使用。

    【讨论】:

      猜你喜欢
      • 2018-10-20
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      相关资源
      最近更新 更多