boost/doc/html/move/introduction.html
2021-10-05 21:37:46 +02:00

102 lines
8.3 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Introduction</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../move.html" title="Chapter 25. Boost.Move">
<link rel="prev" href="what_is_boost_move.html" title="What is Boost.Move?">
<link rel="next" href="implementing_movable_classes.html" title="Implementing copyable and movable classes">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="what_is_boost_move.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../move.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="implementing_movable_classes.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="move.introduction"></a><a class="link" href="introduction.html" title="Introduction">Introduction</a>
</h2></div></div></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
The first 3 chapters are the adapted from the article <a href="http://www.artima.com/cppsource/rvalue.html" target="_top"><span class="emphasis"><em>A
Brief Introduction to Rvalue References</em></span></a> by Howard E. Hinnant,
Bjarne Stroustrup, and Bronek Kozicki
</p></td></tr>
</table></div>
<p>
Copying can be expensive. For example, for vectors <code class="computeroutput"><span class="identifier">v2</span><span class="special">=</span><span class="identifier">v1</span></code> typically
involves a function call, a memory allocation, and a loop. This is of course
acceptable where we actually need two copies of a vector, but in many cases,
we don't: We often copy a <code class="computeroutput"><span class="identifier">vector</span></code>
from one place to another, just to proceed to overwrite the old copy. Consider:
</p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">b</span><span class="special">)</span>
<span class="special">{</span>
<span class="identifier">T</span> <span class="identifier">tmp</span><span class="special">(</span><span class="identifier">a</span><span class="special">);</span> <span class="comment">// now we have two copies of a</span>
<span class="identifier">a</span> <span class="special">=</span> <span class="identifier">b</span><span class="special">;</span> <span class="comment">// now we have two copies of b</span>
<span class="identifier">b</span> <span class="special">=</span> <span class="identifier">tmp</span><span class="special">;</span> <span class="comment">// now we have two copies of tmp (aka a)</span>
<span class="special">}</span>
</pre>
<p>
But, we didn't want to have any copies of a or b, we just wanted to swap them.
Let's try again:
</p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span><span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">b</span><span class="special">)</span>
<span class="special">{</span>
<span class="identifier">T</span> <span class="identifier">tmp</span><span class="special">(::</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">a</span><span class="special">));</span>
<span class="identifier">a</span> <span class="special">=</span> <span class="special">::</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">b</span><span class="special">);</span>
<span class="identifier">b</span> <span class="special">=</span> <span class="special">::</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">tmp</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
This <code class="computeroutput"><span class="identifier">move</span><span class="special">()</span></code>
gives its target the value of its argument, but is not obliged to preserve
the value of its source. So, for a <code class="computeroutput"><span class="identifier">vector</span></code>,
<code class="computeroutput"><span class="identifier">move</span><span class="special">()</span></code>
could reasonably be expected to leave its argument as a zero-capacity vector
to avoid having to copy all the elements. In other words, <span class="bold"><strong>move
is a potentially destructive copy</strong></span>.
</p>
<p>
In this particular case, we could have optimized swap by a specialization.
However, we can't specialize every function that copies a large object just
before it deletes or overwrites it. That would be unmanageable.
</p>
<p>
In C++0x, move semantics are implemented with the introduction of rvalue references.
They allow us to implement <code class="computeroutput"><span class="identifier">move</span><span class="special">()</span></code> without verbosity or runtime overhead. <span class="bold"><strong>Boost.Move</strong></span> is a library that offers tools to implement
those move semantics not only in compilers with <code class="computeroutput"><span class="identifier">rvalue</span>
<span class="identifier">references</span></code> but also in compilers
conforming to C++03.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2008-2014 Ion Gaztanaga<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
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>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="what_is_boost_move.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../move.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="implementing_movable_classes.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>