1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>exchange</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="../index.html" title="Chapter 1. Boost.Core"> 8<link rel="up" href="../index.html" title="Chapter 1. Boost.Core"> 9<link rel="prev" href="enable_if.html" title="enable_if"> 10<link rel="next" href="explicit_operator_bool.html" title="explicit_operator_bool"> 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="enable_if.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="explicit_operator_bool.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 27<a name="core.exchange"></a><a class="link" href="exchange.html" title="exchange">exchange</a> 28</h2></div></div></div> 29<div class="toc"><dl class="toc"> 30<dt><span class="section"><a href="exchange.html#core.exchange.overview">Overview</a></span></dt> 31<dt><span class="section"><a href="exchange.html#core.exchange.examples">Examples</a></span></dt> 32<dt><span class="section"><a href="exchange.html#core.exchange.reference">Reference</a></span></dt> 33</dl></div> 34<div class="simplesect"> 35<div class="titlepage"><div><div><h3 class="title"> 36<a name="idm45312828603872"></a>Authors</h3></div></div></div> 37<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"> 38 Glen Fernandes 39 </li></ul></div> 40</div> 41<div class="section"> 42<div class="titlepage"><div><div><h3 class="title"> 43<a name="core.exchange.overview"></a><a class="link" href="exchange.html#core.exchange.overview" title="Overview">Overview</a> 44</h3></div></div></div> 45<p> 46 The header <boost/core/exchange.hpp> provides the function template 47 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">exchange</span></code> which is an implementation of 48 the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">exchange</span></code> function introduced in C++14. 49 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">exchange</span><span class="special">(</span><span class="identifier">o</span><span class="special">,</span> <span class="identifier">v</span><span class="special">)</span></code> replaces the value of <code class="computeroutput"><span class="identifier">o</span></code> 50 with <code class="computeroutput"><span class="identifier">v</span></code> and returns the old 51 value of <code class="computeroutput"><span class="identifier">o</span></code>. 52 </p> 53</div> 54<div class="section"> 55<div class="titlepage"><div><div><h3 class="title"> 56<a name="core.exchange.examples"></a><a class="link" href="exchange.html#core.exchange.examples" title="Examples">Examples</a> 57</h3></div></div></div> 58<p> 59 The following example shows <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">exchange</span></code> 60 used to simplify the implementation of a move constructor. 61 </p> 62<pre class="programlisting"><span class="identifier">Node</span><span class="special">(</span><span class="identifier">Node</span><span class="special">&&</span> <span class="identifier">other</span><span class="special">)</span> 63 <span class="special">:</span> <span class="identifier">head_</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">exchange</span><span class="special">(</span><span class="identifier">other</span><span class="special">.</span><span class="identifier">head_</span><span class="special">,</span> <span class="keyword">nullptr</span><span class="special">))</span> 64 <span class="special">,</span> <span class="identifier">tail_</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">exchange</span><span class="special">(</span><span class="identifier">other</span><span class="special">.</span><span class="identifier">tail_</span><span class="special">,</span> <span class="keyword">nullptr</span><span class="special">))</span> <span class="special">{</span> <span class="special">}</span> 65</pre> 66</div> 67<div class="section"> 68<div class="titlepage"><div><div><h3 class="title"> 69<a name="core.exchange.reference"></a><a class="link" href="exchange.html#core.exchange.reference" title="Reference">Reference</a> 70</h3></div></div></div> 71<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> 72 <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">class</span> <span class="identifier">U</span> <span class="special">=</span> <span class="identifier">T</span><span class="special">></span> 73 <span class="keyword">constexpr</span> <span class="identifier">T</span> <span class="identifier">exchange</span><span class="special">(</span><span class="identifier">T</span><span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="identifier">U</span><span class="special">&&</span> <span class="identifier">u</span><span class="special">);</span> 74<span class="special">}</span> 75</pre> 76<div class="section"> 77<div class="titlepage"><div><div><h4 class="title"> 78<a name="core.exchange.reference.functions"></a><a class="link" href="exchange.html#core.exchange.reference.functions" title="Functions">Functions</a> 79</h4></div></div></div> 80<p> 81 <span class="bold"><strong><code class="computeroutput"><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">class</span> 82 <span class="identifier">U</span> <span class="special">=</span> 83 <span class="identifier">T</span><span class="special">></span> 84 <span class="keyword">constexpr</span> <span class="identifier">T</span> 85 <span class="identifier">exchange</span><span class="special">(</span><span class="identifier">T</span><span class="special">&</span> <span class="identifier">t</span><span class="special">,</span> <span class="identifier">U</span><span class="special">&&</span> <span class="identifier">u</span><span class="special">);</span></code></strong></span> 86 </p> 87<p> 88 Equivalent to: 89 </p> 90<pre class="programlisting"><span class="identifier">T</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">t</span><span class="special">);</span> 91<span class="identifier">t</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special"><</span><span class="identifier">U</span><span class="special">>(</span><span class="identifier">u</span><span class="special">);</span> 92<span class="keyword">return</span> <span class="identifier">v</span><span class="special">;</span> 93</pre> 94</div> 95</div> 96</div> 97<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 98<td align="left"></td> 99<td align="right"><div class="copyright-footer">Copyright © 2014 Peter Dimov<br>Copyright © 2014 Glen Fernandes<br>Copyright © 2014 Andrey Semashev<p> 100 Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost 101 Software License, Version 1.0</a>. 102 </p> 103</div></td> 104</tr></table> 105<hr> 106<div class="spirit-nav"> 107<a accesskey="p" href="enable_if.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.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="explicit_operator_bool.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 108</div> 109</body> 110</html> 111