Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / core / doc / html / core / swap.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>swap</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&#160;1.&#160;Boost.Core">
8 <link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Boost.Core">
9 <link rel="prev" href="scoped_enum.html" title="scoped_enum">
10 <link rel="next" href="typeinfo.html" title="typeinfo">
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="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>
24 </div>
25 <div class="section">
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 &lt;boost/core/swap.hpp&gt;</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>
37 </dl></div>
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; ">
42 <li class="listitem">
43           Niels Dekker
44         </li>
45 <li class="listitem">
46           Joseph Gauterin
47         </li>
48 <li class="listitem">
49           Steven Watanabe
50         </li>
51 <li class="listitem">
52           Eric Niebler
53         </li>
54 </ul></div>
55 </div>
56 <div class="section">
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 &lt;boost/core/swap.hpp&gt;">Header &lt;boost/core/swap.hpp&gt;</a>
59 </h3></div></div></div>
60 <p>
61         <code class="computeroutput"><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">left</span><span class="special">,</span> <span class="identifier">T</span><span class="special">&amp;</span>
62         <span class="identifier">right</span><span class="special">);</span></code>
63       </p>
64 </div>
65 <div class="section">
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>
69 <p>
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>
74         is used.
75       </p>
76 </div>
77 <div class="section">
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>
81 <p>
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>.
89       </p>
90 <p>
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.
96       </p>
97 <p>
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>.
102       </p>
103 </div>
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>
108 <p>
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">&gt;</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.
117       </p>
118 </div>
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>
123 <p>
124         Either:
125       </p>
126 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
127 <li class="listitem">
128             T must be assignable
129           </li>
130 <li class="listitem">
131             T must be copy constructible
132           </li>
133 </ul></div>
134 <p>
135         Or:
136       </p>
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">&amp;,</span><span class="identifier">T</span><span class="special">&amp;)</span></code> is available via argument dependent
139             lookup
140           </li></ul></div>
141 <p>
142         Or:
143       </p>
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>
146             exists for T
147           </li></ul></div>
148 <p>
149         Or:
150       </p>
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
153           </li></ul></div>
154 </div>
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>
159 <p>
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.
164       </p>
165 </div>
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
174           </li>
175 <li class="listitem">
176             <span class="bold"><strong>Joseph Gauterin</strong></span> - for the initial idea,
177             implementation, tests, and documentation
178           </li>
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
183           </li>
184 </ul></div>
185 </div>
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"
191         </p></div>
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>
195         </p></div>
196 </div>
197 </div>
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 &#169; 2014 Peter Dimov<br>Copyright &#169; 2014 Glen Fernandes<br>Copyright &#169; 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>.
203       </p>
204 </div></td>
205 </tr></table>
206 <hr>
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>
209 </div>
210 </body>
211 </html>