<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2561: [fund.ts.v2] Incorrect exception specifications for 'swap' in C++ Extensions for Library Fundamentals</title>
<meta property="og:title" content="Issue 2561: [fund.ts.v2] Incorrect exception specifications for 'swap' in C++ Extensions for Library Fundamentals">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2561.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2561"><a href="lwg-defects.html#2561">2561</a>. [fund.ts.v2] Incorrect exception specifications for 'swap' in C++ Extensions for Library Fundamentals</h3>
<p><b>Section:</b> 5.3.4 <a href="https://cplusplus.github.io/fundamentals-ts/v2.html#optional.object.swap">[fund.ts.v2::optional.object.swap]</a>, 3.7.8 <a href="https://cplusplus.github.io/fundamentals-ts/v2.html#propagate_const.modifiers">[fund.ts.v2::propagate_const.modifiers]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2015-11-14 <b>Last modified:</b> 2022-07-28</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses: fund.ts.v2</b></p>
<p>
As pointed out in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4511.html#Related_Issues">N4511</a>,
the Library fundamentals are affected by a similar problem as described in LWG <a href="lwg-defects.html#2456" title="Incorrect exception specifications for 'swap' throughout library (Status: Resolved)">2456</a><sup><a href="https://cplusplus.github.io/LWG/issue2456" title="Latest snapshot">(i)</a></sup>. First, it is caused
by <code>optional</code>'s member <code>swap</code> (5.3.4 <a href="https://cplusplus.github.io/fundamentals-ts/v2.html#optional.object.swap">[fund.ts.v2::optional.object.swap]</a>):
</p>
<blockquote><pre>
void swap(optional&lt;T&gt;&amp; rhs) noexcept(<i>see below</i>);
</pre></blockquote>
<p>
with 
</p>
<blockquote>
<p>
The expression inside <code>noexcept</code> is equivalent to: 
</p>
<blockquote><pre>
is_nothrow_move_constructible_v&lt;T&gt; &amp;&amp; noexcept(swap(declval&lt;T&amp;&gt;(), declval&lt;T&amp;&gt;()))
</pre></blockquote>
</blockquote>
<p>
Again, the unqualified lookup for <code>swap</code> finds the member <code>swap</code> instead of the result of a normal argument-depending 
lookup, making this ill-formed.
<p/>
A second example of such a problem recently entered the arena with the addition of the <code>propagate_const</code> template
with another member <code>swap</code> (3.7.8 <a href="https://cplusplus.github.io/fundamentals-ts/v2.html#propagate_const.modifiers">[fund.ts.v2::propagate_const.modifiers]</a>):
</p>
<blockquote>
<pre>
constexpr void swap(propagate_const&amp; pt) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-2- The constant-expression in the exception-specification is <code>noexcept(swap(t_, pt.t_))</code>.
</p>
</blockquote>
</blockquote>
<p>
A working approach is presented in 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4511.html#Proposed_resolution">N4511</a>. By adding a new
trait to the standard library and referencing this by the library fundamentals (A similar approach had been applied in the
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3940.html#path-inserter-extractor">file system specification</a>
where the <code>quoted</code> manipulator from C++14 had been referred to, albeit the file system specification is generally based on the
C++11 standard), <code>optional</code>'s member <code>swap</code> exception specification could be rephrased as follows:
</p>
<blockquote>
<p>
The expression inside <code>noexcept</code> is equivalent to: 
</p>
<blockquote><pre>
is_nothrow_move_constructible_v&lt;T&gt; &amp;&amp; <ins>is_nothrow_swappable_v&lt;T&gt;</ins><del>noexcept(swap(declval&lt;T&amp;&gt;(), declval&lt;T&amp;&gt;()))</del>
</pre></blockquote>
</blockquote>
<p>
and <code>propagate_const</code>'s member <code>swap</code> exception specification could be rephrased as follows:
</p>
<blockquote>
<pre>
constexpr void swap(propagate_const&amp; pt) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-2- The constant-expression in the exception-specification is <code><ins>is_nothrow_swappable_v&lt;T&gt;</ins><del>noexcept(swap(t_, pt.t_))</del></code>.
</p>
</blockquote>
</blockquote>

<p><i>[2016-02-20, Ville comments]</i></p>

<p>
Feedback from an implementation:
<p/>
libstdc++ already applies the proposed resolution for <code>propagate_const</code>,
but not for <code>optional</code>.
</p>

<p><i>[2016-02-20, Daniel comments]</i></p>

<p>
A recent paper update has been provided: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0185r0.html#Proposed_resolution">P0185R0</a>.
</p>

<p><i>[2016-03, Jacksonville]</i></p>

Add a link to <a href="lwg-defects.html#2456" title="Incorrect exception specifications for 'swap' throughout library (Status: Resolved)">2456</a><sup><a href="https://cplusplus.github.io/LWG/issue2456" title="Latest snapshot">(i)</a></sup>

<p><i>[2016-11-08, Issaquah]</i></p>

<p>Not adopted during NB comment resolution</p>

<p><i>[2020-03-30; Daniel comments]</i></p>

<p>
This has strong overlap with LWG <a href="lwg-defects.html#3413" title="[fund.ts.v3] propagate_const's swap's noexcept specification needs to 
be constrained and use a trait (Status: C++23)">3413</a><sup><a href="https://cplusplus.github.io/LWG/issue3413" title="Latest snapshot">(i)</a></sup>, which describes a sub-set of the problem here. 
Rebasing of the library fundamentals on C++20 has removed the mentioned problem for 
<code>optional</code>s free <code>swap</code>, so there are now no longer any further free <code>swap</code> function
templates with conditionally <code>noexcept</code> specifications except for <code>propagate_const</code> 
(but now handled by LWG <a href="lwg-defects.html#3413" title="[fund.ts.v3] propagate_const's swap's noexcept specification needs to 
be constrained and use a trait (Status: C++23)">3413</a><sup><a href="https://cplusplus.github.io/LWG/issue3413" title="Latest snapshot">(i)</a></sup>).
</p>
<p><i>[2022-07-28 Resolved by <a href="https://wg21.link/P0966R1" title=" string::reserve Should Not Shrink">P0966R1</a> and LWG <a href="lwg-defects.html#3413" title="[fund.ts.v3] propagate_const's swap's noexcept specification needs to 
be constrained and use a trait (Status: C++23)">3413</a><sup><a href="https://cplusplus.github.io/LWG/issue3413" title="Latest snapshot">(i)</a></sup>. Status changed: New &rarr; Resolved.]</i></p>



<p id="res-2561"><b>Proposed resolution:</b></p>





</body>
</html>
