<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3566: Constraint recursion for operator&lt;=&gt;(optional&lt;T&gt;, U)</title>
<meta property="og:title" content="Issue 3566: Constraint recursion for operator&lt;=&gt;(optional&lt;T&gt;, U)">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3566.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="3566"><a href="lwg-defects.html#3566">3566</a>. Constraint recursion for <code>operator&lt;=&gt;(optional&lt;T&gt;, U)</code></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> Casey Carter <b>Opened:</b> 2021-06-07 <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>
The three-way-comparison operator for optionals and non-optionals is defined in [optional.comp.with.t] paragraph 25:
</p>
<blockquote><pre>
template&lt;class T, three_way_comparable_with&lt;T&gt; U&gt;
  constexpr compare_three_way_result_t&lt;T, U&gt;
    operator&lt;=&gt;(const optional&lt;T&gt;&amp; x, const U&amp; v);
</pre>
<blockquote><p>
-25- <i>Effects:</i> Equivalent to: <code>return bool(x) ? *x &lt;=&gt; v : strong_ordering::less;</code>
</p></blockquote>
</blockquote>
<p>
Checking <code>three_way_comparable_with</code> in particular requires checking that <code>x &lt; v</code> is well-formed 
for an lvalue <code>const optional&lt;T&gt;</code> and lvalue <code>const U</code>. <code>x &lt; v</code> can be rewritten as 
<code>(v &lt;=&gt; x) &gt; 0</code>. If <code>U</code> is a specialization of <code>optional</code>, this overload could be used for 
<code>v &lt;=&gt; x</code>, but first we must check the constraints&hellip;
</p>
<p>
The straightforward fix for this recursion seems to be to refuse to check <code>three_way_comparable_with&lt;T&gt;</code> 
when <code>U</code> is a specialization of <code>optional</code>. MSVC has been shipping such a fix; our initial tests for 
this new operator triggered the recursion.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>
This wording is relative to <a href="https://wg21.link/n4885">N4885</a>.
</p>

<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;]
<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>=(const optional&lt;T&gt;&amp;, const U&amp;);
template&lt;class T, class U&gt; constexpr bool operator>=(const T&amp;, const optional&lt;U&gt;&amp;);
template&lt;class T, <del>three_way_comparable_with&lt;T&gt;</del><ins>class</ins> U&gt;
  <ins>requires <i>see below</i></ins>
    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, <del>three_way_comparable_with&lt;T&gt;</del><ins>class</ins> U&gt;
  <ins>requires (!<i>is-specialization-of</i>&lt;U, optional&gt;) &amp;&amp; three_way_comparable_with&lt;T, U&gt;</ins>
    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>
<ins>-?- The exposition-only trait template <code><i>is-specialization-of</i>&lt;A, B&gt;</code> is a constant expression 
with value <code>true</code> when <code>A</code> denotes a specialization of the class template <code>B</code>, and <code>false</code> 
otherwise.</ins>
<p/>
-25- <i>Effects:</i> Equivalent to: <code>return bool(x) ? *x &lt;=&gt; v : strong_ordering::less;</code>
</p>
</blockquote>
</blockquote>
</li>

</ol>
</blockquote>

<p><i>[2021-06-14; Improved proposed wording based on reflector discussion]</i></p>


<p><i>[2021-06-23; Reflector poll]</i></p>

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

<p><i>[2021-10-14 Approved at October 2021 virtual plenary. Status changed: Voting &rarr; WP.]</i></p>



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

<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;]
<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;

<ins>template&lt;class T&gt; 
  constexpr bool <i>is-optional</i> = false;               <i>// exposition only</i></ins>
<ins>template&lt;class T&gt; 
  constexpr bool <i>is-optional</i>&lt;optional&lt;T&gt;&gt; = true;   <i>// exposition only</i></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>=(const optional&lt;T&gt;&amp;, const U&amp;);
template&lt;class T, class U&gt; constexpr bool operator>=(const T&amp;, const optional&lt;U&gt;&amp;);
template&lt;class T, <del>three_way_comparable_with&lt;T&gt;</del><ins>class</ins> U&gt;
  <ins>requires (!<i>is-optional</i>&lt;U&gt;) &amp;&amp; three_way_comparable_with&lt;T, U&gt;</ins>
    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, <del>three_way_comparable_with&lt;T&gt;</del><ins>class</ins> U&gt;
  <ins>requires (!<i>is-optional</i>&lt;U&gt;) &amp;&amp; three_way_comparable_with&lt;T, U&gt;</ins>
    constexpr compare_three_way_result_t&lt;T, U&gt;
      operator&lt;=&gt;(const optional&lt;T&gt;&amp; x, const U&amp; v);
</pre>
<blockquote>
<p>
-25- <i>Effects:</i> Equivalent to: <code>return bool(x) ? *x &lt;=&gt; v : strong_ordering::less;</code>
</p>
</blockquote>
</blockquote>
</li>

</ol>




</body>
</html>
