| Home | Libraries | People | FAQ | More

#### Movable I/O Objects

When move support is available (via rvalue references), Boost.Asio allows move construction and assignment of sockets, serial ports, POSIX descriptors and Windows handles.

Move support allows you to write code like:

    tcp::socket make_socket(io_service& i)
    {
      tcp::socket s(i);
      ...
      std::move(s);
    }

or:

    class connection : public enable_shared_from_this<connection>
    {
    private:
      tcp::socket socket_;
      ...
    public:
      connection(tcp::socket&& s) : socket_(std::move(s)) {}
      ...
    };

    ...

    class server
    {
    private:
      tcp::acceptor acceptor_;
      tcp::socket socket_;
      ...
      void handle_accept(error_code ec)
      {
        if (!ec)
          std::make_shared<connection>(std::move(socket_))->go();
        acceptor_.async_accept(socket_, ...);
      }
      ...
    };

as well as:

    std::vector<tcp::socket> sockets;
    sockets.push_back(tcp::socket(...));

A word of warning: There is nothing stopping you from moving these objects while there are pending asynchronous operations, but it is unlikely to be a good idea to do so. In particular, composed operations like async_read() store a reference to the stream object. Moving during the composed operation means that the composed operation may attempt to access a moved-from object.

Move support is automatically enabled for `g++` 4.5 and later, when the `-std=c++0x` or `-std=gnu++0x` compiler options are used. It may be disabled by defining `BOOST_ASIO_DISABLE_MOVE`, or explicitly enabled for other compilers by defining `BOOST_ASIO_HAS_MOVE`. Note that these macros also affect the availability of movable handlers.