title: return reference to unique_ptr in set
body: <p>I have a class, Entity, and that class has a property called componentSet. This is a set of unique_ptr's to another class, Component. Each component object has a property called actionStringSet, a set containing strings. </p>
  
  <p>What I want to do is write a member function for Entity that takes a string argument, and returns a reference to whichever component has that string in actionStringSet. They should be unique, so I don't need any special handling if it is in more than one. Also, I'm not sure what to do if none of the components have this string.</p>
  
  <p>What I have so far is:</p>
  
  <pre><code>Component&amp; Entity::get_component_for_action(std::string actionStr) {
      std::cout &lt;&lt; componentSet.size() &lt;&lt; std::endl;
      for (auto&amp; comp : componentSet) {
          std::cout &lt;&lt; comp-&gt;name;
          if (comp-&gt;actionStringSet.count(actionStr)) {
              return comp;
          }
      }
      throw;
  }
  </code></pre>
  
  <p>What I would do in python is return None if nothing was found. I can't do this here, so my first though was to instead raise an error, and catch that in all callers. But I'm pretty sure exceptions aren't supposed to be used in c++ like that.</p>
  
  <p>Anyway, the above code will not compile, saying</p>
  
  <pre><code>/home/jagoly/Projects/sqee/libsqee/logic/entity.cpp:20: error: non-const lvalue reference to type 'sq::Component' cannot bind to a value of unrelated type 'const std::unique_ptr&lt;sq::Component, std::default_delete&lt;sq::Component&gt; &gt;'
              return comp;
                     ^~~~
  </code></pre>
  
  <p>But that tries to return the pointer itself. I fiddled with changing everything else to expect a unique_ptr, but then I just got errors saying that things had been deleted (I'd assume passing around the pointers are not a good idea).</p>
  
  <p>Anyway, what should I change? Is it just a small bit of syntax in that function, do I need to change my data types, or is my approach fundamentally wrong?</p>
  
  <p>Thanks in advanced :)</p>