<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2562: Consistent total ordering of pointers by comparison functors</title>
<meta property="og:title" content="Issue 2562: Consistent total ordering of pointers by comparison functors">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2562.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++17">C++17</a> status.</em></p>
<h3 id="2562"><a href="lwg-defects.html#2562">2562</a>. Consistent total ordering of pointers by comparison functors</h3>
<p><b>Section:</b> 22.10.8 <a href="https://wg21.link/comparisons">[comparisons]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2015-11-18 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#comparisons">active issues</a> in [comparisons].</p>
<p><b>View all other</b> <a href="lwg-index.html#comparisons">issues</a> in [comparisons].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
N4567 22.10.8 <a href="https://wg21.link/comparisons">[comparisons]</a>/14 specifies that the comparison functors provide a total ordering for pointer types:
</p>
<blockquote><p>
For templates <code>greater</code>, <code>less</code>, <code>greater_equal</code>, and <code>less_equal</code>, the specializations for 
any pointer type yield a total order, even if the built-in operators <code>&lt;</code>, <code>&gt;</code>, <code>&lt;=</code>, <code>&gt;=</code> 
do not.
</p></blockquote>
<p>
It notably does not specify:
</p>
<ul>
<li><p>whether the specializations of all of the named templates for a given pointer type yield the <em>same</em> total order</p></li>
<li><p>whether the total order imposed respects the partial order imposed by the built-in operators</p></li>
<li><p>whether the total order imposed is consistent with the partition induced by <code>==</code></p></li>
</ul>
<p>
All of which are important for sane semantics and provided by common implementations, since the built-in operators provide 
a total order and the comparison functors yield that same order.
<p/>
It would be extremely confusing &mdash; if not outright insane &mdash; for e.g.:
</p>
<ul>
<li><p><code>less&lt;int*&gt;</code> and <code>greater&lt;int*&gt;</code> to yield different orders</p></li>
<li><p><code>less&lt;int*&gt;</code> to disagree with <code>&lt;</code> on the relative order of two pointers for which <code>&lt;</code> 
is defined</p></li>
<li><p><code>less&lt;int*&gt;</code> to order <code>a</code> before <code>b</code> when <code>a == b</code>, i.e., not preserve equality.</p></li>
</ul>
<p>
Consistent semantics for the various comparison functors and the built-in operators is so intuitive as to be assumed by most programmers.
<p/>
Related issues: <a href="lwg-defects.html#2450" title="(greater|less|greater_equal|less_equal)&lt;void&gt; do not yield a total order for pointers (Status: C++17)">2450</a><sup><a href="https://cplusplus.github.io/LWG/issue2450" title="Latest snapshot">(i)</a></sup>, <a href="lwg-active.html#2547" title="Container requirements (and other library text) should say &quot;strict total order&quot;, not just &quot;total order&quot; (Status: New)">2547</a><sup><a href="https://cplusplus.github.io/LWG/issue2547" title="Latest snapshot">(i)</a></sup>.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to N4567.</p>

<ol>
<li><p>Alter 22.10.8 <a href="https://wg21.link/comparisons">[comparisons]</a>/14 to read:</p>
<blockquote><p>
For templates <code>greater</code>, <code>less</code>, <code>greater_equal</code>, and <code>less_equal</code>, the specializations for any pointer type
yield <del>a</del><ins>the same</ins> total order, even if the built-in operators <code>&lt;</code>, <code>&gt;</code>, <code>&lt;=</code>, 
<code>&gt;=</code> do not. <ins>The total order shall respect the partial order imposed by the built-in operators.</ins>
</p></blockquote>
</li>
</ol>
</blockquote>

<p><i>[2016-05-20, Casey Carter comments and suggests revised wording]</i></p>

<p>
The new proposed wording is attempting to address the issue raised in the 2016-02-04 telecon.
<p/>
The real issue I'm trying to address here is ensure that "weird" implementations provide the same kind of consistency 
for pointer orderings as "normal" implementations that use a flat address spaces and have totally ordered <code>&lt;</code>. 
If <code>a &lt; b</code> is true for <code>int</code> pointers <code>a</code> and <code>b</code>, then <code>less&lt;int*&gt;(a, b)</code>, 
<code>less_equal&lt;int*&gt;(a, b)</code>, <code>less&lt;char*&gt;(a, b)</code>, <code>less&lt;void*&gt;(a, b)</code>, and 
<code>greater&lt;int*&gt;(b, a)</code> should all hold. I <em>think</em> this wording is sufficient to provide that.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to N4582.</p>

