<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2749: swappable traits for variants</title>
<meta property="og:title" content="Issue 2749: swappable traits for variants">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2749.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++17">C++17</a> status.</em></p>
<h3 id="2749"><a href="lwg-defects.html#2749">2749</a>. <code>swappable</code> traits for <code>variant</code>s</h3>
<p><b>Section:</b> 22.6.3.7 <a href="https://wg21.link/variant.swap">[variant.swap]</a>, 22.6.10 <a href="https://wg21.link/variant.specalg">[variant.specalg]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Agust&iacute;n K-ballo Berg&eacute; <b>Opened:</b> 2016-07-19 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>1
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>variant</code> does not play nice with swappable traits, the non-member specialized <code>swap</code> overload is not 
SFINAE friendly. On the other hand, the member <code>swap</code> is SFINAE friendly, albeit with an incomplete condition, 
when arguably it shouldn't be. Given the <i>Effects</i>, <i>Throws</i>, and <i>Remarks</i> clauses, the SFINAE 
condition should include <code>is_move_constructible_v</code> and <code>is_move_assignable_v</code> to account for the 
involvement of <code>variant</code>'s move constructor and move assignment operator (the <code>noexcept</code> specification is 
correct as is, since the move assignment operator would only be called for <code>variant</code>s with different alternatives). 
This SFINAE condition should apply to the non-member <code>swap</code> overload, while the member <code>swap</code> should require 
all alternatives are swappable (as defined by 16.4.4.3 <a href="https://wg21.link/swappable.requirements">[swappable.requirements]</a>).
</p>
<p><i>[2016-07 Chicago]</i></p>

<p>Monday: P1 - review later in the week</p>
<p>Fri PM: Move to Tentatively Ready</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to N4606.</p>

<ol>
<li><p>Modify 22.6.3.7 <a href="https://wg21.link/variant.swap">[variant.swap]</a> as indicated:</p>

<blockquote>
<pre>
void swap(variant&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
<ins>-?- <i>Requires:</i> Lvalues of type <code>T<sub><i>i</i></sub></code> shall be swappable and 
<code>is_move_constructible_v&lt;T<sub><i>i</i></sub>&gt; &amp;&amp; is_move_assignable_v&lt;T<sub><i>i</i></sub>&gt;</code> 
is <code>true</code> for all <code><i>i</i></code>.</ins>
<p/>
[&hellip;]
<p/>
-3- <i>Remarks:</i> <del>This function shall not participate in overload resolution unless <code>is_swappable_v&lt;Ti&gt;</code> 
is <code>true</code> for all <code><i>i</i></code>.</del> If an exception is thrown during the call to function 
<code>swap(get&lt;<i>i</i>&gt;(*this), get&lt;<i>i</i>&gt;(rhs))</code>, the states of the contained values of 
<code>*this</code> and of <code>rhs</code> are determined by the exception safety guarantee of swap for lvalues of 
<code>T<sub><i>i</i></sub></code> with <code><i>i</i></code> being <code>index()</code>. If an exception is thrown during the 
exchange of the values of <code>*this</code> and <code>rhs</code>, the states of the values of <code>*this</code> and of <code>rhs</code> 
are determined by the exception safety guarantee of <code>variant</code>'s move constructor and move assignment operator. 
The expression inside <code>noexcept</code> is equivalent to the logical AND of 
<code>is_nothrow_move_constructible_v&lt;T<sub><i>i</i></sub>&gt; &amp;&amp; 
is_nothrow_swappable_v&lt;T<sub><i>i</i></sub>&gt;</code> for all <code><i>i</i></code>.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Modify 22.6.10 <a href="https://wg21.link/variant.specalg">[variant.specalg]</a> as indicated:</p>

<blockquote>
<pre>
template &lt;class... Types&gt; void swap(variant&lt;Types...&gt;&amp; v, variant&lt;Types...&gt;&amp; w) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-1- <i>Effects:</i> Equivalent to <code>v.swap(w)</code>.
<p/>
-2- <i>Remarks:</i> <ins>This function shall not participate in overload resolution unless 
<code>is_move_constructible_v&lt;T<sub><i>i</i></sub>&gt; &amp;&amp; is_move_assignable_v&lt;T<sub><i>i</i></sub>&gt; 
&amp;&amp; is_swappable_v&lt;T<sub><i>i</i></sub>&gt;</code> is <code>true</code> for all <code><i>i</i></code>.</ins> The 
expression inside <code>noexcept</code> is equivalent to <code>noexcept(v.swap(w))</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2016-08-13, Reopened by Casey Carter]</i></p>

