• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Movable I/O Objects</title>
5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../boost_asio.html" title="Boost.Asio">
8<link rel="up" href="../cpp2011.html" title="C++ 2011 Support">
9<link rel="prev" href="../cpp2011.html" title="C++ 2011 Support">
10<link rel="next" href="move_handlers.html" title="Movable Handlers">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="../cpp2011.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../cpp2011.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="move_handlers.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h4 class="title">
27<a name="boost_asio.overview.cpp2011.move_objects"></a><a class="link" href="move_objects.html" title="Movable I/O Objects">Movable I/O
28        Objects</a>
29</h4></div></div></div>
30<p>
31          When move support is available (via rvalue references), Boost.Asio allows
32          move construction and assignment of sockets, serial ports, POSIX descriptors
33          and Windows handles.
34        </p>
35<p>
36          Move support allows you to write code like:
37        </p>
38<pre class="programlisting">tcp::socket make_socket(io_context&amp; i)
39{
40  tcp::socket s(i);
41  ...
42  std::move(s);
43}
44</pre>
45<p>
46          or:
47        </p>
48<pre class="programlisting">class connection : public enable_shared_from_this&lt;connection&gt;
49{
50private:
51  tcp::socket socket_;
52  ...
53public:
54  connection(tcp::socket&amp;&amp; s) : socket_(std::move(s)) {}
55  ...
56};
57
58...
59
60class server
61{
62private:
63  tcp::acceptor acceptor_;
64  ...
65  void handle_accept(error_code ec, tcp::socket socket)
66  {
67    if (!ec)
68      std::make_shared&lt;connection&gt;(std::move(socket))-&gt;go();
69    acceptor_.async_accept(...);
70  }
71  ...
72};
73</pre>
74<p>
75          as well as:
76        </p>
77<pre class="programlisting">std::vector&lt;tcp::socket&gt; sockets;
78sockets.push_back(tcp::socket(...));
79</pre>
80<p>
81          A word of warning: There is nothing stopping you from moving these objects
82          while there are pending asynchronous operations, but it is unlikely to
83          be a good idea to do so. In particular, composed operations like <a class="link" href="../../reference/async_read.html" title="async_read">async_read()</a> store a reference
84          to the stream object. Moving during the composed operation means that the
85          composed operation may attempt to access a moved-from object.
86        </p>
87<p>
88          Move support is automatically enabled for <code class="literal">g++</code> 4.5 and
89          later, when the <code class="literal">-std=c++0x</code> or <code class="literal">-std=gnu++0x</code>
90          compiler options are used. It may be disabled by defining <code class="computeroutput">BOOST_ASIO_DISABLE_MOVE</code>,
91          or explicitly enabled for other compilers by defining <code class="computeroutput">BOOST_ASIO_HAS_MOVE</code>.
92          Note that these macros also affect the availability of <a class="link" href="move_handlers.html" title="Movable Handlers">movable
93          handlers</a>.
94        </p>
95</div>
96<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
97<td align="left"></td>
98<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M.
99      Kohlhoff<p>
100        Distributed under the Boost Software License, Version 1.0. (See accompanying
101        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
102      </p>
103</div></td>
104</tr></table>
105<hr>
106<div class="spirit-nav">
107<a accesskey="p" href="../cpp2011.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../cpp2011.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="move_handlers.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
108</div>
109</body>
110</html>
111