title: std::unique_ptr with std::map
body: <p>I have a <code>std::map</code> where the key is <code>std::shared_ptr&lt;Foo&gt;</code> and the value is <code>std::unique_ptr&lt;Bar&gt;</code> where <code>Foo</code> and <code>Bar</code> are very different classes from a third-party library. I am using this <code>std::map</code> object as an in-memory cache.</p>
  
  <p>I am wondering what the best way of inserting a new entry into this map will be and then returned from a method, given that the <code>Bar</code> passed into the <code>std::unique_ptr</code> will already be constructed?</p>
  
  <p>I currently have the following:</p>
  
  <pre><code>class SomeClass
  {
  public:
  
      const Bar* TryGetBarValue(std::shared_ptr&lt;Foo&gt; foo)
      {
          auto it = _cache.find(foo);
  
          if(it == _cache.end())
          {
              Bar bar = ThirdPartLibrary::CreateBar();
              _cache.emplace(foo, std::make_unique&lt;Bar&gt;(bar));
              return _cache.rbegin()-&gt;second.get();
          }
  
          //return result as raw ptr from unique_ptr
          return it-&gt;second.get();
      }
  
  private:
      std::map&lt;std::shared_ptr&lt;Foo&gt;, std::unique_ptr&lt;Bar&gt;&gt; _cache;
  }
  </code></pre>
  
  <p><strong>EDIT</strong>   </p>
  
  <p>Thanks to the answer provided by Quentin, this is now my implementation:</p>
  
  <pre><code>class SomeClass
  {
  public:
  
      const Bar* TryGetBarValue(std::shared_ptr&lt;Foo&gt; foo)
      {
          auto it = _cachedImages.find(texture);
  
          if (it != _cachedImages.end())
          {
              return it-&gt;second.get();
          }
  
          return _cachedImages.emplace(
                  std::move(texture), 
                  std::make_unique&lt;sf::Image&gt;(texture-&gt;copyToImage())
              ).first-&gt;second.get(); 
          }
  
  private:
      std::map&lt;std::shared_ptr&lt;Foo&gt;, std::unique_ptr&lt;Bar&gt;&gt; _cache;
  }
  </code></pre>
  
  <p>Thanks for all your help!</p>