对 boost asio 完成处理程序的右值引用

rvalue reference to boost asio completion handler

在 boost::asio 中,是否可以在 C++11 中使用移动语义和右值引用模拟来创建和实现完成处理程序?

我的尝试如下,但我不明白我收到错误的原因:

Class定义:

struct SocketTest : std::enable_shared_from_this<SocketTest> {


  SocketTest(boost::asio::ip::udp::socket socket):socket_(std::move(socket)){}

  template<typename Handler>
  void async_receive(dummy dummy,Handler&& handler){
    auto self(shared_from_this());

    socket_.async_receive(boost::asio::buffer(buf_), 
       std::bind([this,self](
          boost::system::error_code ec, 
          std::size_t bytes_transferred, 
          Handler&& moved_handler
       ){
           moved_handler(ec,bytes_transferred);
        }, std::move(handler)));

  }

  std::array<char,max_length> buf_;
  boost::asio::ip::udp::socket socket_;
};

调用class的例子:

SocketTest socket_test(std::move(s));

socket_test.async_receive(dummy(), []( boost::system::error_code ec, std::size_t bytes_transferred){
    // many cool things accomplished!
});

复制处理程序的以下版本似乎可以正常工作,但我有兴趣了解如何避免该复制:

struct SocketTest : std::enable_shared_from_this<SocketTest> {


  SocketTest(boost::asio::ip::udp::socket socket):socket_(std::move(socket)){}

  template<typename Handler>
  void async_receive(dummy dummy,Handler handler){
    auto self(shared_from_this());

    socket_.async_receive(boost::asio::buffer(buf_), [this,self,handler](boost::system::error_code ec, std::size_t bytes_transferred){
       handler(ec,bytes_transferred);

    });

  }

  std::array<char,max_length> buf_;
  boost::asio::ip::udp::socket socket_;
};

The full source and errors can be found here

您不能 bind 到采用右值引用的可调用对象,因为当 bind 保存其参数时,它不再是右值。在您的情况下,您可以让它按值接受 Handler (这是存储处理程序的最终位置)。您可以查看 this question 了解更多详情。

此外,您忘记为 bind 提供占位符:

socket_.async_receive(boost::asio::buffer(buf_), 
   std::bind([this,self](
      boost::system::error_code ec, 
      std::size_t bytes_transferred, 
      Handler moved_handler
   // ^^^^^^^ passing by value 
   ){
       moved_handler(ec,bytes_transferred);
    }, std::placeholders::_1, std::placeholders::_2, std::move(handler)));
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ placeholders added