<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3891: LWG 3870 breaks std::expected&lt;cv T, E&gt;</title>
<meta property="og:title" content="Issue 3891: LWG 3870 breaks std::expected&lt;cv T, E&gt;">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3891.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="3891"><a href="lwg-active.html#3891">3891</a>. LWG 3870 breaks <code>std::expected&lt;<i>cv</i> T, E&gt;</code></h3>
<p><b>Section:</b> 22.8.6.1 <a href="https://wg21.link/expected.object.general">[expected.object.general]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2023-02-19 <b>Last modified:</b> 2024-10-02</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#expected.object.general">issues</a> in [expected.object.general].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Currently the <code>value_type</code> of <code>std::expected</code> can be a <i>cv</i>-qualified type, which is  possibly intended.
However, LWG <a href="lwg-defects.html#3870" title="Remove voidify (Status: C++23)">3870</a><sup><a href="https://cplusplus.github.io/LWG/issue3870" title="Latest snapshot">(i)</a></sup> disallows <code>std::construct_at</code> to construct objects via <code><i>cv</i> T*</code>, which
breaks <code>std::expected&lt;<i>cv</i> T, E&gt;</code> because some operations are specified with <code>std::construct_at</code>
(22.8.6.4 <a href="https://wg21.link/expected.object.assign">[expected.object.assign]</a>, 22.8.6.5 <a href="https://wg21.link/expected.object.swap">[expected.object.swap]</a>).
<p/>
I think when <code>T</code> is <i>cv</i>-qualified, it would be better to store <code>std::remove_cv_t&lt;T&gt;</code> subobject
while sometimes (other than construction/destruction) access it via a <i>cv</i>-qualified glvalue, which can also avoid UB
associated with <code>const</code>/<code>volatile</code> objects.
</p>

<p><i>[2023-03-22; Reflector poll]</i></p>

<p>
Set priority to 2 after reflector poll.
"Not clear if all these wording changes are needed or desired."
"Unconvinced that the mixed-value-error swap should use <code>value()</code>,
source is destroyed immediately anyway. The else branch should use
<code>remove_cv_t</code> too."
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">

<p>
This wording is relative to <a href="https://wg21.link/N4928" title=" Working Draft, Standard for Programming Language C++">N4928</a>.
</p>

<blockquote class="note">
<p>
[<i>Drafting note</i>: When assignment and <code>swap</code> need to backup the old value by move construction,
the source should be considered <i>cv</i>-unqualified, as the backup mechanism is only used internally.]
</p>
</blockquote>

<ol>

<li><p>Modify 22.8.6.1 <a href="https://wg21.link/expected.object.general">[expected.object.general]</a> as indicated:</p>

<blockquote>
<pre>
[&hellip;]
bool <i>has_val</i>;      // <i>exposition only</i>
union {
  <ins>remove_cv_t&lt;</ins>T<ins>&gt;</ins> <i>val</i>;       // <i>exposition only</i>
  E <i>unex</i>;          // <i>exposition only</i>
};
[&hellip;]
</pre>
</blockquote>

</li>

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

