<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2554: Swapping multidimensional arrays is never noexcept</title>
<meta property="og:title" content="Issue 2554: Swapping multidimensional arrays is never noexcept">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2554.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="2554"><a href="lwg-defects.html#2554">2554</a>. Swapping multidimensional arrays is never <code>noexcept</code></h3>
<p><b>Section:</b> 22.2.2 <a href="https://wg21.link/utility.swap">[utility.swap]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Orson Peters <b>Opened:</b> 2015-11-01 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#utility.swap">issues</a> in [utility.swap].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The <code>noexcept</code> specification for the <code>std::swap</code> overload for arrays has the effect that 
all multidimensional arrays &mdash; even those of build-in types &mdash; would be considered as non-<code>noexcept</code> 
swapping, as described in the following 
<a href="http://stackoverflow.com/questions/26793979/why-is-swapping-multidimensional-arrays-not-noexcept">Stackoverflow article</a>.
<p/>
Consider the following example code:
</p>
<blockquote><pre>
#include &lt;utility&gt;
#include &lt;iostream&gt;

int main() 
{
  int x[2][3];
  int y[2][3];

  using std::swap;
  std::cout &lt;&lt; noexcept(swap(x, y)) &lt;&lt; "\n";
}
</pre></blockquote>
<p>
Both clang 3.8.0 and gcc 5.2.0 return <code>0</code>.
<p/>
The reason for this unexpected result seems to be a consequence of both core wording rules (6.4.2 <a href="https://wg21.link/basic.scope.pdecl">[basic.scope.pdecl]</a> says
that "The point of declaration for a name is immediately after its complete declarator (Clause 8) and before its initializer (if any)"
and the exception specification is part of the declarator) and the fact that the exception-specification of the <code>std::swap</code> 
overload for arrays uses an expression and not a type trait. At the point where the expression is evaluated, only the non-array
<code>std::swap</code> overload is in scope whose <code>noexcept</code> specification evaluates to <code>false</code> since arrays are neither
move-constructible nor move-assignable.
<p/>
<b>Daniel:</b>
<p/>
The here described problem is another example for the currently broken <code>swap</code> exception specifications in the Standard
library as pointed out by 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>. The paper 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4511.html">N4511</a> describes a resolution that would address
this problem. If the array <code>swap</code> overload would be declared instead as follows,
</p>
<blockquote><pre>
template &lt;class T, size_t N&gt; 
void swap(T (&amp;a)[N], T (&amp;b)[N]) noexcept(is_nothrow_swappable&lt;T&gt;::value);
</pre></blockquote>
<p>
the expected outcome is obtained.
<p/>
Revision 2 (<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0185r0.html">P0185R0</a>) of above mentioned paper will 
available for the mid February 2016 mailing.
</p>

<p><i>[2016-03-06, Daniel comments]</i></p>

<p>
With the acceptance of revision 3 <a href="https://wg21.link/P0185R1">P0185R1</a> during the Jacksonville meeting, this
issue should be closed as "resolved": The expected program output is now <code>1</code>. The current gcc 6.0 trunk has
already <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68877">implemented</a> the relevant parts of P0185R1.
</p>


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





</body>
</html>
