<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3991: variant's move assignment should not be guaranteed to produce a valueless by exception state</title>
<meta property="og:title" content="Issue 3991: variant's move assignment should not be guaranteed to produce a valueless by exception state">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3991.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#New">New</a> status.</em></p>
<h3 id="3991"><a href="lwg-active.html#3991">3991</a>. <code>variant</code>'s move assignment should not be guaranteed to produce a valueless by exception state</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#New">New</a>
 <b>Submitter:</b> Brian Bi <b>Opened:</b> 2023-08-29 <b>Last modified:</b> 2023-10-30</p>
<p><b>Priority: </b>3
</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#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
22.6.3.4 <a href="https://wg21.link/variant.assign">[variant.assign]</a> bullet 8.4 states that an alternative-changing move assignment on <code>std::variant</code> 
is equivalent to a call to <code>emplace</code>. However, bullet 10.1 also states that if the construction of the new 
alternative exits via an exception, then the destination of the assignment is guaranteed to become valueless 
by exception. This is inconsistent with the specification of <code>emplace</code>, 22.6.3.5 <a href="https://wg21.link/variant.mod">[variant.mod]</a>/11, 
which permits (but does not require) the variant to become valueless.
</p>

<p><i>[2023-10-30; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
"The remark is normatively redundant with the spec of <code>emplace</code>,
strike it."
</p>



<p id="res-3991"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4958" title=" Working Draft, Programming Languages — C++">N4958</a>.
</p>

<ol>

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

<blockquote>
<pre>
constexpr variant&amp; operator=(variant&amp;&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-6- Let <code><i>j</i></code> be <code>rhs.index()</code>.
<p/>
[&hellip;]
<p/>
-8- <i>Effects</i>:
</p>
<ol style="list-style-type:none">
<li><p>(8.1) &mdash; If neither <code>*this</code> nor <code>rhs</code> holds a value, there is no effect.</p></li>
<li><p>(8.2) &mdash; Otherwise, 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.</p></li>
<li><p>(8.3) &mdash; Otherwise, if <code>index() == <i>j</i></code>, assigns <code>get&lt;<i>j</i>&gt;(std::move(rhs))</code> 
to the value contained in <code>*this</code>.</p></li>
<li><p>(8.4) &mdash; Otherwise, equivalent to <code>emplace&lt;<i>j</i>&gt;(get&lt;<i>j</i>&gt;(std::move(rhs)))</code>.</p></li>
</ol>
<p>
[&hellip;]
<p/>
-10- <i>Remarks:</i> [&hellip;]
</p>
<ol style="list-style-type:none">
<li><p><del>(10.1) &mdash; If an exception is thrown during the call to <code>T<sub><i>j</i></sub></code>'s move construction 
(with <code><i>j</i></code> being <code>rhs.index()</code>), the <code>variant</code> will hold no value.</del></p></li>
<li><p><del>(10.2) &mdash;</del> If an exception is thrown during the call to <code>T<sub><i>j</i></sub></code>'s move assignment, 
the state of the contained value is as defined by the exception safety guarantee of <code>T<sub><i>j</i></sub></code>'s move 
assignment; <code>index()</code> will be <code><i>j</i></code>.</p></li>
</ol>
</blockquote>
</blockquote>

</li>

</ol>





</body>
</html>
