<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4222: expected constructor from a single value missing a constraint</title>
<meta property="og:title" content="Issue 4222: expected constructor from a single value missing a constraint">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4222.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#WP">WP</a> status.</em></p>
<h3 id="4222"><a href="lwg-defects.html#4222">4222</a>. <code class='backtick'>expected</code> constructor from a single value missing a constraint</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#WP">WP</a>
 <b>Submitter:</b> Bronek Kozicki <b>Opened:</b> 2025-03-12 <b>Last modified:</b> 2025-06-23</p>
<p><b>Priority: </b>Not Prioritized
</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#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
When an <code class='backtick'>expected</code> object is initialized with a constructor taking first parameter of type <code class='backtick'>unexpect_t</code> , 
the expectation is that the object will be always initialized in disengaged state (i.e. the user expected 
postcondition is that <code class='backtick'>has_value()</code> will be <code class='backtick'>false</code>), as in the example:
</p>
<blockquote><pre>
struct T { explicit T(auto) {} };
struct E { E() {} };

int main() {
  expected&lt;T, E&gt; a(unexpect);
  assert(!a.has_value());
}
</pre></blockquote>
<p>
This does not hold when both value type <code class='backtick'>T</code> and error type <code class='backtick'>E</code> have certain properties. Observe:
</p>
<blockquote><pre>
struct T { explicit T(auto) {} };
struct E { E(int) {} }; // <span style="color:red;font-weight:bolder">Only this line changed from the above example</span>

int main() {
  expected&lt;T, E&gt; a(unexpect);
  assert(!a.has_value()); // <span style="color:red;font-weight:bolder">This assert will now fail</span>
}
</pre></blockquote>
<p>
In the example above the overload resolution of <code class='backtick'>a</code> finds the universal single parameter constructor for 
initializing <code class='backtick'>expected</code> in engaged state (22.8.6.2 <a href="https://wg21.link/expected.object.cons">[expected.object.cons]</a> p23):
</p>
<blockquote><pre>
template&lt;class U = remove_cv_t&lt;T&gt;&gt;
  constexpr explicit(!is_convertible_v&lt;U, T&gt;) expected(U&amp;&amp; v);
</pre></blockquote>
<p>
This constructor has a list of constraints which does not mention <code class='backtick'>unexpect_t</code> (but it mentions e.g. <code class='backtick'>unexpected</code> and 
<code class='backtick'>in_place_t</code>). Email exchange with the author of <code class='backtick'>expected</code> confirmed that it is an omission.
<p/>
The proposed resolution is to add the following additional constraint to this constructor:
</p>
<blockquote><p>
<ins><code>is_same_v&lt;remove_cvref_t&lt;U&gt;, unexpect_t&gt;</code> is <code class='backtick'>false</code></ins>
</p></blockquote>
<p>
This will result in the above, most likely buggy, program to become ill-formed. If the user intent was for the object 
to be constructed in an engaged state, passing <code class='backtick'>unexpect_t</code> to the <code class='backtick'>T</code> constructor, they can fix the compilation error 
like so:
</p>
<blockquote><pre>
expected&lt;T, E&gt; a(in_place, unexpect);
</pre></blockquote>

<p><i>[2025-06-13; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after nine votes in favour during reflector poll.
</p>

<p><i>[Sofia 2025-06-21; Status changed: Voting &rarr; WP.]</i></p>



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

<ol>

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

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





</body>
</html>
