diff options
author | Anas Nashif <anas.nashif@intel.com> | 2012-10-30 12:57:26 -0700 |
---|---|---|
committer | Anas Nashif <anas.nashif@intel.com> | 2012-10-30 12:57:26 -0700 |
commit | 1a78a62555be32868418fe52f8e330c9d0f95d5a (patch) | |
tree | d3765a80e7d3b9640ec2e930743630cd6b9fce2b /doc/html/move/introduction.html | |
download | boost-1a78a62555be32868418fe52f8e330c9d0f95d5a.tar.gz boost-1a78a62555be32868418fe52f8e330c9d0f95d5a.tar.bz2 boost-1a78a62555be32868418fe52f8e330c9d0f95d5a.zip |
Imported Upstream version 1.49.0upstream/1.49.0
Diffstat (limited to 'doc/html/move/introduction.html')
-rwxr-xr-x | doc/html/move/introduction.html | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/doc/html/move/introduction.html b/doc/html/move/introduction.html new file mode 100755 index 0000000000..ff39f3a808 --- /dev/null +++ b/doc/html/move/introduction.html @@ -0,0 +1,100 @@ +<html> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> +<title>Introduction</title> +<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> +<meta name="generator" content="DocBook XSL Stylesheets V1.76.1"> +<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> +<link rel="up" href="../move.html" title="Chapter 16. Boost.Move"> +<link rel="prev" href="../move.html" title="Chapter 16. 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="../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"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span><span class="identifier">T</span><span class="special">&</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">T</span><span class="special">&</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"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> <span class="keyword">void</span> <span class="identifier">swap</span><span class="special">(</span><span class="identifier">T</span><span class="special">&</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">T</span><span class="special">&</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-2010 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="../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> |