<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3836: std::expected&lt;bool, E1&gt; conversion constructor expected(const expected&lt;U, G&gt;&amp;) 
should take precedence over expected(U&amp;&amp;) with operator bool</title>
<meta property="og:title" content="Issue 3836: std::expected&lt;bool, E1&gt; conversion constructor expected(const expected&lt;U, G&gt;&amp;) 
should take precedence over expected(U&amp;&amp;) with operator bool">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3836.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++23">C++23</a> status.</em></p>
<h3 id="3836"><a href="lwg-defects.html#3836">3836</a>. <code>std::expected&lt;bool, E1&gt;</code> conversion constructor <code>expected(const expected&lt;U, G&gt;&amp;)</code> 
should take precedence over <code>expected(U&amp;&amp;)</code> with operator <code>bool</code></h3>
<p><b>Section:</b> 22.8.6.2 <a href="https://wg21.link/expected.object.cons">[expected.object.cons]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Hui Xie <b>Opened:</b> 2022-11-30 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>1
</p>
<p><b>View all other</b> <a href="lwg-index.html#expected.object.cons">issues</a> in [expected.object.cons].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The issue came up when implementing <code>std::expected</code> in libc++. Given the following example:
</p>
<blockquote><pre>
struct BaseError{};
struct DerivedError : BaseError{};

std::expected&lt;int, DerivedError&gt; e1(5);  
std::expected&lt;int, BaseError&gt; e2(e1);  // <span style="color:green;font-weight:bold">e2 holds 5</span>
</pre></blockquote>
<p>
In the above example, <code>e2</code> is constructed with the conversion constructor
</p>
<blockquote><pre>
expected::expected(const expected&lt;U, G&gt;&amp;)
</pre></blockquote>
<p>
and the value <code>5</code> is correctly copied into <code>e2</code> as expected.
<p/>
However, if we change the type from <code>int</code> to <code>bool</code>, the behaviour is very surprising.
</p>
<blockquote><pre>
std::expected&lt;bool, DerivedError&gt; e1(false);
std::expected&lt;bool, BaseError&gt; e2(e1);  // <span style="color:red;font-weight:bolder">e2 holds true</span>
</pre></blockquote>
<p>
In this example <code>e2</code> is constructed with
</p>
<blockquote><pre>
expected::expected(U&amp;&amp;)
</pre></blockquote>
<p>
together with
</p>
<blockquote><pre>
expected::operator bool() const
</pre></blockquote>
<p>
Instead of copying <code>e1</code>'s "<code>false</code>" into <code>e2</code>, it uses <code>operator bool</code>, which returns 
<code>true</code> in this case and <code>e2</code> would hold "<code>true</code>" instead.
<p/>
This is surprising behaviour given how inconsistent between <code>int</code> and <code>bool</code>.
<p/>
The reason why the second example uses a different overload is that the constructor 
<code>expected(const expected&lt;U, G&gt;&amp; rhs);</code> has the following constraint
(22.8.6.2 <a href="https://wg21.link/expected.object.cons">[expected.object.cons]</a> p17):
</p>
<blockquote>
<ol style="list-style-type: none">
<li><p>(17.3) &mdash; <code>is_constructible_v&lt;T, expected&lt;U, G&gt;&amp;&gt;</code> is <code>false</code>; and</p></li>
<li><p>(17.4) &mdash; <code>is_constructible_v&lt;T, expected&lt;U, G&gt;&gt;</code> is <code>false</code>; and</p></li>
<li><p>(17.5) &mdash; <code>is_constructible_v&lt;T, const expected&lt;U, G&gt;&amp;&gt;</code> is <code>false</code>; and</p></li>
<li><p>(17.6) &mdash; <code>is_constructible_v&lt;T, const expected&lt;U, G&gt;&gt;</code> is <code>false</code>; and</p></li>
<li><p>(17.7) &mdash; <code>is_convertible_v&lt;expected&lt;U, G&gt;&amp;, T&gt;</code> is <code>false</code>; and</p></li>
<li><p>(17.8) &mdash; <code>is_convertible_v&lt;expected&lt;U, G&gt;&amp;&amp;, T&gt;</code> is <code>false</code>; and</p></li>
<li><p>(17.9) &mdash; <code>is_convertible_v&lt;const expected&lt;U, G&gt;&amp;, T&gt;</code> is <code>false</code>; and</p></li>
<li><p>(17.10) &mdash; <code>is_convertible_v&lt;const expected&lt;U, G&gt;&amp;&amp;, T&gt;</code> is <code>false</code>; and</p></li>
</ol>
</blockquote>
<p>
Since <code>T</code> is <code>bool</code> in the second example, and <code>bool</code> can be constructed <code>from</code> 
<code>std::expected</code>, this overload will be removed. and the overload that takes <code>U&amp;&amp;</code> will be selected.
<p/>
I would suggest to special case <code>bool</code>, i.e.
</p>
<ul>
<li><p>(The above 8 constraints); or</p></li>
<li><p><code>is_same_v&lt;remove_cv_t&lt;T&gt;, bool&gt;</code> is <code>true</code></p></li>
</ul>
<p>
And we need to make sure this overload and the overload that takes <code>expected(U&amp;&amp;)</code> be mutually exclusive.
</p>

<p><i>[2023-01-06; Reflector poll]</i></p>

<p>
Set priority to 1 after reflector poll.
</p>
<p>
There was a mix of votes for P1 and P2 but also one for NAD
("The design of forward/repack construction for expected matches optional,
when if the stored value can be directly constructed, we use that.").
<code>std::optional&lt;bool&gt;</code> is similarly affected.
Any change should consider the effects on
<code>expected&lt;expected&lt;&gt;&gt;</code> use cases.
</p>

<p><i>[Issaquah 2023-02-08; Jonathan provides wording]</i></p>

<p><i>[Issaquah 2023-02-09; LWG]</i></p>

<p>Move to Immediate for C++23</p>

<p><i>[2023-02-13 Approved at February 2023 meeting in Issaquah. Status changed: Immediate &rarr; WP.]</i></p>



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

<ol>
<li><p>Modify 22.5.3.2 <a href="https://wg21.link/optional.ctor">[optional.ctor]</a> as indicated:</p>
<blockquote>
<pre><code>
template&lt;class U = T&gt; constexpr explicit(<i>see below</i>) optional(U&amp;&amp; v);
</code></pre>
<p>-23- <i>Constraints</i>:
<blockquote class="note">
[<i>Drafting note</i>: Change this paragraph to a bulleted list.]
</blockquote>
<ul>
<li><ins>(23.1) &mdash;</ins>
<code>is_constructible_v&lt;T, U&gt;</code> is <code>true</code>,
</li>
<li><ins>(23.2) &mdash;</ins>
<code>is_same_v&lt;remove_cvref_t&lt;U&gt;, in_place_t&gt;</code> is <code>false</code>,
<del>and</del>
</li>
<li><ins>(23.3) &mdash;</ins>
<code>is_same_v&lt;remove_cvref_t&lt;U&gt;, optional&gt;</code> is <code>false</code><ins>, and</ins>
</li>
<li><ins>(23.4) &mdash;
if <code>T</code> is <i>cv</i> <code>bool</code>,
<code>remove_cvref_t&lt;U&gt;</code> is not a specialization of <code>optional</code></ins>.
</li>
</ul>
</p>
<p>-24- <i>Effects</i>:
Direct-non-list-initializes the contained value with
<code>std::forward&gt;U&gt;(v)</code>.
</p>
<p>-25- <i>Postconditions</i>:
<code>*this</code> has a value.
</p>
<p>-26- <i>Throws</i>:
Any exception thrown by the selection constructor of <code>T</code>.
</p>
<p>-27- <i>Remarks</i>: If <code>T</code>'s selected constructor is a constexpr constructor,
this constructor is a constexpr constructor.
The expression inside <code>explicit</code> is equivalent to:<br/>
<pre>  !is_convertible_v&lt;U, T&gt;</pre>
</p>

<pre><code>
template&lt;class U&gt; constexpr explicit(<i>see below</i>) optional(const optional&lt;U&gt;&amp; rhs);
</code></pre>
<p>-28- <i>Constraints</i>:
<ul>
<li>(28.1) &mdash;
<code>is_constructible_v&lt;T, const U&amp;&gt;</code> is <code>true</code>,
and
</li>
<li>(28.1) &mdash;
<ins>if <code>T</code> is not <i>cv</i> <code>bool</code>,</ins>
<code><i>converts-from-any-cvref</i>&lt;T, optional&lt;U&gt;&gt;</code> is <code>false</code>.
</li>
</ul>
</p>
<p>-29- <i>Effects</i>:
If <code>rhs</code> contains a value,
direct-non-list-initializes the contained value with
<code>*rhs</code>.
</p>
<p>-30- <i>Postconditions</i>:
<code>rhs.has_value() == this->has_value()</code>.
</p>
<p>-31- <i>Throws</i>:
Any exception thrown by the selection constructor of <code>T</code>.
</p>
<p>-32- <i>Remarks</i>: The expression inside <code>explicit</code> is equivalent to:<br/>
<pre>  !is_convertible_v&lt;const U&amp;, T&gt;</pre>
</p>

<pre><code>
template&lt;class U&gt; constexpr explicit(<i>see below</i>) optional(optional&lt;U&gt;&amp;&amp; rhs);
</code></pre>
<p>-33- <i>Constraints</i>:
<ul>
<li>(33.1) &mdash;
<code>is_constructible_v&lt;T, U&gt;</code> is <code>true</code>,
and
</li>
<li>(33.1) &mdash;
<ins>if <code>T</code> is not <i>cv</i> <code>bool</code>,</ins>
<code><i>converts-from-any-cvref</i>&lt;T, optional&lt;U&gt;&gt;</code> is <code>false</code>.
</li>
</ul>
</p>
<p>-34- <i>Effects</i>:
If <code>rhs</code> contains a value,
direct-non-list-initializes the contained value with
<code>std::move(*rhs)</code>.
<code>rhs.has_value()</code> is unchanged.
</p>
<p>-35- <i>Postconditions</i>:
<code>rhs.has_value() == this->has_value()</code>.
</p>
<p>-36- <i>Throws</i>:
Any exception thrown by the selection constructor of <code>T</code>.
</p>
<p>-37- <i>Remarks</i>: The expression inside <code>explicit</code> is equivalent to:<br/>
<pre>  !is_convertible_v&lt;U, T&gt;</pre>
</p>

</blockquote>
</li>

<li><p>Modify 22.8.6.2 <a href="https://wg21.link/expected.object.cons">[expected.object.cons]</a> as indicated:</p>
<blockquote>
<pre><code>
template&lt;class U, class G&gt;
  constexpr explicit(see below) expected(const expected&lt;U, G&gt;&amp; rhs);
template&lt;class U, class G&gt;
  constexpr explicit(see below) expected(expected&lt;U, G&gt;&amp;&amp; rhs);
</code></pre>
<p>-17- Let:
<ul>
<li>(17.1) &mdash;
<code>UF</code> be <code>const U&amp;</code> for the first overload and <code>U</code> for the second overload.
</li>
<li>(17.2) &mdash;
<code>GF</code> be <code>const G&amp;</code> for the first overload and <code>G</code> for the second overload.
</li>
</ul>
</p>
<p>-18- <i>Constraints</i>:
<ul>
<li>(18.1) &mdash;
<code>is_constructible_v&lt;T, UF&gt;</code> is <code>true</code>; and
</li>
<li>(18.2) &mdash;
<code>is_constructible_v&lt;E, GF&gt;</code> is <code>true</code>; and
</li>
<li>(18.3) &mdash;
<ins>if <code>T</code> is not <i>cv</i> <code>bool</code>,</ins>
<code><i>converts-from-any-cvref</i>&lt;T, expected&lt;U, G&gt;&gt;</code> is <code>false</code>; and
</li>
<li>(18.4) &mdash;
<code>is_constructible_v&lt;unexpected&lt;E&gt;, expected&lt;U, G&gt;&amp;&gt;</code> is <code>false</code>; and
</li>
<li>(18.5) &mdash;
<code>is_constructible_v&lt;unexpected&lt;E&gt;, expected&lt;U, G&gt;&gt;</code> is <code>false</code>; and
</li>
<li>(18.6) &mdash;
<code>is_constructible_v&lt;unexpected&lt;E&gt;, const expected&lt;U, G&gt;&amp;&gt;</code> is <code>false</code>; and
</li>
<li>(18.7) &mdash;
<code>is_constructible_v&lt;unexpected&lt;E&gt;, const expected&lt;U, G&gt;&gt;</code> is <code>false</code>.
</li>
</ul>
</p>
<p>-19- <i>Effects</i>:
If <code>rhs.has_value()</code>,
direct-non-list-initializes <code><i>val</i></code> with
<code>std::forward&gt;UF&gt;(*rhs)</code>.
Otherwise, direct-non-list-initializes <code><i>unex</i></code>
with <code>std::forward&gt;GF&gt;(rhs.error())</code>.
</p>
<p>-20- <i>Postconditions</i>:
<code>rhs.has_value()</code> is unchanged;
<code>rhs.has_value() == this->has_value()</code> is <code>true</code>.
</p>
<p>-21- <i>Throws</i>:
Any exception thrown by the initialization of <code><i>val</i></code> or <code><i>unex</i></code>.
</p>
<p>-22- <i>Remarks</i>: The expression inside <code>explicit</code> is equivalent to
<code>!is_convertible_v&lt;UF, T&gt; || !is_convertible_v&lt;GF, E&gt;</code>.
</p>

<pre><code>
template&lt;class U = T&gt;
  constexpr explicit(!is_convertible_v&lt;U, T&gt;) expected(U&amp;&amp; v);
</code></pre>
<p>-23- <i>Constraints</i>:
<ul>
<li>(23.1) &mdash;
<code>is_same_v&lt;remove_cvref_t&lt;U&gt;, in_place_t&gt;</code> is <code>false</code>; and
</li>
<li>(23.2) &mdash;
<code>is_same_v&lt;expected, remove_cvref_t&lt;U&gt;&gt;</code> is <code>false</code>; and
</li>
<li>(23.3) &mdash;
<code>is_constructible_v&lt;T, U&gt;</code> is <code>true</code>; and
</li>
<li>(23.4) &mdash;
<code>remove_cvref_t&lt;U&gt;</code> is not a specialization of <code>unexpected</code><ins>; and</ins>
</li>
<li><ins>(23.5) &mdash;
if <code>T</code> is <i>cv</i> <code>bool</code>,
<code>remove_cvref_t&lt;U&gt;</code> is not a specialization of <code>expected</code></ins>.
</li>
</ul>
</p>
<p>-24- <i>Effects</i>:
Direct-non-list-initializes <code><i>val</i></code> with
<code>std::forward&gt;U&gt;(v)</code>.
</p>
<p>-25- <i>Postconditions</i>:
<code>has_value()</code> is <code>true</code>.
</p>
<p>-26- <i>Throws</i>:
Any exception thrown by the initialization of <code><i>val</i></code>.
</p>

</blockquote>
</li>
</ol>





</body>
</html>
