title: Is there a way to keep references/pointers to objects in unordered_set?
body: <p>I have a couple of objects of <code>class A</code>. In some other <code>class B</code>, I want to keep an unordered_set of objects of <code>class A</code>. Note that objects of <code>class B</code> thus contain a set which may point to (some of) the same <code>class A</code> objects as another set. It is required that these <code>class A</code> objects are never copied or anything, as they contain member data that is supposed to be used and changed (shared-in-use) by different objects of <code>class B</code> and <code>class A</code>.</p>
  
  <p>First, since I come from C, I thought just to use pointers to the object as the values in the set. However, upon coding I saw that I now needed to also pass overridden hash and equality functionality as I have to compare objects of class A for equality on a private member of <code>class A</code>. The hashing function thus needs to be a public member function of <code>class A</code>.</p>
  
  <p>I was however wondering, since <code>std::unordered_set::insert()</code> seems to take a <em>reference to a value_type</em>, if it is possible to just use A as the value type for the <code>std::unordered_set</code>. This would make the code quite a bit easier, as it would by default use the <code>== operator</code> already implemented in <code>class A</code>. However, I'm not sure if <code>std::unordered_set::insert()</code> copies the objects you pass into it. It seems it doesn't, but the value_type remains A, and not A&amp;.</p>
  
  <p>I also fail to see the difference between <code>std::unordered_set::insert()</code> and <code>std::unordered_set::emplace()</code>. I don't understand what "copy vs move" means.</p>
  
  <p>TLDR: to keep references to objects of user-defined classes in a set or map, is it possible in this case to use <code>A*</code> as value_type? Or <code>std::reference_wrapper&lt;A&gt;</code>? Or just the plain <code>A</code>?</p>
  
  <p>Here is a minimum reproducible example:</p>
  
  <pre><code>class A {
  public:
      bool operator== (A&amp; other);
  private:
      B* b;
      size_t id;
      size_t someIntThatMustBeShared;
  };
  
  class B {
  public:
      std::unordered_set&lt;A ? &gt; as;
  };
  </code></pre>