title: Equality-compare std::weak_ptr
body: <p>I want to compare two std::weak_ptr's or one std::weak_ptr and one std::shared_ptr for equality.</p>
  
  <p>What I want to know is whether the object each of the weak_ptr's/shared_ptr's point to is the same.
  The comparison should yield negative results not just if the addresses don't match, but also if the underlying object was deleted and then reconstructed with the same address by chance.</p>
  
  <p>So basically, I want this assertion to hold even if the allocator reserves the same address:</p>
  
  <pre><code>auto s1 = std::make_shared&lt;int&gt;(43);
  std::weak_ptr&lt;int&gt; w1(s1);
  
  s1.reset();
  
  auto s2 = std::make_shared&lt;int&gt;(41);
  std::weak_ptr&lt;int&gt; w2(s2);
  
  assert(!equals(w1,w2));
  </code></pre>
  
  <p>The weak_ptr templates do not provide equality operators, and as I understood that's <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1590.html">for a good reason</a>.</p>
  
  <p>So a naive implementation would look like this:</p>
  
  <pre><code>template &lt;typename T, typename U&gt;
  inline bool naive_equals(const std::weak_ptr&lt;T&gt;&amp; t, const std::weak_ptr&lt;U&gt;&amp; u)
  {
      return !t.expired() &amp;&amp; t.lock() == u.lock();
  }
  
  template &lt;typename T, typename U&gt;
  inline bool naive_equals(const std::weak_ptr&lt;T&gt;&amp; t, const std::shared_ptr&lt;U&gt;&amp; u)
  {
      return !t.expired() &amp;&amp; t.lock() == u;
  }
  </code></pre>
  
  <p>If the first weak_ptr expired in the meantime, it yields 0. If not, I upgrade the weak_ptr to a shared_ptr and compare the addresses.</p>
  
  <p>The problem with this is that i have to lock the weak_ptr's twice (once)! I'm afraid that takes too much time.</p>
  
  <p>I came up with this:</p>
  
  <pre><code>template &lt;typename T, typename U&gt;
  inline bool equals(const std::weak_ptr&lt;T&gt;&amp; t, const std::weak_ptr&lt;U&gt;&amp; u)
  {
      return !t.owner_before(u) &amp;&amp; !u.owner_before(t);
  }
  
  
  template &lt;typename T, typename U&gt;
  inline bool equals(const std::weak_ptr&lt;T&gt;&amp; t, const std::shared_ptr&lt;U&gt;&amp; u)
  {
      return !t.owner_before(u) &amp;&amp; !u.owner_before(t);
  }
  </code></pre>
  
  <p>Which checks if the owner block of u is not "before" t's and t's not before u's, so t == u.</p>
  
  <p>Does this work as I intend it? Do two weak_ptr's created from distinct shared_ptr's always compare as non-equal this way?
  Or did I miss something?</p>
  
  <p><strong>Edit:</strong> Why do I want to do this in the first place?
  I want to have a container with shared pointers, and I want to hand out references to the objects in it.
  I can't use iterators, since they may be invalidated. I could hand out (integer) ID's, but that leads to problems with uniqueness and would require a map type and complicate search/insertion/removal operations.
  The idea is to use a std::set and give out the pointers themselves (capsuled in a wrapper class) as keys, so that clients can use the weak_ptr's to access the objects in the set.</p>