<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3431: &lt;=&gt; for containers should require three_way_comparable&lt;T&gt; instead of &lt;=&gt;</title>
<meta property="og:title" content="Issue 3431: &lt;=&gt; for containers should require three_way_comparable&lt;T&gt; instead of &lt;=&gt;">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3431.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="3431"><a href="lwg-defects.html#3431">3431</a>. <code>&lt;=&gt;</code> for containers should require <code>three_way_comparable&lt;T&gt;</code> instead of <code>&lt;=&gt;</code></h3>
<p><b>Section:</b> 23.2.2.4 <a href="https://wg21.link/container.opt.reqmts">[container.opt.reqmts]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2020-04-17 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>2
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The precondition for <code>&lt;=&gt;</code> on containers is:
<p/>
"Either <code>&lt;=&gt;</code> is defined for values of type (possibly <code>const</code>) <code>T</code>, 
or <code>&lt;</code> is defined for values of type (possibly <code>const</code>) <code>T</code> and 
<code>&lt;</code> is a total ordering relationship."
<p/>
I don't think <code>&lt;=&gt;</code> is sufficient, because <code><i>synth-three-way</i></code> won't 
use <code>&lt;=&gt;</code> unless <code>three_way_comparable&lt;T&gt;</code> is satisfied, which requires
<code><i>weakly-equality-comparable-with</i>&lt;T, T&gt;</code> as well as <code>&lt;=&gt;</code>.
<p/>
So to use <code>&lt;=&gt;</code> I think the type also requires <code>==</code>, or more precisely, it
must satisfy <code>three_way_comparable</code>.
<p/>
The problem becomes clearer with the following example:
</p>
<blockquote><pre>
#include &lt;compare&gt;
#include &lt;vector&gt;

struct X
{
  friend std::strong_ordering operator&lt;=&gt;(X, X) { return std::strong_ordering::equal; }
};

std::vector&lt;X&gt; v(1);
std::strong_ordering c = v &lt;=&gt; v;
</pre></blockquote>
<p>
This doesn't compile, because despite <code>X</code> meeting the preconditions for <code>&lt;=&gt;</code> in 
[tab:container.opt], <code><i>synth-three-way</i></code> will return <code>std::weak_ordering</code>.
<p/>
Here is another example:
</p>
<blockquote><pre>
#include &lt;compare&gt;
#include &lt;vector&gt;

struct X
{
  friend bool operator&lt;(X, X) { return true; } // <i>The return value is intentional, see below</i>
  friend std::strong_ordering operator&lt;=&gt;(X, X) { return std::strong_ordering::equal; }
};

std::vector&lt;X&gt; v(1);
std::weak_ordering c = v &lt;=&gt; v;
</pre></blockquote>
<p>
This meets the precondition because it defines <code>&lt;=&gt;</code>, but the result of <code>&lt;=&gt;</code> 
on <code>vector&lt;X&gt;</code> will be nonsense, because <code><i>synth-three-way</i></code> will use 
<code>operator&lt;</code> not <code>operator&lt;=&gt;</code> and that defines a broken ordering.
<p/>
So we're stating a precondition which implies "if you do this, you don't get garbage results" and 
then we give garbage results anyway.
<p/>
The proposed resolution is one way to fix that, by tightening the precondition so that it matches 
what <code><i>synth-three-way</i></code> actually does.
</p>

<p><i>[2020-04-25 Issue Prioritization]</i></p>

<p>Priority to 2 after reflector discussion.</p>

<p>
<strong>Previous resolution [SUPERSEDED]:</strong>
</p>
<blockquote class="note">
<p>
This wording is relative to <a href="https://wg21.link/N4861" title=" Working Draft, Standard for Programming Language C++">N4861</a>.
</p>

<ol>
<li><p>Modify 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a>, Table [tab:container.opt], as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 75: Optional container operations [tab:container.opt]</caption>
<tr>
<th align="center">Expression</th>
<th align="center">Return type</th>
<th align="center">Operational<br/>semantics</th>
<th align="center">Assertion/note<br/>pre-/post-condition</th>
<th align="center">Complexity</th>
</tr>

