3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
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="scoped_enum.html" title="scoped_enum">
10 <link rel="next" href="typeinfo.html" title="typeinfo">
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>
22 <div class="spirit-nav">
23 <a accesskey="p" href="scoped_enum.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="typeinfo.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="core.swap"></a><a class="link" href="swap.html" title="swap">swap</a>
28 </h2></div></div></div>
29 <div class="toc"><dl class="toc">
30 <dt><span class="section"><a href="swap.html#core.swap.header_boost_core_swap_hpp">Header <boost/core/swap.hpp></a></span></dt>
31 <dt><span class="section"><a href="swap.html#core.swap.introduction">Introduction</a></span></dt>
32 <dt><span class="section"><a href="swap.html#core.swap.rationale">Rationale</a></span></dt>
33 <dt><span class="section"><a href="swap.html#core.swap.exception_safety">Exception Safety</a></span></dt>
34 <dt><span class="section"><a href="swap.html#core.swap.requirements">Requirements</a></span></dt>
35 <dt><span class="section"><a href="swap.html#core.swap.portability">Portability</a></span></dt>
36 <dt><span class="section"><a href="swap.html#core.swap.credits">Credits</a></span></dt>
38 <div class="simplesect">
39 <div class="titlepage"><div><div><h3 class="title">
40 <a name="idm46687073211952"></a>Authors</h3></div></div></div>
41 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
57 <div class="titlepage"><div><div><h3 class="title">
58 <a name="core.swap.header_boost_core_swap_hpp"></a><a class="link" href="swap.html#core.swap.header_boost_core_swap_hpp" title="Header <boost/core/swap.hpp>">Header <boost/core/swap.hpp></a>
59 </h3></div></div></div>
61 <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">void</span> <span class="identifier">swap</span><span class="special">(</span><span class="identifier">T</span><span class="special">&</span> <span class="identifier">left</span><span class="special">,</span> <span class="identifier">T</span><span class="special">&</span>
62 <span class="identifier">right</span><span class="special">);</span></code>
66 <div class="titlepage"><div><div><h3 class="title">
67 <a name="core.swap.introduction"></a><a class="link" href="swap.html#core.swap.introduction" title="Introduction">Introduction</a>
68 </h3></div></div></div>
70 The template function <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code>
71 allows the values of two variables to be swapped, using argument dependent
72 lookup to select a specialized swap function if available. If no specialized
73 swap function is available, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
78 <div class="titlepage"><div><div><h3 class="title">
79 <a name="core.swap.rationale"></a><a class="link" href="swap.html#core.swap.rationale" title="Rationale">Rationale</a>
80 </h3></div></div></div>
82 The generic <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code> function requires that the elements
83 to be swapped are assignable and copy constructible. It is usually implemented
84 using one copy construction and two assignments - this is often both unnecessarily
85 restrictive and unnecessarily slow. In addition, where the generic swap implementation
86 provides only the basic guarantee, specialized swap functions are often able
87 to provide the no-throw exception guarantee (and it is considered best practice
88 to do so where possible <a href="#ftn.core.swap.rationale.f0" class="footnote" name="core.swap.rationale.f0"><sup class="footnote">[1]</sup></a>.
91 The alternative to using argument dependent lookup in this situation is to
92 provide a template specialization of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
93 for every type that requires a specialized swap. Although this is legal C++,
94 no Boost libraries use this method, whereas many Boost libraries provide
95 specialized swap functions in their own namespaces.
98 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code> also supports swapping built-in arrays.
99 Note that <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code> originally did not do so, but a request
100 to add an overload of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
101 for built-in arrays has been accepted by the C++ Standards Committee<a href="#ftn.core.swap.rationale.f1" class="footnote" name="core.swap.rationale.f1"><sup class="footnote">[2]</sup></a>.
104 <div class="section">
105 <div class="titlepage"><div><div><h3 class="title">
106 <a name="core.swap.exception_safety"></a><a class="link" href="swap.html#core.swap.exception_safety" title="Exception Safety">Exception Safety</a>
107 </h3></div></div></div>
109 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code> provides the same exception guarantee
110 as the underlying swap function used, with one exception; for an array of
111 type <code class="computeroutput"><span class="identifier">T</span><span class="special">[</span><span class="identifier">n</span><span class="special">]</span></code>, where
112 <code class="computeroutput"><span class="identifier">n</span> <span class="special">></span>
113 <span class="number">1</span></code> and the underlying swap function
114 for <code class="computeroutput"><span class="identifier">T</span></code> provides the strong
115 exception guarantee, <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code>
116 provides only the basic exception guarantee.
119 <div class="section">
120 <div class="titlepage"><div><div><h3 class="title">
121 <a name="core.swap.requirements"></a><a class="link" href="swap.html#core.swap.requirements" title="Requirements">Requirements</a>
122 </h3></div></div></div>
126 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
127 <li class="listitem">
130 <li class="listitem">
131 T must be copy constructible
137 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
138 A function with the signature <code class="computeroutput"><span class="identifier">swap</span><span class="special">(</span><span class="identifier">T</span><span class="special">&,</span><span class="identifier">T</span><span class="special">&)</span></code> is available via argument dependent
144 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
145 A template specialization of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
151 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
152 T is a built-in array of swappable elements
155 <div class="section">
156 <div class="titlepage"><div><div><h3 class="title">
157 <a name="core.swap.portability"></a><a class="link" href="swap.html#core.swap.portability" title="Portability">Portability</a>
158 </h3></div></div></div>
160 Several older compilers do not support argument dependent lookup. On these
161 compilers <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code> will call <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>,
162 ignoring any specialized swap functions that could be found as a result of
163 argument dependent lookup.
166 <div class="section">
167 <div class="titlepage"><div><div><h3 class="title">
168 <a name="core.swap.credits"></a><a class="link" href="swap.html#core.swap.credits" title="Credits">Credits</a>
169 </h3></div></div></div>
170 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
171 <li class="listitem">
172 <span class="bold"><strong>Niels Dekker</strong></span> - for implementing and
173 documenting support for built-in arrays
175 <li class="listitem">
176 <span class="bold"><strong>Joseph Gauterin</strong></span> - for the initial idea,
177 implementation, tests, and documentation
179 <li class="listitem">
180 <span class="bold"><strong>Steven Watanabe</strong></span> - for the idea to make
181 <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code> less specialized than <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>, thereby allowing the function
182 to have the name 'swap' without introducing ambiguity
186 <div class="footnotes">
187 <br><hr style="width:100; text-align:left;margin-left: 0">
188 <div id="ftn.core.swap.rationale.f0" class="footnote"><p><a href="#core.swap.rationale.f0" class="para"><sup class="para">[1] </sup></a>
189 Scott Meyers, Effective C++ Third Edition, Item 25: "Consider support
190 for a non-throwing swap"
192 <div id="ftn.core.swap.rationale.f1" class="footnote"><p><a href="#core.swap.rationale.f1" class="para"><sup class="para">[2] </sup></a>
193 <a href="http://open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#809" target="_top">LWG
194 Defect Report 809: std::swap should be overloaded for array types</a>
198 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
199 <td align="left"></td>
200 <td align="right"><div class="copyright-footer">Copyright © 2014 Peter Dimov<br>Copyright © 2014 Glen Fernandes<br>Copyright © 2014 Andrey Semashev<p>
201 Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost
202 Software License, Version 1.0</a>.
207 <div class="spirit-nav">
208 <a accesskey="p" href="scoped_enum.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="typeinfo.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>