title: std::reference_wrapper/std::unique_ptr alternative for std::vector that stores polymorphic value
body: <p>I usually store objects in <code>std::vector</code> as value and use std::move to transfer ownership. The problem is that storing by value can cause slicing when used with polymorphic objects.</p>
  
  <p>This blog: <a href="https://oopscenities.net/2012/08/09/reference_wrapper/" rel="nofollow noreferrer">https://oopscenities.net/2012/08/09/reference_wrapper/</a> which shows that <code>std::vector</code> can be used to store polymorphic object in <code>std::vector</code>. Problem is that since <code>std::reference_wrapper</code> simply stores the reference to the object, the lifetime of the object must be guaranteed for the duration of <code>std::vector&lt;std::reference_wrapper&lt;&gt;&gt;</code>'s life time.</p>
  
  <p>eg:</p>
  
  <pre><code>class PolymorphicClass; //derived from SuperClass
  namespace MyProject
  {
      class MyClass
      {
  
      public:
          std::vector&lt;std::reference_wrapper&lt;SuperClass&gt;&gt; _member;
      };
  }
  
  int main()
  {
      MyClass test;
      test._member.push_back(PolymorphicClass());
  }
  </code></pre>
  
  <p>This wouldn't work. Instead of <code>std::reference_wrapper</code> I can use <code>std::unique_ptr</code> but I was wondering if there is another option.</p>