<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2491: std::less&lt;T*&gt; in constant expression</title>
<meta property="og:title" content="Issue 2491: std::less&lt;T*&gt; in constant expression">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2491.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#New">New</a> status.</em></p>
<h3 id="2491"><a href="lwg-active.html#2491">2491</a>. <code>std::less&lt;T*&gt;</code> in constant expression</h3>
<p><b>Section:</b> 22.10.8 <a href="https://wg21.link/comparisons">[comparisons]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Agust&iacute;n K-ballo Berg&eacute; <b>Opened:</b> 2015-04-01 <b>Last modified:</b> 2021-04-10</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#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
It is not entirely clear if and when the specializations of <code>std::less</code> (and friends) for pointer types 
can be used in a constant expression. Consider the following code:
</p>
<blockquote><pre>
#include &lt;functional&gt;

struct foo {};
foo x, y;
constexpr bool b = std::less&lt;foo*&gt;{}(&amp;x, &amp;y); // [1]

foo z[] = {{}, {}};
constexpr bool ba = std::less&lt;foo*&gt;{}(&amp;z[0], &amp;z[1]); // [2]
</pre></blockquote>
<p>
Comparing the address of unrelated objects is not a constant expression since the result is unspecified, so 
it could be expected for [1] to fail and [2] to succeed. However, <code>std::less</code> specialization for pointer 
types is well-defined and yields a total order, so it could just as well be expected for [1] to succeed. Finally, 
since the implementation of such specializations is not mandated, [2] could fail as well (This could happen, if
an implementation would provide such a specialization and if that would use built-in functions that would not be
allowed in constant expressions, for example). In any case, the standard should be clear so as to avoid 
implementation-defined <code>constexpr</code>-ness.
</p>

<p><i>[2017-01-22, Jens provides rationale and proposed wording]</i></p>

<p>
<code>std::less&lt;T*&gt;</code> is required to deliver a total order on pointers.
However, the layout of global objects is typically determined
by the linker, not the compiler, so requiring <code>std::less&lt;T*&gt;</code> to
provide an ordering at compile-time that is consistent with
run-time would need results from linking to feed back to
the compiler, something that C++ has traditionally not required.
</p>

<strong>Previous resolution [SUPERSEDED]:</strong>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4618">N4618</a>.</p>

<ol>
<li><p>Add at the end of 22.10.8 <a href="https://wg21.link/comparisons">[comparisons]</a>:</p>
<blockquote>
<p>
-2- For templates <code>less</code>, <code>greater</code>, <code>less_equal</code>, and <code>greater_equal</code>, [&hellip;], 
if the call operator calls a built-in operator comparing pointers, the call operator yields a strict total order 
that is consistent among those specializations and is also consistent with the partial order imposed by those 
built-in operators. <ins>Relational comparisons of pointer values are not required to be usable as constant expressions.</ins>
</p>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2021-04-05; Jiang An comments and provides alternative wording]</i></p>

<p>
The libc++ and MSVC STL implementations only support flat address spaces, and always use comparison operators. 
The libstdc++ implementation casts pointer values to <code>uintptr_t</code> if the direct comparison result is unusable 
in constant evaluation.
<p/>
So, I think that we can specify that the implementation-defined strict total order (3.28 <a href="https://wg21.link/defns.order.ptr">[defns.order.ptr]</a>) 
generates a core constant expression if and only if the corresponding underlying comparison expression comparing 
pointer values is a core constant expression. No any other case should be a core constant expression, otherwise 
we should also make the underlying comparison expression a core constant expression.
<p/>
IMO the proposed resolution is already implemented in libc++, libstdc++, and MSVC STL, and implementable on compilers 
that either support flat address spaces only or have implemented intrinsics needed for transparent comparison operators 
and <code>std::is_constant_evaluated</code>.
</p>



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

