title: C++ - construct vector of unique_ptr via initialization list
body: <p>I have a class, called <code>group</code>, that shall keep a number of base classes inside it, held in <code>std::unique_ptr</code>s (polymorphism). The order does not matter, nor does if some of the elements are equal between them. It's a task for a <code>std::vector</code>, that I would ideally flag as <code>const</code> in the member declaration.</p>
  
  <p>As part of the design, I would like to pass all the elements that will fit into the <code>group</code> object in an <code>initializer_list</code>, like this:</p>
  
  <pre><code>class base;
  
  class derived1 : base;
  class derived2 : base;
  class derived3 : base;
  
  group my_object({ new derived1() , new derived2() , new derived3() });
  </code></pre>
  
  <p>But I'm having trouble with the following:</p>
  
  <ul>
  <li>The vector inside the <code>group</code> class will be made up of <code>std::unique_ptr</code>, <strong>not</strong> objects.</li>
  <li>As <code>std::initializer_list</code>s may only hold objects of <strong>one type</strong>, that type shall be a pointer-to-base-class.</li>
  </ul>
  
  <p>Taking all that into account, how should I implement it? As the elements held in the <code>group</code> class' vector are not supposed to change, it's much more efficient to initialize it in the initialization list of the constructor. However, <code>std::unique_ptr</code>s are not copyable, and I can't figure out how to construct a <code>vector</code> of <code>unique_ptr&lt;base_class&gt;</code> from an <code>initializer_list</code> made up of raw pointers.</p>