Note that there are some explanatory texts on larger screens.

plurals
  1. POunordered_set using hash of value object's address
    primarykey
    data
    text
    <p>I have a class which needs to have a <code>std::unordered_set</code> which holds non-copyable, non-moveable entity objects, and whose hash function hashes the instance's address. Something like the following:</p> <pre><code>class A { public: A(); A(const A&amp;) = delete; A(A&amp;&amp;) = delete; void operator=(const A&amp;) = delete; void operator=(A&amp;&amp;) = delete; bool operator==(const A&amp; other) { return this == &amp;other; } }; template&lt;&gt; struct std::hash&lt;A&gt; { size_t operator()(const A&amp; obj) const { return std::hash&lt;A*&gt;()(&amp;obj); } }; class B { private: std::unordered_set&lt;A&gt; entities; }; </code></pre> <p>If <code>emplace()</code> is always used instead of <code>insert()</code>, is it safe to use <code>unordered_set</code> in this way? Does the standard specify that an implementation can't move node objects after they are constructed?</p> <p>What about if <code>A</code> were moveable? Is it guaranteed that the hash function will be called on the object owned by the set, or since the standard library prefers to think of everything as value-objects, is it allowed to hash an <code>insert</code>ed object before storage is allocated for it?</p> <p>As a final thought, I know I could get around all this by using <code>std::unordered_set&lt;std::unique_ptr&lt;A&gt;&gt;</code>, but I'd like to use a custom allocator for the <code>A</code> objects, and I don't want to override <code>new</code> and <code>delete</code> for <code>A</code>.</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.
 

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