<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1262: std::less&lt;std::shared_ptr&lt;T&gt;&gt; is underspecified</title>
<meta property="og:title" content="Issue 1262: std::less&lt;std::shared_ptr&lt;T&gt;&gt; is underspecified">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1262.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++11">C++11</a> status.</em></p>
<h3 id="1262"><a href="lwg-defects.html#1262">1262</a>. <code>std::less&lt;std::shared_ptr&lt;T&gt;&gt;</code> is underspecified</h3>
<p><b>Section:</b> 20.3.2.2.8 <a href="https://wg21.link/util.smartptr.shared.cmp">[util.smartptr.shared.cmp]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2009-11-10 <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#util.smartptr.shared.cmp">issues</a> in [util.smartptr.shared.cmp].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
20.3.2.2.8 <a href="https://wg21.link/util.smartptr.shared.cmp">[util.smartptr.shared.cmp]</a>/5 says:
</p>

<blockquote><p>
For templates <code>greater</code>, <code>less</code>, <code>greater_equal</code>, and <code>less_equal</code>, the
partial specializations for <code>shared_ptr</code> shall yield a total order, even if
the built-in operators <code>&lt;</code>, <code>&gt;</code>, <code>&lt;=</code>, and <code>&gt;=</code> do not. 
Moreover, <code>less&lt;shared_ptr&lt;T&gt; &gt;::operator()(a, b)</code> shall return
<code>std::less&lt;T*&gt;::operator()(a.get(), b.get())</code>.
</p></blockquote>

<p>
This is necessary in order to use <code>shared_ptr</code> as the key in associate
containers because
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2637.pdf">n2637</a>
changed <code>operator&lt;</code> on <code>shared_ptr</code>s to be
defined in terms of <code>operator&lt;</code> on the stored pointers (a mistake IMHO
but too late now.)  By 7.6.9 <a href="https://wg21.link/expr.rel">[expr.rel]</a>/2 the result of comparing builtin
pointers is unspecified except in special cases which generally do not
apply to <code>shared_ptr</code>.
</p>

<p>
Earlier versions of the WP
(<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf">n2798</a>,
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2857.pdf">n2857</a>)
had the following note on that paragraph:
</p>

<blockquote><p>
[Editor's note: It's not clear to me whether the first sentence is a requirement
or a note. The second sentence seems to be a requirement, but it doesn't really belong 
here, under <code>operator&lt;</code>.]
</p></blockquote>

<p>
I agree completely - if partial specializations are needed they should
be properly specified.
</p>

<p>
20.3.2.2.8 <a href="https://wg21.link/util.smartptr.shared.cmp">[util.smartptr.shared.cmp]</a>/6 has a note saying the comparison operator
allows <code>shared_ptr</code> objects to be used as keys in associative
containers, which is misleading because something else like a
<code>std::less</code> partial specialization is needed.  If it is not correct that
note should be removed.
</p>

<p>
20.3.2.2.8 <a href="https://wg21.link/util.smartptr.shared.cmp">[util.smartptr.shared.cmp]</a>/3 refers to '<code>x</code>' and
'<code>y</code>' but the prototype has parameters '<code>a</code>' and
'<code>b</code>' - that needs to be fixed even if the rest of the issue is
NAD.
</p>

<p>
I see two ways to fix this, I prefer the first because it removes the
need for any partial specializations and also fixes <code>operator&gt;</code> and
other comparisons when defined in terms of <code>operator&lt;</code>.
</p>

<ol>
<li>
<p>
Replace 20.3.2.2.8 <a href="https://wg21.link/util.smartptr.shared.cmp">[util.smartptr.shared.cmp]</a>/3 with the following and
remove p5:
</p>

<blockquote><pre>
template&lt;class T, class U&gt; bool operator&lt;(const shared_ptr&lt;T&gt;&amp; a, const shared_ptr&lt;U&gt;&amp; b);
</pre>
<blockquote>
<p>
3  <i>Returns:</i> <del><code>x.get() &lt; y.get()</code>.</del>
<ins><code>std::less&lt;V&gt;()(a.get(), b.get())</code>, where <code>V</code> is the
composite pointer type (7.6.9 <a href="https://wg21.link/expr.rel">[expr.rel]</a>).</ins>
</p>

<p>
4 <i>Throws:</i> nothing.
</p>

<p><del>
5 For templates <code>greater</code>, <code>less</code>, <code>greater_equal</code>, and <code>less_equal</code>, the
partial specializations for <code>shared_ptr</code> shall yield a total order, even if
the built-in operators <code>&lt;</code>, <code>&gt;</code>,
<code>&lt;=</code>, and <code>&gt;=</code> do not. Moreover,
<code>less&lt;shared_ptr&lt;T&gt; &gt;::operator()(a, b)</code> shall return
<code>std::less&lt;T*&gt;::operator()(a.get(), b.get())</code>.
</del></p>
<p>
6 [<i>Note:</i> Defining a comparison operator allows
<code>shared_ptr</code> objects to be used as keys in associative
containers. &mdash; <i>end note</i>]
</p>
</blockquote>
</blockquote>
</li>


<li>
<p>
Add to 20.3.2.2 <a href="https://wg21.link/util.smartptr.shared">[util.smartptr.shared]</a>/1 (after the <code>shared_ptr</code> comparisons)
</p>

<blockquote><pre>
template&lt;class T&gt; struct greater&lt;shared_ptr&lt;T&gt;&gt;;
template&lt;class T&gt; struct less&lt;shared_ptr&lt;T&gt;&gt;;
template&lt;class T&gt; struct greater_equal&lt;shared_ptr&lt;T&gt;&gt;;
template&lt;class T&gt; struct less_equal&lt;shared_ptr&lt;T&gt;&gt;;
</pre></blockquote>

<p>
Remove 20.3.2.2.8 <a href="https://wg21.link/util.smartptr.shared.cmp">[util.smartptr.shared.cmp]</a>/5 and /6 and replace with:
</p>

<blockquote><pre>
template&lt;class T, class U&gt; bool operator&lt;(const shared_ptr&lt;T&gt;&amp; a, const shared_ptr&lt;U&gt;&amp; b);
</pre>
<blockquote>
<p>
3  <i>Returns:</i> <code><del>x</del><ins>a</ins>.get() &lt; <del>y</del><ins>b</ins>.get()</code>.
</p>

<p>
4 <i>Throws:</i> nothing.
</p>

<p><del>
5 For templates <code>greater</code>, <code>less</code>, <code>greater_equal</code>, and <code>less_equal</code>, the
partial specializations for <code>shared_ptr</code> shall yield a total order, even if
the built-in operators <code>&lt;</code>, <code>&gt;</code>,
<code>&lt;=</code>, and <code>&gt;=</code> do not. Moreover,
<code>less&lt;shared_ptr&lt;T&gt; &gt;::operator()(a, b)</code> shall return
<code>std::less&lt;T*&gt;::operator()(a.get(), b.get())</code>.
</del></p>
<p><del>
6 [<i>Note:</i> Defining a comparison operator allows
<code>shared_ptr</code> objects to be used as keys in associative
containers. &mdash; <i>end note</i>]
</del></p>
</blockquote>

<pre><ins>
template&lt;class T&gt; struct greater&lt;shared_ptr&lt;T&gt;&gt; :
binary_function&lt;shared_ptr&lt;T&gt;, shared_ptr&lt;T&gt;, bool&gt; {
  bool operator()(const shared_ptr&lt;T&gt;&amp; a, const shared_ptr&lt;T&gt;&amp; b) const;
};
</ins></pre>

<blockquote><p><ins>
<code>operator()</code> returns <code>greater&lt;T*&gt;()(a.get(), b.get())</code>.
</ins></p></blockquote>

<pre><ins>
template&lt;class T&gt; struct less&lt;shared_ptr&lt;T&gt;&gt; :
binary_function&lt;shared_ptr&lt;T&gt;, shared_ptr&lt;T&gt;, bool&gt; {
  bool operator()(const shared_ptr&lt;T&gt;&amp; a, const shared_ptr&lt;T&gt;&amp; b) const;
};
</ins></pre>

<blockquote><p><ins>
<code>operator()</code> returns <code>less&lt;T*&gt;()(a.get(), b.get())</code>.
</ins></p></blockquote>

<pre><ins>
template&lt;class T&gt; struct greater_equal&lt;shared_ptr&lt;T&gt;&gt; :
binary_function&lt;shared_ptr&lt;T&gt;, shared_ptr&lt;T&gt;, bool&gt; {
  bool operator()(const shared_ptr&lt;T&gt;&amp; a, const shared_ptr&lt;T&gt;&amp; b) const;
};
</ins></pre>

<blockquote><p><ins>
<code>operator()</code> returns <code>greater_equal&lt;T*&gt;()(a.get(), b.get())</code>.
</ins></p></blockquote>

<pre><ins>
template&lt;class T&gt; struct less_equal&lt;shared_ptr&lt;T&gt;&gt; :
binary_function&lt;shared_ptr&lt;T&gt;, shared_ptr&lt;T&gt;, bool&gt; {
  bool operator()(const shared_ptr&lt;T&gt;&amp; a, const shared_ptr&lt;T&gt;&amp; b) const;
};
</ins></pre>

<blockquote><p><ins>
<code>operator()</code> returns <code>less_equal&lt;T*&gt;()(a.get(), b.get())</code>.
</ins></p></blockquote>

</blockquote>
</li>
</ol>

<p><i>[
2009-11-18: Moved to Tentatively Ready after 5 positive votes on c++std-lib.
]</i></p>




<p id="res-1262"><b>Proposed resolution:</b></p>
<p>
Replace 20.3.2.2.8 <a href="https://wg21.link/util.smartptr.shared.cmp">[util.smartptr.shared.cmp]</a>/3 with the following and
remove p5:
</p>

<blockquote><pre>
template&lt;class T, class U&gt; bool operator&lt;(const shared_ptr&lt;T&gt;&amp; a, const shared_ptr&lt;U&gt;&amp; b);
</pre>
<blockquote>
<p>
3  <i>Returns:</i> <del><code>x.get() &lt; y.get()</code>.</del>
<ins><code>less&lt;V&gt;()(a.get(), b.get())</code>, where <code>V</code> is the
composite pointer type (7.6.9 <a href="https://wg21.link/expr.rel">[expr.rel]</a>) of <code>T*</code> and <code>U*</code>.</ins>
</p>

<p>
4 <i>Throws:</i> nothing.
</p>

<p><del>
5 For templates <code>greater</code>, <code>less</code>, <code>greater_equal</code>, and <code>less_equal</code>, the
partial specializations for <code>shared_ptr</code> shall yield a total order, even if
the built-in operators <code>&lt;</code>, <code>&gt;</code>,
<code>&lt;=</code>, and <code>&gt;=</code> do not. Moreover,
<code>less&lt;shared_ptr&lt;T&gt; &gt;::operator()(a, b)</code> shall return
<code>std::less&lt;T*&gt;::operator()(a.get(), b.get())</code>.
</del></p>
<p>
6 [<i>Note:</i> Defining a comparison operator allows
<code>shared_ptr</code> objects to be used as keys in associative
containers. &mdash; <i>end note</i>]
</p>
</blockquote>
</blockquote>






</body>
</html>
