<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3347: std::pair&lt;T, U&gt; now requires T and U to be less-than-comparable</title>
<meta property="og:title" content="Issue 3347: std::pair&lt;T, U&gt; now requires T and U to be less-than-comparable">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3347.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++20">C++20</a> status.</em></p>
<h3 id="3347"><a href="lwg-defects.html#3347">3347</a>. <code>std::pair&lt;T, U&gt;</code> now requires <code>T</code> and <code>U</code> to be less-than-comparable</h3>
<p><b>Section:</b> 22.3.3 <a href="https://wg21.link/pairs.spec">[pairs.spec]</a>, 23.3.3.1 <a href="https://wg21.link/array.overview">[array.overview]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2019-12-03 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>1
</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++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<a href="https://wg21.link/p1614r2">P1614R2</a> added <code>operator&lt;=&gt;</code> as a hidden friend to 
<code>std::pair</code>:
</p>
<blockquote>
<pre>
friend constexpr common_comparison_category_t&lt;<i>synth-three-way-result</i>&lt;T1&gt;, 
                                              <i>synth-three-way-result</i>&lt;T2&gt;&gt;
  operator&lt;=&gt;(const pair&amp; x, const pair&amp; y) { <i>see below</i> }
</pre>
</blockquote>
<p>
That is not a function template, so is not a SFINAE context. If one or both of 
<code><i>synth-three-way-result</i>&lt;T1&gt;</code> or <code><i>synth-three-way-result</i>&lt;T2&gt;</code> is an 
invalid type then the declaration of <code>operator&lt;=&gt;</code> is ill-formed, and so the specialization 
<code>std::pair&lt;T1, T2&gt;</code> is ill-formed.
<p/>
A similar problem exists for <code>std::array</code>.
<p/>
There are at least two ways to fix this:
</p>
<ol>
<li><p>Constrain the function and delay the use of synth-three-way-result until we know it's valid.</p></li>
<li><p>Replace the hidden friend with a namespace-scope function template, so invalid 
<code><i>synth-three-way-result</i></code> types cause substitution failure.</p></li>
</ol>
<p>
The first option is somewhat hard to specify, because current policy is to avoid the use of requires-clauses 
in most of the library clauses. Even with a requires-clause, the potentially-invalid 
<code><i>synth-three-way-result</i></code> types cannot be used in the function declarator. Furthermore, the 
<code>operator&lt;=&gt;</code> for <code>std::array</code> is currently specified in Table [tab:container.opt] and 
so there's nowhere to add a <i>Constraints:</i> element.
<p/>
The second option would partially revert the <a href="https://wg21.link/p1614r2">P1614R2</a> changes for 
<code>std::pair</code> and <code>std::array</code> and bring them closer to what was in C++17. The main motivation 
for making <code>operator==</code> a hidden friend was to allow it to be defaulted, so that <code>std::pair</code> 
and <code>std::array</code> would be usable as non-type template parameters. Following the acceptance of 
<a href="https://wg21.link/p1907">P1907</a> in Belfast it isn't necessary to default it, so we can go back 
to what was in C++17.
</p>

<p><i>[2019-12-12 Issue Prioritization]</i></p>

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

<p><i>[2020-02-10 Move to Immediate Monday afternoon in Prague]</i></p>



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

<ol>
<li><p>Modify 22.2.1 <a href="https://wg21.link/utility.syn">[utility.syn]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> This restores the pre-<a href="https://wg21.link/p1614r2">P1614R2</a> 
<code>operator==</code> and uses <code>operator&lt;=&gt;</code> as replacement for
<code>operator&lt;</code>, <code>operator&lt;=</code>, <code>operator&gt;</code>, <code>operator&gt;=</code>.]
</p>
</blockquote>

<blockquote>
<pre>
[&hellip;]

// 22.3 <a href="https://wg21.link/pairs">[pairs]</a>, class template pair
template&lt;class T1, class T2&gt;
struct pair;

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

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

