<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3069: Move assigning variant's subobject corrupts data</title>
<meta property="og:title" content="Issue 3069: Move assigning variant's subobject corrupts data">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3069.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="3069"><a href="lwg-active.html#3069">3069</a>. Move assigning <code>variant</code>'s subobject corrupts data</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> Antony Polukhin <b>Opened:</b> 2018-02-20 <b>Last modified:</b> 2020-09-06</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>
<code>variant::emplace</code> functions in 22.6.3.5 <a href="https://wg21.link/variant.mod">[variant.mod]</a> destroy the currently
contained value before initializing it to a new value. Assignments in 
22.6.3.4 <a href="https://wg21.link/variant.assign">[variant.assign]</a> are described in terms on <code>emplace</code>.
<p/>
This leads to situation, when move/copy assigning subobject from <code>variant</code> into the same 
<code>variant</code> corrupts data:
</p>
<blockquote><pre>
#include &lt;variant&gt;
#include &lt;memory&gt;
#include &lt;iostream&gt;

using str_t = std::string;
using str_ptr_t = std::unique_ptr&lt;str_t&gt;;
using var_t = std::variant&lt;str_t, str_ptr_t&gt;;

int main() 
{
  var_t v = str_ptr_t{
    new str_t{"Long string that does not fit into SS buffer and forces dynamic allocation"}
  };

  // Any of the following lines corrupt the variant's content:
  v = *std::get&lt;str_ptr_t&gt;(v);
  //v = std::move(*std::get&lt;str_ptr_t&gt;(v));

  std::cout &lt;&lt; std::get&lt;str_t&gt;(v) &lt;&lt; std::endl; // SEGV - 'str_t' inside 'v' is invalid
}
</pre></blockquote>
<p>
Such behavior confuses users, especially those users who are used to
<code>boost::variant</code>'s behavior. Consider making <code>variant</code> assignments safer
by defining them close to copy-and-swap.
</p>

<p><i>[2018-06-18 after reflector discussion]</i></p>

<p>Priority set to 3; Antony volunteered to write a paper for Rapperswil.</p>


<p id="res-3069"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4727">N4727</a>.
</p>

<ol>
<li><p>Change 22.6.3.4 <a href="https://wg21.link/variant.assign">[variant.assign]</a> as indicated:</p>
<blockquote>
<pre>
variant&amp; operator=(const variant&amp; rhs);
</pre>
<blockquote>
<p>
-1- Let <code><i>j</i></code> be <code>rhs.index()</code>.
<p/>
-2- <i>Effects:</i>
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; If neither <code>*this</code> nor <code>rhs</code> holds a value, there is no effect.</p></li>
<li><p>(2.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>(2.3) &mdash; Otherwise, if <code>index() == <i>j</i></code>, assigns the value contained in <code>rhs</code> 
to the value contained in <code>*this</code>.</p></li>
<li><p><del>(2.4) &mdash; Otherwise, if either <code>is_nothrow_copy_constructible_v&lt;T<sub><i>j</i></sub>&gt;</code> 
is <code>true</code> or <code>is_nothrow_move_constructible_v&lt;T<sub><i>j</i></sub>&gt;</code> is 
<code>false</code>, equivalent to <code>emplace&lt;<i>j</i>&gt;(get&lt;<i>j</i>&gt;(rhs))</code>.</del></p></li>
<li><p>(2.5) &mdash; Otherwise, equivalent to <ins><code>emplace&lt;<i>j</i>&gt;(T<sub><i>j</i></sub>{get&lt;<i>j</i>&gt;(rhs)})</code></ins><del><code>operator=(variant(rhs))</code></del>.</p></li>
</ol>
[&hellip;]
</p>
</blockquote>
<pre>
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/>
-7- <i>Effects:</i>
<ol style="list-style-type: none">
<li><p>(7.1) &mdash; If neither <code>*this</code> nor <code>rhs</code> holds a value, there is no effect.</p></li>
<li><p>(7.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>(7.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>(7.4) &mdash; Otherwise, equivalent to 
<code>emplace&lt;<i>j</i>&gt;(<ins>T<sub><i>j</i></sub>{</ins>get&lt;<i>j</i>&gt;(std::move(rhs))<ins>}</ins>)</code>.</p></li>
</ol>
[&hellip;]
</p>
</blockquote>
<pre>
</pre>
<blockquote>
<p>
-10- Let <code>T<sub><i>j</i></sub></code> be a type that is determined as follows: build an imaginary function 
<code><i>FUN</i>(T<sub><i>i</i></sub>)</code> for each alternative type <code>T<sub><i>i</i></sub></code>. The overload 
<code><i>FUN</i>(T<sub><i>j</i></sub>)</code> selected by overload resolution for the expression 
<code><i>FUN</i>(std::forward&lt;T&gt;(t))</code> defines the alternative <code>T<sub><i>j</i></sub></code> which is the 
type of the contained value after assignment.
<p/>
-11- <i>Effects:</i>
<ol style="list-style-type: none">
<li><p>(11.1) &mdash; If <code>*this</code> 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>.</p></li>
<li><p><del>(11.2) &mdash; Otherwise, 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>.</del></p></li>
<li><p>(11.3) &mdash; Otherwise, equivalent to 
<code><ins>emplace&lt;<i>j</i>&gt;(T<sub><i>j</i></sub>{std::forward&lt;T&gt;(t)})</ins><del>operator=(variant(std::forward&lt;T&gt;(t)))</del></code>.</p></li>
</ol>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
