<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3746: optional's spaceship with U with a type derived from optional 
causes infinite constraint meta-recursion</title>
<meta property="og:title" content="Issue 3746: optional's spaceship with U with a type derived from optional 
causes infinite constraint meta-recursion">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3746.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="3746"><a href="lwg-defects.html#3746">3746</a>. <code>optional</code>'s spaceship with <code>U</code> with a type derived from <code>optional</code> 
causes infinite constraint meta-recursion</h3>
<p><b>Section:</b> 22.5.9 <a href="https://wg21.link/optional.comp.with.t">[optional.comp.with.t]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Ville Voutilainen <b>Opened:</b> 2022-07-25 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#optional.comp.with.t">issues</a> in [optional.comp.with.t].</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>
What ends up happening is that the constraints of 
<code>operator&lt;=&gt;(const optional&lt;T&gt;&amp;, const U&amp;)</code> end up 
in <code>three_way_comparable_with</code>, and then in <code><i>partially-ordered-with</i></code>, 
and the expressions there end up performing a conversion from <code>U</code> to an 
<code>optional</code>, and we end up instantiating the same <code>operator&lt;=&gt;</code>
again, evaluating its constraints again, until the compiler bails out.
<p/>
See an <a href="https://godbolt.org/z/T7f4sr8jv">online example here</a>.
<p/>
All implementations end up with infinite meta-recursion.
<p/>
The solution to the problem is to stop the meta-recursion by constraining
the spaceship with <code>U</code> so that <code>U</code> is not publicly and unambiguously derived
from a specialization of optional, SFINAEing that candidate out, and letting 
22.5.7 <a href="https://wg21.link/optional.relops">[optional.relops]</a>/20 perform the comparison instead.
</p>

<p><i>[2022-08-23; Reflector poll]</i></p>

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

<p><i>[2022-11-12 Approved at November 2022 meeting in Kona. Status changed: Voting &rarr; WP.]</i></p>



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

<blockquote class="note">
<p>
[<i>Drafting note:</i> This proposed wording removes the only use of <code><i>is-optional</i></code>. ]
</p>
</blockquote>

<ol>
<li><p>Modify 22.5.2 <a href="https://wg21.link/optional.syn">[optional.syn]</a>, header <code>&lt;optional&gt;</code> synopsis, as indicated:</p>

<blockquote>
<pre>
[&hellip;]
namespace std {
  <i>// 22.5.3 <a href="https://wg21.link/optional.optional">[optional.optional]</a>, class template optional</i>
  template&lt;class T&gt;
    class optional;

  <del>template&lt;class T&gt;
    constexpr bool <i>is-optional</i> = false; <i>// exposition only</i>
  template&lt;class T&gt;
    constexpr bool <i>is-optional</i>&lt;optional&lt;T&gt;&gt; = true; <i>// exposition only</i></del>
  <ins>template&lt;class T&gt;
    concept <i>is-derived-from-optional</i> = requires(const T&amp; t) { <i>// exposition only</i>
      []&lt;class U&gt;(const optional&lt;U&gt;&amp;){ }(t);
    };</ins>
  [&hellip;]
  <i>// 22.5.9 <a href="https://wg21.link/optional.comp.with.t">[optional.comp.with.t]</a>, comparison with T</i>
  [&hellip;]
  template&lt;class T, class U&gt; constexpr bool operator&gt;=(const T&amp;, const optional&lt;U&gt;&amp;);
  template&lt;class T, class U&gt; requires (!<i>is-<ins>derived-from-</ins>optional</i>&lt;U&gt;) &amp;&amp; three_way_comparable_with&lt;T, U&gt;
    constexpr compare_three_way_result_t&lt;T, U&gt;
      operator&lt;=&gt;(const optional&lt;T&gt;&amp;, const U&amp;);
  [&hellip;]
}
</pre>
</blockquote>
</li>

<li><p>Modify 22.5.9 <a href="https://wg21.link/optional.comp.with.t">[optional.comp.with.t]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class T, class U&gt; requires (!<i>is-<ins>derived-from-</ins>optional</i>&lt;U&gt;) &amp;&amp; three_way_comparable_with&lt;T, U&gt;
  constexpr compare_three_way_result_t&lt;T, U&gt;
    operator&lt;=&gt;(const optional&lt;T&gt;&amp;, const U&amp;);
</pre>
<blockquote>
<p>
-25- <i>Effects:</i> Equivalent to: <code>return x.has_value() ? *x &lt;=&gt; v : strong_ordering::less;</code>
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