<blockquote>
<pre>
  [&hellip;]
  constexpr void swap(pair&amp; p) noexcept(<i>see below</i>);
  
  <del><i>// 22.3.3 <a href="https://wg21.link/pairs.spec">[pairs.spec]</a>, pair specialized algorithms</i>
  friend constexpr bool operator==(const pair&amp;, const pair&amp;) = default;
  friend constexpr bool operator==(const pair&amp; x, const pair&amp; y)
    requires (is_reference_v&lt;T1&gt; || is_reference_v&lt;T2&gt;)
    { return x.first == y.first &amp;&amp; x.second == y.second; }
  friend constexpr common_comparison_category_t&lt;<i>synth-three-way-result</i>&lt;T1&gt;,
                                                <i>synth-three-way-result</i>&lt;T2&gt;&gt;
    operator&lt;=&gt;(const pair&amp; x, const pair&amp; y) { <i>see below</i> }</del>
};

template&lt;class T1, class T2&gt;
pair(T1, T2) -&gt; pair&lt;T1, T2&gt;;
[&hellip;]
</pre>
</blockquote>
</li>

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

<blockquote>
<p>
<b>20.4.3 Specialized algorithms [pairs.spec]</b>
</p>
<pre>
<ins>template&lt;class T1, class T2&gt;
  constexpr bool operator==(const pair&lt;T1, T2&gt;&amp; x, const pair&lt;T1, T2&gt;&amp; y);</ins>
</pre>
<blockquote>
<p>
<ins>-?- <i>Returns:</i> <code>x.first == y.first &amp;&amp; x.second == y.second</code>.</ins>
</p>
</blockquote>
<pre>
<ins>template&lt;class T1, class T2&gt;</ins>
<del>friend</del> constexpr
  common_comparison_category_t&lt;<i>synth-three-way-result</i>&lt;T1&gt;, <i>synth-three-way-result</i>&lt;T2&gt;&gt;
    operator&lt;=&gt;(const pair<ins>&lt;T1, T2&gt;</ins>&amp; x, const pair<ins>&lt;T1, T2&gt;</ins>&amp; y);
</pre>
<blockquote>
<p>
-1- <i>Effects:</i> Equivalent to:
</p>
<blockquote><pre>
if (auto c = <i>synth-three-way</i>(x.first, y.first); c != 0) return c;
return <i>synth-three-way</i>(x.second, y.second);
</pre></blockquote>
</blockquote>
</blockquote>
</li>

<li><p>Modify 23.3.2 <a href="https://wg21.link/array.syn">[array.syn]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> This restores the pre-<a href="https://wg21.link/p1614r2">P1614R2</a> 
<code>operator==</code> and uses <code>operator&lt;=&gt;</code> as replacement for
<code>operator&lt;</code>, <code>operator&lt;=</code>, <code>operator&gt;</code>, <code>operator&gt;=</code>.]
</p>
</blockquote>

<blockquote>
<pre>
namespace std {

// 23.3.3 <a href="https://wg21.link/array">[array]</a>, class template array
template&lt;class T, size_t N&gt; struct array;

<ins>template&lt;class T, size_t N&gt;
  constexpr bool operator==(const array&lt;T, N&gt;&amp; x, const array&lt;T, N&gt;&amp; y);
template&lt;class T, size_t N&gt;
  constexpr <i>synth-three-way-result</i>&lt;T&gt;
    operator&lt;=&gt;(const array&lt;T, N&gt;&amp; x, const array&lt;T, N&gt;&amp; y);</ins>

template&lt;class T, size_t N&gt;
constexpr void swap(array&lt;T, N&gt;&amp; x, array&lt;T, N&gt;&amp; y) noexcept(noexcept(x.swap(y)));
[&hellip;]
</pre>
</blockquote>
</li>

<li><p>Modify 23.3.3.1 <a href="https://wg21.link/array.overview">[array.overview]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> there is no need to add definitions of <code>operator==</code> and 
<code>operator&lt;=&gt;</code> to  [array.spec] because they are defined by Table 71: 
Container requirements [tab:container.req] and Table 73: Optional container operations 
[tab:container.opt] respectively.]
</p>
</blockquote>

<blockquote>
<pre>
  [&hellip;]
  constexpr T * data() noexcept;
  constexpr const T * data() const noexcept;
  
  <del>friend constexpr bool operator==(const array&amp;, const array&amp;) = default;
  friend constexpr <i>synth-three-way-result</i>&lt;value_type&gt;
    operator&lt;=&gt;(const array&amp;, const array&amp;);</del>
};

template&lt;class T, class... U&gt;
array(T, U...) -&gt; array&lt;T, 1 + sizeof...(U)&gt;;
[&hellip;]
</pre>
</blockquote>
</li>

</ol>




</body>
</html>
