<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3865: Sorting a range of pairs</title>
<meta property="og:title" content="Issue 3865: Sorting a range of pairs">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3865.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="3865"><a href="lwg-defects.html#3865">3865</a>. Sorting a range of <code>pair</code>s</h3>
<p><b>Section:</b> 22.3.3 <a href="https://wg21.link/pairs.spec">[pairs.spec]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Barry Revzin <b>Opened:</b> 2023-01-28 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#pairs.spec">issues</a> in [pairs.spec].</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>
Consider this example:
</p>
<blockquote><pre>
#include &lt;algorithm&gt;
#include &lt;ranges&gt;

int main() {
  int a[3] = {1, 2, -1};
  int b[3] = {1, 4, 1};
  std::ranges::sort(std::views::zip(a, b));
}
</pre></blockquote>
<p>
This is currently valid C++23 code, but wasn't before <a href="https://wg21.link/P2165" title=" Compatibility between tuple, pair and tuple-like objects">P2165</a> (Compatibility between <code>tuple</code>, <code>pair</code> 
and <i>tuple-like</i> objects). Before P2165, <code>zip(a, b)</code> returned a range whose reference was 
<code>std::pair&lt;int&amp;, int&amp;&gt;</code> and whose <code>value_type</code> was <code>std::pair&lt;int, int&gt;</code> and 
<code>std::pair</code>, unlike <code>std::tuple</code>, does not have any heterogeneous comparisons &mdash; which is required to 
satisfy the <code>sortable</code> concept. 
<p/>
While the zip family of range adapters no longer has this problem, nothing prevents users from themselves creating a range 
whose reference type is <code>pair&lt;T&amp;, U&amp;&gt;</code> and whose <code>value_type</code> is <code>pair&lt;T, U&gt;</code> 
(which is now a valid range after the zip paper) and then discovering that this range isn't sortable, even though the 
equivalent using <code>tuple</code> is. 
<p/>
Suggested resolution:
<p/>
Change <code>pair</code>'s comparison operators from comparing two arguments of type <code>const pair&lt;T1, T2&gt;&amp;</code> 
to instead comparing arguments of types <code>const pair&lt;T1, T2&gt;&amp;</code> and <code>const pair&lt;U1, U2&gt;&amp;</code>. 
</p>

<p><i>[2023-02-05; Barry provides wording]</i></p>


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

<p>
Set priority to 2 after reflector poll.
</p>

<p><i>[Issaquah 2023-02-07; 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-3865"><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.2.1 <a href="https://wg21.link/utility.syn">[utility.syn]</a>, header <code>&lt;utility&gt;</code> synopsis, as indicated:</p>

<blockquote>
<pre>
[&hellip;]
// <i>22.3.3 <a href="https://wg21.link/pairs.spec">[pairs.spec]</a>, pair specialized algorithms</i>
template&lt;class T1, class T2<ins>, class U1, class U2</ins>&gt;
  constexpr bool operator==(const pair&lt;T1, T2&gt;&amp;, const pair&lt;<del>T</del><ins>U</ins>1, <del>T</del><ins>U</ins>2&gt;&amp;);
template&lt;class T1, class T2<ins>, class U1, class U2</ins>&gt;
  constexpr common_comparison_category_t&lt;<i>synth-three-way-result</i>&lt;T1<ins>, U1</ins>&gt;,
                                         <i>synth-three-way-result</i>&lt;T2<ins>, U2</ins>&gt;&gt;
    operator&lt;=&gt;(const pair&lt;T1, T2&gt;&amp;, const pair&lt;<del>T</del><ins>U</ins>1, <del>T</del><ins>U</ins>2&gt;&amp;);
[&hellip;]
</pre>
</blockquote>
</li>

<li><p>Modify 22.3.3 <a href="https://wg21.link/pairs.spec">[pairs.spec]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class T1, class T2<ins>, class U1, class U2</ins>&gt;
  constexpr bool operator==(const pair&lt;T1, T2&gt;&amp; x, const pair&lt;<del>T</del><ins>U</ins>1, <del>T</del><ins>U</ins>2&gt;&amp; y);
</pre>
<blockquote>
<p>
-1- [&hellip;]
<p/>
-2- [&hellip;]
</p>
</blockquote>
<pre>
template&lt;class T1, class T2<ins>, class U1, class U2</ins>&gt;
  constexpr common_comparison_category_t&lt;<i>synth-three-way-result</i>&lt;T1<ins>, U1</ins>&gt;,
                                         <i>synth-three-way-result</i>&lt;T2<ins>, U2</ins>&gt;&gt;
    operator&lt;=&gt;(const pair&lt;T1, T2&gt;&amp; x, const pair&lt;<del>T</del><ins>U</ins>1, <del>T</del><ins>U</ins>2&gt;&amp; y);
</pre>
<blockquote>
<p>
-3- [&hellip;]
</p>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
