summaryrefslogtreecommitdiff
path: root/libs/core/doc/html/core/swap.html
blob: 4ff223b9c087c62f07e7a5fa42d34632eeaeedea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>swap</title>
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../index.html" title="Chapter&#160;1.&#160;Boost.Core">
<link rel="up" href="../index.html" title="Chapter&#160;1.&#160;Boost.Core">
<link rel="prev" href="scoped_enum.html" title="scoped_enum">
<link rel="next" href="typeinfo.html" title="typeinfo">
</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="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>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="core.swap"></a><a class="link" href="swap.html" title="swap">swap</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<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>
<dt><span class="section"><a href="swap.html#core.swap.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="swap.html#core.swap.rationale">Rationale</a></span></dt>
<dt><span class="section"><a href="swap.html#core.swap.exception_safety">Exception Safety</a></span></dt>
<dt><span class="section"><a href="swap.html#core.swap.requirements">Requirements</a></span></dt>
<dt><span class="section"><a href="swap.html#core.swap.portability">Portability</a></span></dt>
<dt><span class="section"><a href="swap.html#core.swap.credits">Credits</a></span></dt>
</dl></div>
<div class="simplesect">
<div class="titlepage"><div><div><h3 class="title">
<a name="idp87592048"></a>Authors</h3></div></div></div>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          Niels Dekker
        </li>
<li class="listitem">
          Joseph Gauterin
        </li>
<li class="listitem">
          Steven Watanabe
        </li>
<li class="listitem">
          Eric Niebler
        </li>
</ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<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>
</h3></div></div></div>
<p>
        <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>
        <span class="identifier">right</span><span class="special">);</span></code>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.introduction"></a><a class="link" href="swap.html#core.swap.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
        The template function <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code>
        allows the values of two variables to be swapped, using argument dependent
        lookup to select a specialized swap function if available. If no specialized
        swap function is available, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
        is used.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.rationale"></a><a class="link" href="swap.html#core.swap.rationale" title="Rationale">Rationale</a>
</h3></div></div></div>
<p>
        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
        to be swapped are assignable and copy constructible. It is usually implemented
        using one copy construction and two assignments - this is often both unnecessarily
        restrictive and unnecessarily slow. In addition, where the generic swap implementation
        provides only the basic guarantee, specialized swap functions are often able
        to provide the no-throw exception guarantee (and it is considered best practice
        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>.
      </p>
<p>
        The alternative to using argument dependent lookup in this situation is to
        provide a template specialization of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
        for every type that requires a specialized swap. Although this is legal C++,
        no Boost libraries use this method, whereas many Boost libraries provide
        specialized swap functions in their own namespaces.
      </p>
<p>
        <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code> also supports swapping built-in arrays.
        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
        to add an overload of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
        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>.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.exception_safety"></a><a class="link" href="swap.html#core.swap.exception_safety" title="Exception Safety">Exception Safety</a>
</h3></div></div></div>
<p>
        <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code> provides the same exception guarantee
        as the underlying swap function used, with one exception; for an array of
        type <code class="computeroutput"><span class="identifier">T</span><span class="special">[</span><span class="identifier">n</span><span class="special">]</span></code>, where
        <code class="computeroutput"><span class="identifier">n</span> <span class="special">&gt;</span>
        <span class="number">1</span></code> and the underlying swap function
        for <code class="computeroutput"><span class="identifier">T</span></code> provides the strong
        exception guarantee, <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">swap</span></code>
        provides only the basic exception guarantee.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.requirements"></a><a class="link" href="swap.html#core.swap.requirements" title="Requirements">Requirements</a>
</h3></div></div></div>
<p>
        Either:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            T must be assignable
          </li>
<li class="listitem">
            T must be copy constructible
          </li>
</ul></div>
<p>
        Or:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
            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
            lookup
          </li></ul></div>
<p>
        Or:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
            A template specialization of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span></code>
            exists for T
          </li></ul></div>
<p>
        Or:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">
            T is a built-in array of swappable elements
          </li></ul></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.portability"></a><a class="link" href="swap.html#core.swap.portability" title="Portability">Portability</a>
</h3></div></div></div>
<p>
        Several older compilers do not support argument dependent lookup. On these
        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>,
        ignoring any specialized swap functions that could be found as a result of
        argument dependent lookup.
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="core.swap.credits"></a><a class="link" href="swap.html#core.swap.credits" title="Credits">Credits</a>
</h3></div></div></div>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            <span class="bold"><strong>Niels Dekker</strong></span> - for implementing and
            documenting support for built-in arrays
          </li>
<li class="listitem">
            <span class="bold"><strong>Joseph Gauterin</strong></span> - for the initial idea,
            implementation, tests, and documentation
          </li>
<li class="listitem">
            <span class="bold"><strong>Steven Watanabe</strong></span> - for the idea to make
            <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
            to have the name 'swap' without introducing ambiguity
          </li>
</ul></div>
</div>
<div class="footnotes">
<br><hr style="width:100; text-align:left;margin-left: 0">
<div id="ftn.core.swap.rationale.f0" class="footnote"><p><a href="#core.swap.rationale.f0" class="para"><sup class="para">[1] </sup></a>
          Scott Meyers, Effective C++ Third Edition, Item 25: "Consider support
          for a non-throwing swap"
        </p></div>
<div id="ftn.core.swap.rationale.f1" class="footnote"><p><a href="#core.swap.rationale.f1" class="para"><sup class="para">[2] </sup></a>
          <a href="http://open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#809" target="_top">LWG
          Defect Report 809: std::swap should be overloaded for array types</a>
        </p></div>
</div>
</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 &#169; 2014 Peter Dimov<br>Copyright &#169; 2014 Glen Fernandes<br>Copyright &#169; 2014 Andrey Semashev<p>
        Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost
        Software License, Version 1.0</a>.
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<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>
</div>
</body>
</html>