Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>make_shared</code> allocates a new <code>int</code>, and wraps a <code>shared_ptr</code> around it. This means that your two <code>shared_ptr&lt;int&gt;</code>s point to <em>different memory</em>, and since you're creating a hash table keyed on pointer value, they are distinct keys.</p> <p>For the same reason, this will result in a size of 2:</p> <pre><code>boost::unordered_set&lt;int *&gt; s3; s3.insert(new int(5)); s3.insert(new int(5)); assert(s3.size() == 2); </code></pre> <p>For the most part you can consider <code>shared_ptr</code>s to act just like pointers, including for comparisons, except for the auto-destruction.</p> <p>You could define your own hash function and comparison predicate, and pass them as template parameters to <code>unordered_map</code>, though:</p> <pre><code>struct your_equality_predicate : std::binary_function&lt;boost::shared_ptr&lt;int&gt;, boost::shared_ptr&lt;int&gt;, bool&gt; { bool operator()(boost::shared_ptr&lt;int&gt; i1, boost::shared_ptr&lt;int&gt; i2) const { return *i1 == *i2; } }; struct your_hash_function : std::unary_function&lt;boost::shared_ptr&lt;int&gt;, std::size_t&gt; { std::size_t operator()(boost::shared_ptr&lt;int&gt; x) const { return *x; // BAD hash function, replace with somethign better! } }; boost::unordered_set&lt;int, your_hash_function, your_equality_predicate&gt; s4; </code></pre> <p>However, this is probably a bad idea for a few reasons:</p> <ol> <li>You have the confusing situation where <code>x != y</code> but <code>s4[x]</code> and <code>s4[y]</code> are the same.</li> <li><p>If someone ever changes the value pointed-to by a hash key <strong>your hash will break</strong>! That is:</p> <pre><code>boost::shared_ptr&lt;int&gt; tmp(new int(42)); s4[tmp] = 42; *tmp = 24; // UNDEFINED BEHAVIOR </code></pre></li> </ol> <p>Typically with hash functions you want the key to be immutable; it will always compare the same, no matter what happens later. If you're using pointers, you usually want the pointer identity to be what is matched on, as in <code>extra_info_hash[&amp;some_object] = ...</code>; this will normally always map to the same hash value whatever <code>some_object</code>'s members may be. With the keys mutable after insertion is it all too easy to actually do so, resulting in undefined behavior in the hash.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload