<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2904: Make variant move-assignment more exception safe</title>
<meta property="og:title" content="Issue 2904: Make variant move-assignment more exception safe">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2904.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="2904"><a href="lwg-defects.html#2904">2904</a>. Make <code>variant</code> move-assignment more exception safe</h3>
<p><b>Section:</b> 22.6.3.4 <a href="https://wg21.link/variant.assign">[variant.assign]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> United States <b>Opened:</b> 2017-02-03 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#variant.assign">active issues</a> in [variant.assign].</p>
<p><b>View all other</b> <a href="lwg-index.html#variant.assign">issues</a> in [variant.assign].</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>
<b>Addresses US 119 and CH 7</b>

<p>The copy-assignment operator is very careful to not destroy the contained element until after a temporary has been constructed, 
which can be safely moved from. </p>
<p>This makes the <code>valueless_by_exception</code> state extremely rare, by design.</p>
<p>However, the same care and attention is not paid to the move-assignment operator, nor the assignment-from-deduced-value 
assignment template. This concern should be similarly important in these cases, especially the latter.</p>

<p>Proposed change: &mdash;</p>

<p><i>[2017-03-02, Kona, Casey comments and suggests wording]</i></p>

<p>
The wording below has been developed with <em>much</em> input from Tomasz.
</p>

<p><i>[Kona 2017-03-02]</i></p>

<p>Accepted as Immediate to resolve NB comment.</p>


<p id="res-2904"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4640">N4640</a>.
</p>
<ol>
<li><p>Modify 22.6.3.4 <a href="https://wg21.link/variant.assign">[variant.assign]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> Presentation of para 9 immediately below has been split into individual bullets.]
</p>
</blockquote>

<blockquote>
<pre>
variant&amp; operator=(const variant&amp; rhs);
</pre>
<blockquote>
<p>
<ins><ins>Let <code><i>j</i></code> be <code>rhs.index()</code>.</ins></ins>
<p/>
-1- <i>Effects:</i>
</p>
<ol style="list-style-type: none">
<li><p>(1.1) &mdash; If neither <code>*this</code> nor <code>rhs</code> holds a value, there is no effect. Otherwise,</p></li>
<li><p>(1.2) &mdash; if <code>*this</code> holds a value but <code>rhs</code> does not, destroys the value contained in 
<code>*this</code> and sets <code>*this</code> to not hold a value. Otherwise,</p></li>
<li><p>(1.3) &mdash; if <code>index() == <ins><i>j</i></ins><del>rhs.index()</del></code>, assigns the value contained in <code>rhs</code> to the value contained in 
<code>*this</code>. Otherwise,</p></li>
<li><p><ins>(1.?) &mdash; if <code>is_nothrow_copy_constructible_v&lt;T<sub><i>j</i></sub>&gt; || 
!is_nothrow_move_constructible_v&lt;T<sub><i>j</i></sub>&gt;</code> is <code>true</code>, equivalent to 
<code>emplace&lt;<i>j</i>&gt;(get&lt;<i>j</i>&gt;(rhs))</code>. Otherwise,</ins></p></li>
<li><p>(1.4) &mdash; <ins>equivalent to <code>operator=(variant(rhs))</code></ins><del>copies the value contained in <code>rhs</code> to 
a temporary, then destroys any value contained in 
<code>*this</code>. Sets <code>*this</code> to hold the same alternative index as <code>rhs</code> and initializes the value contained in 
<code>*this</code> as if direct-non-list-initializing an object of type <code>T<sub><i>j</i></sub></code> with 
<code>std::forward&lt;T<sub><i>j</i></sub>&gt;(TMP)</code>, with <code>TMP</code> being the temporary and <code>j</code> being 
<code>rhs.index()</code></del>.</p></li>
</ol>
<p>
-2- <i>Returns:</i> <code>*this.</code>
<p/>
-3- <i>Postconditions:</i> <code>index() == rhs.index()</code>.
<p/>
-4- <i>Remarks:</i> This function shall not participate in overload resolution unless 
<code>is_copy_constructible_v&lt;T<sub><i>i</i></sub>&gt; <del>&amp;&amp; is_move_constructible_v&lt;T<sub><i>i</i></sub>&gt;</del> 
&amp;&amp; is_copy_assignable_v&lt;T<sub><i>i</i></sub>&gt;</code> is <code>true</code> for all <code><i>i</i></code>.
</p>
<ol style="list-style-type: none">
<li><p><del>(4.1) &mdash; If an exception is thrown during the call [&hellip;]</del></p></li>
<li><p><del>(4.2) &mdash; If an exception is thrown during the call [&hellip;]</del></p></li>
<li><p><del>(4.3) &mdash; If an exception is thrown during the call [&hellip;]</del></p></li>
</ol>

