<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1297: unique_ptr's relational operator functions should induce a total order</title>
<meta property="og:title" content="Issue 1297: unique_ptr's relational operator functions should induce a total order">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1297.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="1297"><a href="lwg-defects.html#1297">1297</a>. <code>unique_ptr</code>'s relational operator functions should induce a total order</h3>
<p><b>Section:</b> 20.3.1.6 <a href="https://wg21.link/unique.ptr.special">[unique.ptr.special]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2009-12-23 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#unique.ptr.special">issues</a> in [unique.ptr.special].</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 comparison functions of <code>unique_ptr</code> currently directly delegate to
the underlying comparison functions of <code>unique_ptr&lt;T, D&gt;::pointer</code>.
This is disadvantageous, because this would not guarantee to induce a total
ordering for native pointers and it is hard to define a total order for mixed
types anyway.
</p>
<p>
The currently suggested resolution for <code>shared_ptr</code> comparison as of
<a href="lwg-defects.html#1262" title="std::less&lt;std::shared_ptr&lt;T&gt;&gt; is underspecified (Status: C++11)">1262</a><sup><a href="https://cplusplus.github.io/LWG/issue1262" title="Latest snapshot">(i)</a></sup> uses a normalization strategy: They perform the comparison on
the <em>composite pointer type</em> (7.6.9 <a href="https://wg21.link/expr.rel">[expr.rel]</a>). This is not
exactly possible for <code>unique_ptr</code> in the presence of user-defined
pointer-like types but the existing definition of <code>std::duration</code>
comparison as of 30.5.7 <a href="https://wg21.link/time.duration.comparisons">[time.duration.comparisons]</a> via
<code>common_type</code> of both argument types demonstrates a solution of this
problem. The approach can be seen as the general way to define a <em>composite
pointer type</em> and this is the approach which is used for here suggested
wording change.
</p>
<p>
For consistency reasons I would have preferred the same normalization strategy
for <code>==</code> and <code>!=</code>, but Howard convinced me not to do so (now).
</p>

<p><i>[
2010-11-03 Daniel comments and adjustes the currently proposed wording changes:
]</i></p>


<p>
Issue <a href="lwg-defects.html#1401" title="Provide support for unique_ptr&lt;T&gt; == nullptr (Status: C++11)">1401</a><sup><a href="https://cplusplus.github.io/LWG/issue1401" title="Latest snapshot">(i)</a></sup> is remotely related. <a href="#1401_extra_bullet">Bullet A</a> of its proposed resolution 
provides an alternative solution for issue discussed here and addresses NB comment GB-99.
Additionally I updated the below suggested wording in regard to the following:
It is an unncessary requirement that the below defined effective composite pointer-like
type <code>CT</code> satisfies the <code>LessThanComparable</code> requirements. All what is 
needed is, that the function object type <code>less&lt;CT&gt;</code> induces a strict
weak ordering on the pointer values.
</p>

<p><i>[2011-03-24 Madrid meeting]</i></p>


<p>Resolved by <a href="lwg-defects.html#1401" title="Provide support for unique_ptr&lt;T&gt; == nullptr (Status: C++11)">1401</a><sup><a href="https://cplusplus.github.io/LWG/issue1401" title="Latest snapshot">(i)</a></sup></p>



<p id="res-1297"><b>Proposed resolution:</b></p>

<p>
Change 20.3.1.6 <a href="https://wg21.link/unique.ptr.special">[unique.ptr.special]</a>/4-7 as indicated: <i>[The implicit
requirements and remarks imposed on the last three operators are the same as for
the first one due to the normative "equivalent to" usage within a Requires
element, see 16.3.2.4 <a href="https://wg21.link/structure.specifications">[structure.specifications]</a>/4. The effects of this
change are that all real pointers wrapped in a <code>unique_ptr</code> will order
like <code>shared_ptr</code> does.]</i>
</p>

<blockquote><pre>
template &lt;class T1, class D1, class T2, class D2&gt;
  bool operator&lt;(const unique_ptr&lt;T1, D1&gt;&amp; x, const unique_ptr&lt;T2, D2&gt;&amp; y);
</pre>

<blockquote>
<p>
<ins>? <i>Requires:</i> Let <code>CT</code> be <code>common_type&lt;unique_ptr&lt;T1,
D1&gt;::pointer, unique_ptr&lt;T2, D2&gt;::pointer&gt;::type</code>. Then
the specialization <code>less&lt;CT&gt;</code> shall be a function object type ([function.objects]) 
that induces a strict weak ordering ([alg.sorting]) on the pointer values.</ins>
</p>

<p>
4 <i>Returns:</i> <code><ins>less&lt;CT&gt;()(x.get(), y.get())</ins><del>x.get()
&lt; y.get()</del></code>.
</p>

<p>
<ins>? <i>Remarks:</i> If <code>unique_ptr&lt;T1, D1&gt;::pointer</code> is not
implicitly convertible to <code>CT</code> or <code>unique_ptr&lt;T2,
D2&gt;::pointer</code> is not implicitly convertible to <code>CT</code>, the program
is ill-formed.</ins>
</p>
</blockquote>

<pre>
template &lt;class T1, class D1, class T2, class D2&gt;
  bool operator&lt;=(const unique_ptr&lt;T1, D1&gt;&amp; x, const unique_ptr&lt;T2, D2&gt;&amp; y);
</pre>

<blockquote><p>
5 <ins><i>Effects:</i> Equivalent to <code>return !(y &lt; x)</code></ins>
<del><i>Returns:</i> <code>x.get() &lt;= y.get()</code></del>.
</p></blockquote>

<pre>
template &lt;class T1, class D1, class T2, class D2&gt;
  bool operator&gt;(const unique_ptr&lt;T1, D1&gt;&amp; x, const unique_ptr&lt;T2, D2&gt;&amp; y);
</pre>

<blockquote><p>
6 <ins><i>Effects:</i> Equivalent to <code>return (y &lt; x)</code></ins>
<del><i>Returns:</i> <code>x.get() &gt; y.get()</code></del>.
</p></blockquote>

<pre>
template &lt;class T1, class D1, class T2, class D2&gt;
  bool operator&gt;=(const unique_ptr&lt;T1, D1&gt;&amp; x, const unique_ptr&lt;T2, D2&gt;&amp; y);
</pre>

<blockquote><p>
7 <ins><i>Effects:</i> Equivalent to <code>return !(x &lt; y)</code></ins>
<del><i>Returns:</i> <code>x.get() &gt;= y.get()</code></del>.
</p></blockquote>
</blockquote>






</body>
</html>
