<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1349: swap should not throw</title>
<meta property="og:title" content="Issue 1349: swap should not throw">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1349.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++11">C++11</a> status.</em></p>
<h3 id="1349"><a href="lwg-defects.html#1349">1349</a>. <code>swap</code> should not throw</h3>
<p><b>Section:</b> 16 <a href="https://wg21.link/library">[library]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> BSI <b>Opened:</b> 2010-08-25 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#library">active issues</a> in [library].</p>
<p><b>View all other</b> <a href="lwg-index.html#library">issues</a> in [library].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses GB-65</b></p>
<p>
Nothrowing <code>swap</code> operations are key to many C++ idioms,
notably the common copy/swap idiom to provide the
strong exception safety guarantee.
</p>

<p><i>[
Resolution proposed by ballot comment
]</i></p>


<p>
Where possible, all library types should provide a
<code>swap</code> operation with an exception specification
guaranteeing no exception shall propagate.
Where <code>noexcept(true)</code> cannot be guaranteed to
not terminate the program, and the <code>swap</code> in
questions is a template, an exception specification
with the appropriate conditional expression could
be specified.
</p>

<p><i>[2011-03-13: Daniel comments and drafts wording]</i></p>


<p>During a survey of the library some main categories for
potential <code>noexcept</code> <code>swap</code> function could be isolated:</p>

<ol>
<li>Free <code>swap</code> functions that are specified in terms of already
<code>noexcept</code> <code>swap</code> member functions, like that of <code>valarray</code>.</li>

<li>Free <code>swap</code> of <code>std::function</code>, and member and free <code>swap</code> 
functions of stream buffers and streams where considered but rejected as good candidates, 
because of the danger to potentially impose requirements of existing implementations. 
These functions could be reconsidered as candidates in the future.</li>
</ol>

<p>Negative list:</p>

<ol>
<li>Algorithms related to swap, like <code>iter_swap</code>, have <em>not</em> been touched,
because there are no fundamental exceptions constraints on iterator operations in general
(only for specific types, like library container iterators)</li>
</ol>

<p>While evaluating the current state of <code>swap</code> functions 
of library components it was observed that several conditional <code>noexcept</code>
functions have turned into unconditional ones, e.g. in the
header <code>&lt;utility&gt;</code> synopsis:</p>

<blockquote><pre>
template&lt;class T&gt; void swap(T&amp; a, T&amp; b) noexcept;
</pre></blockquote>

<p>The suggested resolution shown below also attempts to fix
these cases.</p>

<p><i>[2011-03-22 Daniel redrafts to satisfy new criteria for applying <code>noexcept</code>.
Parts resolved by N3263-v2 and D3267 are not added here.]</i></p>




<p id="res-1349"><b>Proposed resolution:</b></p>
<ol>
<li><p>Edit 22.2 <a href="https://wg21.link/utility">[utility]</a> p. 2, header <code>&lt;utility&gt;</code> synopsis <em>and</em> 
22.2.2 <a href="https://wg21.link/utility.swap">[utility.swap]</a> before p. 1, as indicated (The intent is to fix an editorial
omission):</p>

<blockquote><pre>
template&lt;class T&gt; void swap(T&amp; a, T&amp; b) noexcept<ins>(<i>see below</i>)</ins>;
</pre></blockquote>
</li>

<li><p>Edit the prototype declaration in 22.3.2 <a href="https://wg21.link/pairs.pair">[pairs.pair]</a> before p. 34 as indicated (The intent 
is to fix an editorial omission):</p>

<blockquote><pre>
void swap(pair&amp; p) noexcept<ins>(<i>see below</i>)</ins>;
</pre></blockquote>
</li>

<li><p>Edit 22.4.1 <a href="https://wg21.link/tuple.general">[tuple.general]</a> p. 2 header <code>&lt;tuple&gt;</code> synopsis <em>and</em> 
22.4.12 <a href="https://wg21.link/tuple.special">[tuple.special]</a> before p. 1 as indicated (The intent is to fix an editorial omission):</p>

<blockquote><pre>
template &lt;class... Types&gt;
void swap(tuple&lt;Types...&gt;&amp; x, tuple&lt;Types...&gt;&amp; y) noexcept<ins>(<i>see below</i>)</ins>;
</pre></blockquote>
</li>

<li><p>Edit 22.4.4 <a href="https://wg21.link/tuple.tuple">[tuple.tuple]</a>, class template <code>tuple</code> synopsis <em>and</em>
22.4.4.4 <a href="https://wg21.link/tuple.swap">[tuple.swap]</a> before p. 1 as indicated (The intent is to fix an editorial omission):</p>

<blockquote><pre>
void swap(tuple&amp;) noexcept<ins>(<i>see below</i>)</ins>;
</pre></blockquote>
</li>

<li><p>Edit 20.2.2 <a href="https://wg21.link/memory.syn">[memory.syn]</a> p. 1, header <code>&lt;memory&gt;</code> synopsis as indicated (The 
intent is to fix an editorial omission of the proposing paper N3195).</p>

<blockquote><pre>
template&lt;class T&gt; void swap(shared_ptr&lt;T&gt;&amp; a, shared_ptr&lt;T&gt;&amp; b) <ins>noexcept</ins>;
</pre></blockquote>
</li>

<li><p>Edit header <code>&lt;valarray&gt;</code> synopsis, 29.6.1 <a href="https://wg21.link/valarray.syn">[valarray.syn]</a> <em>and</em> 
29.6.3.4 <a href="https://wg21.link/valarray.special">[valarray.special]</a> before p. 1 as indicated 
<em>[Drafting comment: The corresponding member swap is already noexcept]</em>:</p>

<blockquote><pre>
template&lt;class T&gt; void swap(valarray&lt;T&gt;&amp;, valarray&lt;T&gt;&amp;) <ins>noexcept</ins>;
</pre></blockquote>
</li>

</ol>





</body>
</html>