<ol>
<li><p>Add at the end of 22.10.8 <a href="https://wg21.link/comparisons">[comparisons]</a> p2:</p>
<blockquote>
<p>
-2- For templates <code>less</code>, <code>greater</code>, <code>less_equal</code>, and <code>greater_equal</code>, the specializations 
for any pointer type yield a result consistent with the implementation-defined strict total order over pointers 
(3.28 <a href="https://wg21.link/defns.order.ptr">[defns.order.ptr]</a>). [<i>Note 1</i>: If <code>a &lt; b</code> is well-defined for pointers <code>a</code> and 
<code>b</code> of type <code>P</code>, then <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>] For template specializations <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>, if the call operator calls a built-in operator 
comparing pointers, the call operator yields a result consistent with the implementation-defined strict total order over 
pointers. <ins>A comparison result of pointer values is a core constant expression if and only if the corresponding 
built-in comparison expression is a core constant expression.</ins>
</p>
</blockquote>
</li>

<li><p>Add at the end of 22.10.9 <a href="https://wg21.link/range.cmp">[range.cmp]</a> (3.1):</p>
<blockquote>
<p>
-3- <i>Effects:</i>
</p>
<ol style="list-style-type: none">
<li><p>(3.1) &mdash; If the expression <code>std::forward&lt;T&gt;(t) == std::forward&lt;U&gt;(u)</code> results in a call 
to a built-in operator <code>==</code> comparing pointers: returns <code>false</code> if either (the converted value of) <code>t</code> 
precedes <code>u</code> or <code>u</code> precedes <code>t</code> in the implementation-defined strict total order over pointers 
(3.28 <a href="https://wg21.link/defns.order.ptr">[defns.order.ptr]</a>) and otherwise <code>true</code>. <ins>The result is a core constant expression if and only 
if <code>std::forward&lt;T&gt;(t) == std::forward&lt;U&gt;(u)</code> is a core constant expression.</ins></p></li>
<li><p>(3.2) &mdash; Otherwise, equivalent to: <code>return std::forward&lt;T&gt;(t) == std::forward&lt;U&gt;(u);</code></p></li>
</ol>
</blockquote>
</li>

<li><p>Add at the end of 22.10.9 <a href="https://wg21.link/range.cmp">[range.cmp]</a> (7.1):</p>
<blockquote>
<p>
-7- <i>Effects:</i>
</p>
<ol style="list-style-type: none">
<li><p>(7.1) &mdash; If the expression <code>std::forward&lt;T&gt;(t) &lt; std::forward&lt;U&gt;(u)</code> results in a call 
to a built-in operator <code>&lt;</code> comparing pointers: returns <code>true</code> if (the converted value of) <code>t</code> 
precedes <code>u</code> in the implementation-defined strict total order over pointers (3.28 <a href="https://wg21.link/defns.order.ptr">[defns.order.ptr]</a>) and 
otherwise <code>false</code>. <ins>The result is a core constant expression if and only 
if <code>std::forward&lt;T&gt;(t) &lt; std::forward&lt;U&gt;(u)</code> is a core constant expression.</ins></p></li>
<li><p>(7.2) &mdash; Otherwise, equivalent to: <code>return std::forward&lt;T&gt;(t) &lt; std::forward&lt;U&gt;(u);</code></p></li>
</ol>
</blockquote>
</li>

<li><p>Add at the end of 22.10.8.8 <a href="https://wg21.link/comparisons.three.way">[comparisons.three.way]</a> (3.1):</p>
<blockquote>
<p>
-3- <i>Effects:</i>
</p>
<ol style="list-style-type: none">
<li><p>(3.1) &mdash; If the expression <code>std::forward&lt;T&gt;(t) &lt;=&gt; std::forward&lt;U&gt;(u)</code> results in a call 
to a built-in operator <code>&lt;=&gt;</code> comparing pointers: returns <code>strong_ordering::less</code> if (the converted value 
of) <code>t</code> precedes <code>u</code> in the implementation-defined strict total order over pointers (3.28 <a href="https://wg21.link/defns.order.ptr">[defns.order.ptr]</a>), 
<code>strong_ordering::greater</code> if <code>u</code> precedes <code>t</code>, and otherwise <code>strong_ordering::equal</code>. 
<ins>The result is a core constant expression if and only if <code>std::forward&lt;T&gt;(t) &lt;=&gt; std::forward&lt;U&gt;(u)</code> 
is a core constant expression.</ins></p></li>
<li><p>(3.2) &mdash; Otherwise, equivalent to: <code>return std::forward&lt;T&gt;(t) &lt;=&gt; std::forward&lt;U&gt;(u);</code></p></li>
</ol>
</blockquote>
</li>
</ol>





</body>
</html>
