• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Introduction</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../move.html" title="Chapter 25. Boost.Move">
10<link rel="prev" href="what_is_boost_move.html" title="What is Boost.Move?">
11<link rel="next" href="implementing_movable_classes.html" title="Implementing copyable and movable classes">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<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>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="move.introduction"></a><a class="link" href="introduction.html" title="Introduction">Introduction</a>
29</h2></div></div></div>
30<div class="note"><table border="0" summary="Note">
31<tr>
32<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
33<th align="left">Note</th>
34</tr>
35<tr><td align="left" valign="top"><p>
36        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
37        Brief Introduction to Rvalue References</em></span></a> by Howard E. Hinnant,
38        Bjarne Stroustrup, and Bronek Kozicki
39      </p></td></tr>
40</table></div>
41<p>
42      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
43      involves a function call, a memory allocation, and a loop. This is of course
44      acceptable where we actually need two copies of a vector, but in many cases,
45      we don't: We often copy a <code class="computeroutput"><span class="identifier">vector</span></code>
46      from one place to another, just to proceed to overwrite the old copy. Consider:
47    </p>
48<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>
49<span class="special">{</span>
50   <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>
51   <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>
52   <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>
53<span class="special">}</span>
54</pre>
55<p>
56      But, we didn't want to have any copies of a or b, we just wanted to swap them.
57      Let's try again:
58    </p>
59<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>
60<span class="special">{</span>
61   <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>
62   <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>
63   <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>
64<span class="special">}</span>
65</pre>
66<p>
67      This <code class="computeroutput"><span class="identifier">move</span><span class="special">()</span></code>
68      gives its target the value of its argument, but is not obliged to preserve
69      the value of its source. So, for a <code class="computeroutput"><span class="identifier">vector</span></code>,
70      <code class="computeroutput"><span class="identifier">move</span><span class="special">()</span></code>
71      could reasonably be expected to leave its argument as a zero-capacity vector
72      to avoid having to copy all the elements. In other words, <span class="bold"><strong>move
73      is a potentially destructive copy</strong></span>.
74    </p>
75<p>
76      In this particular case, we could have optimized swap by a specialization.
77      However, we can't specialize every function that copies a large object just
78      before it deletes or overwrites it. That would be unmanageable.
79    </p>
80<p>
81      In C++0x, move semantics are implemented with the introduction of rvalue references.
82      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
83      those move semantics not only in compilers with <code class="computeroutput"><span class="identifier">rvalue</span>
84      <span class="identifier">references</span></code> but also in compilers
85      conforming to C++03.
86    </p>
87</div>
88<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
89<td align="left"></td>
90<td align="right"><div class="copyright-footer">Copyright © 2008-2014 Ion Gaztanaga<p>
91        Distributed under the Boost Software License, Version 1.0. (See accompanying
92        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>)
93      </p>
94</div></td>
95</tr></table>
96<hr>
97<div class="spirit-nav">
98<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>
99</div>
100</body>
101</html>
102