<ol>
<li><p>Alter 22.10.8 <a href="https://wg21.link/comparisons">[comparisons]</a> to read:</p>
<blockquote><p>
-14- For templates <code>greater</code>, <code>less</code>, <code>greater_equal</code>, and <code>less_equal</code>, the 
specializations for any pointer type yield <del>a</del><ins>the same</ins> total order<ins>. That total order is 
consistent with the partial order imposed by</ins><del>, even if</del> the built-in operators <code>&lt;</code>, <code>&gt;</code>, 
<code>&le;</code>, <ins>and</ins> <code>&gt;</code> <del>do not</del>. <ins>[<i>Note</i>: When <code>a &lt; b</code> is 
well-defined for pointers <code>a</code> and <code>b</code> of type <code>P</code>, this implies <code>(a &lt; b) == 
less&lt;P&gt;(a, b)</code>, <code>(a &gt; b) == greater&lt;P&gt;(a, b)</code>, and so forth. &mdash; <i>end note</i>]</ins> 
For template specializations <code>greater&lt;void&gt;</code>, <code>less&lt;void&gt;</code>, <code>greater_equal&lt;void&gt;</code>, 
and <code>less_equal&lt;void&gt;</code>, if the call operator calls a built-in operator comparing pointers, the 
call operator yields a total order.
</p></blockquote>
</li>
</ol>
</blockquote>

<p><i>[2016-08-04 Chicago LWG]</i></p>

<p>
LWG discusses and concludes that we are trying to accomplish the following:
</p>
<ol>
<li>
<blockquote><pre>
T* a = /* ... */;
T* b = /* ... */;
</pre></blockquote>
<p>
 if <code>a &lt; b</code> is valid, <code>a &lt; b == less&lt;T*&gt;(a, b)</code>, and analogously for <code>&gt;</code>, 
 <code>&lt;=</code>, <code>&gt;=</code>.
</p>
</li>
<li>
<blockquote><pre>
less&lt;void&gt;(a, b) == less&lt;T*&gt;(a, b);
less&lt;T*&gt;(a, b) == greater&lt;T*&gt;(b, a);
</pre></blockquote>
<p>
etc.
</p>
</li>
<li><p>
<code>less&lt;T*&gt;</code> produces a strict total ordering with which the other three
function objects are consistent
</p></li>
<li><p>
<code>less&lt;void&gt;</code> when applied to pointers produces a strict total ordering with
which the other three are consistent
</p></li>
<li><p>
<code>less&lt;void&gt;</code> when applied to pointers of the same type produces the same
strict total ordering as <code>less&lt;T*&gt;</code>, and analogously for the other three
</p></li>
<li><p>
we are not addressing <code>less&lt;void&gt;</code> (and the other three) when applied to
pointers of differing types
</p></li>
</ol>
<p>
Walter and Nevin revise Proposed Wording accordingly.
</p>
<p><i>[2016-08 - Chicago]</i></p>

<p>Thurs PM: Moved to Tentatively Ready</p>


<p id="res-2562"><b>Proposed resolution:</b></p>
<p>This wording is relative to N4606.</p>

<ol>
<li><p>Change 22.10.8 <a href="https://wg21.link/comparisons">[comparisons]</a> p14 as indicated:</p>
<blockquote><p>
-14- For templates <del><code>greater</code>, <code>less</code>, <code>greater_equal</code>, and 
<code>less_equal</code></del><ins><ins><code>less</code>, <code>greater</code>, <code>less_equal</code>,
and <code>greater_equal</code></ins></ins>, 
the specializations for any pointer type yield a <ins>strict</ins> total order<ins>
that is consistent among those specializations and is also consistent with the partial order imposed by</ins>
<del>, even if</del> the built-in operators 
<code>&lt;</code>, <code>&gt;</code>, <code>&lt;=</code>, <code>&gt;=</code> <del>do not</del>. <ins>[<i>Note:</i>
When <code>a &lt; b</code> is well-defined for pointers <code>a</code> and <code>b</code> of type <code>P</code>,
this implies <code>(a &lt; b) == less&lt;P&gt;(a, b)</code>, <code>(a &gt; b) == greater&lt;P&gt;(a, b)</code>,
and so forth. &mdash; <i>end note</i>]</ins> For template specializations 
<del><code>greater&lt;void&gt;</code>, <code>less&lt;void&gt;</code>, <code>greater_equal&lt;void&gt;</code>, and 
<code>less_equal&lt;void&gt;</code></del><ins><code>less&lt;void&gt;</code>, <code>greater&lt;void&gt;</code>,
<code>less_equal&lt;void&gt;</code>, and <code>greater_equal&lt;void&gt;</code></ins>, if the call operator 
calls a built-in operator comparing pointers, the call operator yields a <ins>strict</ins> total 
order <ins>that is consistent among those specializations and is also consistent
with the partial order imposed by those built-in operators</ins>.
</p></blockquote>
</li>
</ol>





</body>
</html>