</blockquote>
<pre>
variant&amp; operator=(variant&amp;&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
<ins><ins>Let <code><i>j</i></code> be <code>rhs.index()</code>.</ins></ins>
<p/>
-5- <i>Effects:</i>
</p>
<ol style="list-style-type: none">
<li><p>(5.1) &mdash; If neither <code>*this</code> nor <code>rhs</code> holds a value, there is no effect. Otherwise,</p></li>
<li><p>(5.2) &mdash; if <code>*this</code> holds a value but <code>rhs</code> does not, destroys the value contained in 
<code>*this</code> and sets <code>*this</code> to not hold a value. Otherwise,</p></li>
<li><p>(5.3) &mdash; if <code>index() == <ins><i>j</i></ins><del>rhs.index()</del></code>, assigns <code>get&lt;<i>j</i>&gt;(std::move(rhs))</code> 
to the value contained in <code>*this</code><del>, with <code><i>j</i></code> being <code>index()</code></del>. Otherwise,</p></li>
<li><p>(5.4) &mdash; <ins>equivalent to <code>emplace&lt;<i>j</i>&gt;(get&lt;<i>j</i>&gt;(std::move(rhs)))</code></ins><del>destroys any value contained in 
<code>*this</code>. Sets <code>*this</code> to hold the same alternative index as <code>rhs</code> and initializes the value contained 
in <code>*this</code> as if direct-non-list-initializing an object of type <code>T<sub><i>j</i></sub></code> with
<code>get&lt;<i>j</i>&gt;(std::move(rhs))</code> with <code><i>j</i></code> being <code>rhs.index()</code></del>.</p></li>
</ol>
<p>
[&hellip;]
</p>
</blockquote>
<p>
[&hellip;]
</p>
<pre>
template &lt;class T&gt; variant&amp; operator=(T&amp;&amp; t) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-8- [&hellip;]
<p/>
-9- <i>Effects:</i> 
</p>
<ol style="list-style-type: none">
<li><p>(9.1) &mdash; If *this holds a <code>T<sub><i>j</i></sub></code>, assigns <code>std::forward&lt;T&gt;(t)</code> to the value 
contained in <code>*this</code>. Otherwise,</p></li>
<li><p><ins>(9.?) &mdash; if <code>is_nothrow_constructible_v&lt;T<sub><i>j</i></sub>, T&gt; || 
!is_nothrow_move_constructible_v&lt;T<sub><i>j</i></sub>&gt;</code> is <code>true</code>, equivalent to 
<code>emplace&lt;<i>j</i>&gt;(std::forward&lt;T&gt;(t))</code>. Otherwise,</ins></p></li>
<li><p>(9.3) &mdash; <ins>equivalent to <code>operator=(variant(std::forward&lt;T&gt;(t)))</code></ins><del>destroys any value contained in <code>*this</code>, sets <code>*this</code> to hold the alternative type 
<code>T<sub><i>j</i></sub></code> as selected by the imaginary function overload resolution described above, and direct-initializes 
the contained value as if direct-non-list-initializing it with <code>std::forward&lt;T&gt;(t)</code></del>.</p></li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
