<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3061: What is the return type of compare_3way?</title>
<meta property="og:title" content="Issue 3061: What is the return type of compare_3way?">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3061.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#Resolved">Resolved</a> status.</em></p>
<h3 id="3061"><a href="lwg-defects.html#3061">3061</a>. What is the return type of <code>compare_3way</code>?</h3>
<p><b>Section:</b> 26.8.12 <a href="https://wg21.link/alg.three.way">[alg.three.way]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Richard Smith <b>Opened:</b> 2018-02-07 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#alg.three.way">issues</a> in [alg.three.way].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The <a href="http://wiki.edg.com/pub/Wg21albuquerque/StrawPolls/p0768r1_spaceship_library.pdf">P0768R1 
specification of compare_3way</a> says:
</p>
<blockquote><pre>
template&lt;class T, class U&gt; constexpr auto compare_3way(const T&amp; a, const U&amp; b);
</pre>
<blockquote>
<p>
-1- <i>Effects:</i> Compares two values and produces a result of the strongest applicable comparison
category type:
</p>
<ol style="list-style-type: none">
<li><p>(1.1) &mdash; Returns <code>a &lt;=&gt; b</code> if that expression is well-formed.</p></li>
<li><p>(1.2) &mdash; Otherwise, if the expressions <code>a == b</code> and <code>a &lt; b</code> are each 
well-formed and convertible to <code>bool</code>, returns <code>strong_ordering::equal</code> when 
<code>a == b</code> is <code>true</code>, otherwise returns <code>strong_ordering::less</code> when 
<code>a &lt; b</code> is <code>true</code>, and otherwise returns <code>strong_ordering::greater</code>.</p></li>
<li><p>(1.3) &mdash; Otherwise, if the expression <code>a == b</code> is well-formed and convertible to 
<code>bool</code>, returns <code>strong_equality::equal</code> when <code>a == b</code> is <code>true</code>, and 
otherwise returns <code>strong_equality::nonequal</code>.</p></li>
<li><p>(1.4) &mdash; Otherwise, the function shall be defined as deleted.</p></li>
</ol>
</blockquote>
</blockquote>
<p>
So, it returns <code>strong_ordering::...</code> or <code>strong_equality::</code> or <code>a &lt;=&gt; b</code>. 
By the normal core deduction rules, that means it's always ill-formed, because one return type 
deduction deduces <code>strong_ordering</code> and another deduces <code>strong_equality</code>.
<p/>
I'm guessing the idea was actually that the above happens as if by four separate overloads / 
<code>constexpr if</code> / else. But I think you need to actually say that.
</p>

<p><i>[Tomasz suggests proposed wording]</i></p>


<p><i>[2018-06-18 after reflector discussion]</i></p>

<p>Priority set to 2</p>

<p><i>[2018-11 San Diego Thursday night issue processing]</i></p>

<p>Spaceship is still in flux; revisit in Kona. Status to Open</p>

<p><i>[2020-01 Resolved by the adoption of <a href="https://wg21.link/P1614">P1614</a> in Cologne.]</i></p>



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

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

<blockquote>
<pre>
<i>// 26.8.12 <a href="https://wg21.link/alg.three.way">[alg.three.way]</a>, three-way comparison algorithms</i>
template&lt;class T, class U&gt;
constexpr <del>auto</del><ins><i>see below</i></ins> compare_3way(const T&amp; a, const U&amp; b);
</pre>
</blockquote>
</li>

<li><p>Modify 26.8.12 <a href="https://wg21.link/alg.three.way">[alg.three.way]</a> as indicated:</p>

<blockquote><pre>
template&lt;class T, class U&gt; constexpr <del>auto</del><ins><i>see below</i></ins> compare_3way(const T&amp; a, const U&amp; b);
</pre>
<blockquote>
<p>
-1- <i>Effects:</i> Compares two values and produces a result of the strongest applicable comparison 
category type:
</p>
<ol style="list-style-type: none">
<li><p>(1.1) &mdash; <del>Returns <code>a &lt;=&gt; b</code> if that expression is 
well-formed</del><ins>If the expression <code>a &lt;=&gt; b</code> is well-formed, returns a 
value of type <code>decay_t&lt;decltype(a &lt;=&gt; b)&gt;</code> initialized from 
<code>a &lt;=&gt; b</code></ins>.</p></li>
<li><p>(1.2) &mdash; Otherwise, if the expressions <code>a == b</code> and <code>a &lt; b</code> are each 
well-formed and convertible to <code>bool</code>, returns <ins>a value of type <code>strong_ordering</code> 
equal to</ins> <code>strong_ordering::equal</code> when <code>a == b</code> is <code>true</code>, otherwise 
returns <code>strong_ordering::less</code> when <code>a &lt; b</code> is <code>true</code>, and otherwise 
returns <code>strong_ordering::greater</code>.</p></li>
<li><p>(1.3) &mdash; Otherwise, if the expression <code>a == b</code> is well-formed and convertible to 
<code>bool</code>, returns <ins>a value of type <code>strong_equality</code> equal to</ins> 
<code>strong_equality::equal</code> when <code>a == b</code> is <code>true</code>, and otherwise returns 
<code>strong_equality::nonequal</code>.</p></li>
<li><p>(1.4) &mdash; Otherwise, the <ins>return type is <code>void</code> and the</ins> function is defined 
as deleted.</p></li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