<blockquote>
<pre>
constexpr expected&amp; operator=(const expected&amp; rhs);
</pre>
<blockquote>
<p>
-2- <i>Effects</i>:
</p>
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; If <code>this-&gt;has_value() &amp;&amp; rhs.has_value()</code> is <code>true</code>,
equivalent to <code><ins>value()</ins><del><i>val</i></del> = *rhs</code>.</p></li>
<li><p>[&hellip;]</p></li>
</ol>
</blockquote>
[&hellip;]
<pre>
constexpr expected&amp; operator=(expected&amp;&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-6- <i>Effects</i>:
</p>
<ol style="list-style-type: none">
<li><p>(6.1) &mdash; If <code>this-&gt;has_value() &amp;&amp; rhs.has_value()</code> is <code>true</code>,
equivalent to <code><ins>value()</ins><del><i>val</i></del> = std::move(*rhs)</code>.</p></li>
<li><p>[&hellip;]</p></li>
</ol>
</blockquote>
[&hellip;]
<pre>
template&lt;class U = T&gt;
  constexpr expected&amp; operator=(U&amp;&amp; v);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-10- <i>Effects</i>:
</p>
<ol style="list-style-type: none">
<li><p>(10.1) &mdash; If <code>has_value()</code> is <code>true</code>, equivalent to
 <code><ins>value()</ins><del><i>val</i></del> = std::forward&lt;U&gt;(v)</code>.</p></li>
<li><p>[&hellip;]</p></li>
</ol>
</blockquote>
</blockquote>

</li>

<li><p>Modify Table 64: <code>swap(expected&amp;)</code> effects  [tab:expected.object.swap] as indicated:</p>

<blockquote>

<table border="1">
<caption>Table 64 &mdash; <code>swap(expected&amp;)</code> effects  [tab:expected.object.swap]</caption>
<tr style="text-align:center">
<th></th>
<th><code>this-&gt;has_value()</code></th>
<th><code>!this-&gt;has_value()</code></th>
</tr>
<tr>
<td><code>rhs.has_value()</code></td>
<td>equivalent to: <code>using std::swap;</code><br/>
<code>swap(<ins>value()</ins><del><i>val</i></del>, rhs.<ins>value()</ins><del><i>val</i></del>);</code></td>
<td>calls <code>rhs.swap(*this)</code></td>
</tr>

<tr>
<td colspan="3" align="center">
<code>[&hellip;]</code>
</td>
</tr>
</table>
</blockquote>

</li>

<li><p>Modify 22.8.6.5 <a href="https://wg21.link/expected.object.swap">[expected.object.swap]</a> as indicated:</p>

<blockquote>
<pre>
constexpr void swap(expected&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-1- <i>Constraints</i>: [&hellip;]
<p/>
-2- <i>Effects</i>: See Table 64  [tab:expected.object.swap].
<p/>
For the case where <code>rhs.value()</code> is <code>false</code> and <code>this-&gt;has_value()</code> is <code>true</code>, equivalent to:
</p>
<blockquote>
<pre>
if constexpr (is_nothrow_move_constructible_v&lt;E&gt;) {
  E tmp(std::move(rhs.<i>unex</i>));
  destroy_at(addressof(rhs.<i>unex</i>));
  try {
    construct_at(addressof(rhs.<i>val</i>), std::move(<ins>value()</ins><del><i>val</i></del>));
    destroy_at(addressof(<i>val</i>));
    construct_at(addressof(<i>unex</i>), std::move(tmp));
  } catch(...) {
    construct_at(addressof(rhs.<i>unex</i>), std::move(tmp));
    throw;
  }
} else {
  T tmp(std::move(<i>val</i>));
  destroy_at(addressof(<i>val</i>));
  try {
    construct_at(addressof(<i>unex</i>), std::move(rhs.<i>unex</i>));
    destroy_at(addressof(rhs.<i>unex</i>));
    construct_at(addressof(rhs.<i>val</i>), std::move(tmp));
  } catch (...) {
    construct_at(addressof(<i>val</i>), std::move(tmp));
    throw;
  }
}
<i>has_val</i> = false;
rhs.<i>has_val</i> = true;
</pre>
</blockquote>
</blockquote>
</blockquote>

</li>

</ol>
</blockquote>

<p><i>[2024-10-02; Jonathan provides improved wording]</i></p>

<p>
Removed the use of <code class='backtick'>value()</code> in the [expected.object.swap] p2 <i>Effects</i>:
and added <code class='backtick'>remove_cv_t</code> to the local <code class='backtick'>T</code> in the <code class='backtick'>else</code>-branch.
</p>



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

<blockquote class="note">
<p>
[<i>Drafting note</i>: When assignment and <code>swap</code> need to backup the old value by move construction,
the source should be considered <i>cv</i>-unqualified, as the backup mechanism is only used internally.]
</p>
</blockquote>

<ol>

<li><p>Modify 22.8.6.1 <a href="https://wg21.link/expected.object.general">[expected.object.general]</a> as indicated:</p>

<blockquote>
<pre>
[&hellip;]
bool <i>has_val</i>;      // <i>exposition only</i>
union {
  <ins>remove_cv_t&lt;</ins>T<ins>&gt;</ins> <i>val</i>;       // <i>exposition only</i>
  E <i>unex</i>;          // <i>exposition only</i>
};
[&hellip;]
</pre>
</blockquote>

</li>

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

<blockquote>
<pre>
constexpr expected&amp; operator=(const expected&amp; rhs);
</pre>
<blockquote>
<p>
-2- <i>Effects</i>:
</p>
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; If <code>this-&gt;has_value() &amp;&amp; rhs.has_value()</code> is <code>true</code>,
equivalent to <code><ins>value()</ins><del><i>val</i></del> = *rhs</code>.</p></li>
<li><p>[&hellip;]</p></li>
</ol>
</blockquote>
[&hellip;]
<pre>
constexpr expected&amp; operator=(expected&amp;&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-6- <i>Effects</i>:
</p>
<ol style="list-style-type: none">
<li><p>(6.1) &mdash; If <code>this-&gt;has_value() &amp;&amp; rhs.has_value()</code> is <code>true</code>,
equivalent to <code><ins>value()</ins><del><i>val</i></del> = std::move(*rhs)</code>.</p></li>
<li><p>[&hellip;]</p></li>
</ol>
</blockquote>
[&hellip;]
<pre>
template&lt;class U = T&gt;
  constexpr expected&amp; operator=(U&amp;&amp; v);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-10- <i>Effects</i>:
</p>
<ol style="list-style-type: none">
<li><p>(10.1) &mdash; If <code>has_value()</code> is <code>true</code>, equivalent to
 <code><ins>value()</ins><del><i>val</i></del> = std::forward&lt;U&gt;(v)</code>.</p></li>
<li><p>[&hellip;]</p></li>
</ol>
</blockquote>
</blockquote>

</li>

<li><p>Modify Table 64: <code>swap(expected&amp;)</code> effects  [tab:expected.object.swap] as indicated:</p>

<blockquote>

<table border="1">
<caption>Table 64 &mdash; <code>swap(expected&amp;)</code> effects  [tab:expected.object.swap]</caption>
<tr style="text-align:center">
<th></th>
<th><code>this-&gt;has_value()</code></th>
<th><code>!this-&gt;has_value()</code></th>
</tr>
<tr>
<td><code>rhs.has_value()</code></td>
<td>equivalent to: <code>using std::swap;</code><br/>
<code>swap(<ins>value()</ins><del><i>val</i></del>, rhs.<ins>value()</ins><del><i>val</i></del>);</code></td>
<td>calls <code>rhs.swap(*this)</code></td>
</tr>

<tr>
<td colspan="3" align="center">
<code>[&hellip;]</code>
</td>
</tr>
</table>
</blockquote>

</li>

<li><p>Modify 22.8.6.5 <a href="https://wg21.link/expected.object.swap">[expected.object.swap]</a> as indicated:</p>

<blockquote>
<pre>
constexpr void swap(expected&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-1- <i>Constraints</i>: [&hellip;]
<p/>
-2- <i>Effects</i>: See Table 64  [tab:expected.object.swap].
<p/>
For the case where <code>rhs.value()</code> is <code>false</code> and <code>this-&gt;has_value()</code> is <code>true</code>, equivalent to:
</p>
<blockquote>
<pre>
if constexpr (is_nothrow_move_constructible_v&lt;E&gt;) {
  E tmp(std::move(rhs.<i>unex</i>));
  destroy_at(addressof(rhs.<i>unex</i>));
  try {
    construct_at(addressof(rhs.<i>val</i>), std::move(<i>val</i>));
    destroy_at(addressof(<i>val</i>));
    construct_at(addressof(<i>unex</i>), std::move(tmp));
  } catch(...) {
    construct_at(addressof(rhs.<i>unex</i>), std::move(tmp));
    throw;
  }
} else {
  <ins>remove_cv_t&lt;</ins>T<ins>&gt;</ins> tmp(std::move(<i>val</i>));
  destroy_at(addressof(<i>val</i>));
  try {
    construct_at(addressof(<i>unex</i>), std::move(rhs.<i>unex</i>));
    destroy_at(addressof(rhs.<i>unex</i>));
    construct_at(addressof(rhs.<i>val</i>), std::move(tmp));
  } catch (...) {
    construct_at(addressof(<i>val</i>), std::move(tmp));
    throw;
  }
}
<i>has_val</i> = false;
rhs.<i>has_val</i> = true;
</pre>
</blockquote>
</blockquote>
</blockquote>

</li>

</ol>





</body>
</html>
