1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Movable Handlers</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="move_objects.html" title="Movable I/O Objects"> 10<link rel="next" href="variadic.html" title="Variadic Templates"> 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="move_objects.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="variadic.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_handlers"></a><a class="link" href="move_handlers.html" title="Movable Handlers">Movable 28 Handlers</a> 29</h4></div></div></div> 30<p> 31 With C++11 and later, user-defined completion handlers are only required 32 to be move constructible, and are not required to be copy constructible. 33 </p> 34<p> 35 When move support is enabled, asynchronous that are documented as follows: 36 </p> 37<pre class="programlisting">template <typename Handler> 38void async_XYZ(..., Handler handler); 39</pre> 40<p> 41 are actually declared as: 42 </p> 43<pre class="programlisting">template <typename Handler> 44void async_XYZ(..., Handler&& handler); 45</pre> 46<p> 47 The handler argument is perfectly forwarded and the move construction occurs 48 within the body of <code class="computeroutput">async_XYZ()</code>. This ensures that all other 49 function arguments are evaluated prior to the move. This is critical when 50 the other arguments to <code class="computeroutput">async_XYZ()</code> are members of the handler. 51 For example: 52 </p> 53<pre class="programlisting">struct my_operation 54{ 55 unique_ptr<tcp::socket> socket; 56 unique_ptr<vector<char>> buffer; 57 ... 58 void operator(error_code ec, size_t length) 59 { 60 ... 61 socket->async_read_some(boost::asio::buffer(*buffer), std::move(*this)); 62 ... 63 } 64}; 65</pre> 66<p> 67 Move support is automatically enabled for <code class="literal">g++</code> 4.5 and 68 later, when the <code class="literal">-std=c++0x</code> or <code class="literal">-std=gnu++0x</code> 69 compiler options are used. It may be disabled by defining <code class="computeroutput">BOOST_ASIO_DISABLE_MOVE</code>, 70 or explicitly enabled for other compilers by defining <code class="computeroutput">BOOST_ASIO_HAS_MOVE</code>. 71 Note that these macros also affect the availability of <a class="link" href="move_objects.html" title="Movable I/O Objects">movable 72 I/O objects</a>. 73 </p> 74</div> 75<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 76<td align="left"></td> 77<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 78 Kohlhoff<p> 79 Distributed under the Boost Software License, Version 1.0. (See accompanying 80 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>) 81 </p> 82</div></td> 83</tr></table> 84<hr> 85<div class="spirit-nav"> 86<a accesskey="p" href="move_objects.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="variadic.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 87</div> 88</body> 89</html> 90