<tr>
<td colspan="5" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td>
<code>a &lt;=&gt; b</code>
</td>
<td>
<code><i>synth-three-way-result</i>&lt;value_type&gt;</code>
</td>
<td>
<code>lexicographical_compare_three_way(<br/>
a.begin(), a.end(), b.begin(), b.end(),<br/>
<i>synth-three-way</i>)</code>
</td>
<td>
<i>Preconditions:</i> Either
<del><code>&lt;=&gt;</code> is defined for<br/>
values of type (possibly <code>const</code>)</del><br/>
<code>T</code> <ins>satisfies <code>three_way_comparable</code></ins>,<br/>
or <code>&lt;</code> is defined for values of type<br/>
(possibly <code>const</code>) <code>T</code> and<br/> 
<code>&lt;</code> is a total ordering relationship.
</td>
<td>
linear
</td>
</tr>

<tr>
<td colspan="5" align="center">
<code>&hellip;</code>
</td>
</tr>
</table>

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

<p><i>[2022-04-24; Daniel rebases wording on <a href="https://wg21.link/N4910" title=" Working Draft, Standard for Programming Language C++">N4910</a>]</i></p>

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

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

<ol>
<li><p>Modify 23.2.2.4 <a href="https://wg21.link/container.opt.reqmts">[container.opt.reqmts]</a> as indicated:</p>

<blockquote>
<pre>
a &lt;=&gt; b
</pre>
<blockquote>
<p>
-2- <i>Result:</i> <code><i>synth-three-way-result</i>&lt;X::value_type&gt;</code>.
<p/>
-3- <i>Preconditions:</i> Either <del><code>&lt;=&gt;</code> is defined for values of type (possibly <code>const</code>)</del> 
<code>T</code> <ins>satisfies <code>three_way_comparable</code></ins>, or <code>&lt;</code> is defined for values of type 
(possibly <code>const</code>) <code>T</code> and <code>&lt;</code> is a total ordering relationship.
<p/>
-4- <i>Returns:</i> <code>lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end(),
<i>synth-three-way</i>)</code>
<p/>
[<i>Note 1</i>: The algorithm <code>lexicographical_compare_three_way</code> is defined in Clause 27. &mdash; <i>end note</i>]
<p/>
-5- <i>Complexity:</i> Linear.
</p>
</blockquote>
</blockquote>

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

<p><i>[2023-06-13; Varna]</i></p>

<p>
The group liked the previously suggested wording but would prefer to say "models" instead of "satisfies"
in preconditions.
</p>
<p><i>[2023-06-14 Varna; Move to Ready]</i></p>

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



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

<ol>
<li><p>Modify 23.2.2.4 <a href="https://wg21.link/container.opt.reqmts">[container.opt.reqmts]</a> as indicated:</p>

<blockquote>
<pre>
a &lt;=&gt; b
</pre>
<blockquote>
<p>
-2- <i>Result:</i> <code><i>synth-three-way-result</i>&lt;X::value_type&gt;</code>.
<p/>
-3- <i>Preconditions:</i> Either <del><code>&lt;=&gt;</code> is defined for values of type (possibly <code>const</code>)</del> 
<code>T</code> <ins>models <code>three_way_comparable</code></ins>, or <code>&lt;</code> is defined for values of type 
(possibly <code>const</code>) <code>T</code> and <code>&lt;</code> is a total ordering relationship.
<p/>
-4- <i>Returns:</i> <code>lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end(),
<i>synth-three-way</i>)</code>
<p/>
[<i>Note 1</i>: The algorithm <code>lexicographical_compare_three_way</code> is defined in Clause 27. &mdash; <i>end note</i>]
<p/>
-5- <i>Complexity:</i> Linear.
</p>
</blockquote>
</blockquote>

</li>
</ol>




</body>
</html>