<p>
It is possible to exchange the value of two variants using only move construction on the alternative types, as if by
<pre>
auto tmp = move(x);
x.emplace&lt;<i>i</i>&gt;(move(y));
y.emplace&lt;<i>j</i>&gt;(move(tmp));
</pre> 
where <i>i</i> is <code>y.index()</code> and <i>j</i> is <code>tmp.index()</code>. Consequently, <code>variant</code>'s member swap
need not require move assignable alternatives.
</p>

<p><i>[2016-09-09 Issues Resolution Telecon]</i></p>

<p>Move to Tentatively Ready</p>


<p id="res-2749"><b>Proposed resolution:</b></p>
<p>This wording is relative to N4606.</p>

<ol>
<li><p>Modify 22.6.3.7 <a href="https://wg21.link/variant.swap">[variant.swap]</a> as indicated:</p>

<blockquote>
<pre>
void swap(variant&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p/>
<ins>-?- <i>Requires:</i> Lvalues of type <code>T<sub><i>i</i></sub></code> shall be swappable and 
<code>is_move_constructible_v&lt;T<sub><i>i</i></sub>&gt;</code> shall be <code>true</code> for all <code><i>i</i></code>.</ins>
<p/>
[&hellip;]
<p/>
-2- <i>Throws:</i> <ins>If <code>index() == rhs.index()</code>, a</ins><del>A</del>ny exception thrown by
<code>swap(get&lt;<i>i</i>&gt;(*this), get&lt;<i>i</i>&gt;(rhs))</code> with <i>i</i> being <code>index()</code><del> and
<code>variant</code>'s move constructor and assignment operator</del>. <ins>Otherwise, any exception thrown by the move
constructor of <code>T<sub><i>i</i></sub></code> or <code>T<sub><i>j</i></sub></code> with <code><i>i</i></code> being
<code>index()</code> and <code><i>j</i></code> being <code>rhs.index()</code>.</ins>
<p/>
-3- <i>Remarks:</i> <del>This function shall not participate in overload resolution unless <code>is_swappable_v&lt;Ti&gt;</code> 
is <code>true</code> for all <code><i>i</i></code>.</del> If an exception is thrown during the call to function
<code>swap(get&lt;<i>i</i>&gt;(*this), get&lt;<i>i</i>&gt;(rhs))</code>, the states of the contained values of
<code>*this</code> and of <code>rhs</code> are determined by the exception safety guarantee of swap for lvalues of
<code>T<sub><i>i</i></sub></code> with <code><i>i</i></code> being <code>index()</code>. If an exception is thrown during the 
exchange of the values of <code>*this</code> and <code>rhs</code>, the states of the values of <code>*this</code> and of <code>rhs</code> 
are determined by the exception safety guarantee of <code>variant</code>'s move constructor <del>and move assignment operator</del>. 
The expression inside <code>noexcept</code> is equivalent to the logical AND of 
<code>is_nothrow_move_constructible_v&lt;T<sub><i>i</i></sub>&gt; &amp;&amp; 
is_nothrow_swappable_v&lt;T<sub><i>i</i></sub>&gt;</code> for all <code><i>i</i></code>.
</blockquote>
</blockquote>
</li>

<li><p>Modify 22.6.10 <a href="https://wg21.link/variant.specalg">[variant.specalg]</a> as indicated:</p>

<blockquote>
<pre>
template &lt;class... Types&gt; void swap(variant&lt;Types...&gt;&amp; v, variant&lt;Types...&gt;&amp; w) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-1- <i>Effects:</i> Equivalent to <code>v.swap(w)</code>.
<p/>
-2- <i>Remarks:</i> <ins>This function shall not participate in overload resolution unless 
<code>is_move_constructible_v&lt;T<sub><i>i</i></sub>&gt; &amp;&amp; is_swappable_v&lt;T<sub><i>i</i></sub>&gt;</code> is
<code>true</code> for all <code><i>i</i></code>.</ins> The expression inside <code>noexcept</code> is equivalent to <code>noexcept(v.swap(w))</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
