title: in-place vector construction from initialization list (for class with constructor arguments)
body: <blockquote>
    <p><em>Possible Duplicate:</em><br>
    <a href="https://stackoverflow.com/questions/8468774/can-i-list-initialize-a-vector-of-move-only-type">Can I list-initialize a vector of move-only type?</a>  </p>
  </blockquote>
  
  
  
  <p><em>Edit 1: Please consider a re-open vote: My question emphasize in-place construction. Move construction is an alternative but not what this questions is about. Thanks for the answers!</em></p>
  
  <p><em>Edit 2: Since I can't answer this question (It got closed) I post my own suggestion here. The following is not as good as the answers I accepted, but may be useful for others. At least only the move constructor is called:</em></p>
  
  <pre><code>std::vector&lt;A2&gt; vec;
  {
    std::array&lt;A2,3&gt; numbers{{{2,3},{5,6},{7,8}}};
    vec.reserve(numbers.size());
    for (auto &amp;v: numbers) vec.emplace_back(std::move(v)) ;
  }
  </code></pre>
  
  <p><strong>Original post:</strong></p>
  
  <p>When thinking about the answer to this question: <a href="https://stackoverflow.com/questions/13147935/initialization-of-classes-within-an-stl-array-of-vectors/13148028#13148028">Initialization of classes within an STL array of vectors</a> I found that I could not find a way to get in-place construction of the vector from an initialization list. What am I missing?</p>
  
  <p>Now trying to be more clear, I would like this (perfectly correct) initialization</p>
  
  <pre><code>std::vector&lt;A2&gt; k{{2,3},{4,5},{8,9}};
  </code></pre>
  
  <p>to have an effect more similar to this:</p>
  
  <pre><code>  std::vector&lt;A2&gt; k2;
    k2.reserve(3);
    k2.emplace_back(2,3);
    k2.emplace_back(4,5);
    k2.emplace_back(8,9);
  </code></pre>
  
  <p>However, in the first case the <em>copy</em> constructor is called for A2 on a temporary while inserting. Is there a way to avoid that? What does the standard say?</p>
  
  <p>I desperately tried </p>
  
  <pre><code>std::vector&lt;A2&gt; k{{2,3},{4,5},std::move(A2{8,9})};
  </code></pre>
  
  <p>but that generates an <em>additional</em> call to the move constructor, something I also did not expect. I just wanted to explicitly hint that A2 is a temporary, something I had thought was implied.</p>
  
  <p>Full example:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  struct A2 {
    int mk;
    int mj;
    A2(int k,int j) : mk(k),mj(j) {
      std::cout &lt;&lt; "     constr for "&lt;&lt;this&lt;&lt; ":"&lt;&lt; mk&lt;&lt;std::endl;
    }
    A2(const A2&amp; a2) {
      mk=a2.mk;
      mj=a2.mj;    
      std::cout &lt;&lt; "copy constr for "&lt;&lt;this&lt;&lt; ":" &lt;&lt; mk&lt;&lt;std::endl;
    }
    A2(A2&amp;&amp; a2) noexcept  {
      mk=std::move(a2.mk);
      mj=std::move(a2.mj);
      std::cout &lt;&lt; "move constr for "&lt;&lt;this&lt;&lt; ":"&lt;&lt; mk&lt;&lt;std::endl;
    }
  };
  
  struct Ano {
    Ano() {
      std::cout &lt;&lt; "     constr for "&lt;&lt;this &lt;&lt;std::endl;
    }
    Ano(const Ano&amp; ano) {
      std::cout &lt;&lt; "copy constr for "&lt;&lt;this&lt;&lt;std::endl;
    }
    Ano(Ano&amp;&amp; ano) noexcept  {
      std::cout &lt;&lt; "move constr for "&lt;&lt;this&lt;&lt;std::endl;
    }
  };
  
  
  int main (){
    // here both constructor and copy constructor is called:
    std::vector&lt;A2&gt; k{{2,3},{4,5},std::move(A2{8,9})};
  
    std::cout &lt;&lt; "......"&lt;&lt;std::endl;
    std::vector&lt;A2&gt; k2;
    k2.reserve(3);
    // here (naturally) only constructor is called:
    k2.emplace_back(2,3);
    k2.emplace_back(4,5);
    k2.emplace_back(8,9);
  
    std::cout &lt;&lt; "......"&lt;&lt;std::endl;  
    // here only constructor is called:
    std::vector&lt;Ano&gt; anos(3);
  
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>     constr for 0xbf9fdf18:2
       constr for 0xbf9fdf20:4
       constr for 0xbf9fdf0c:8
  move constr for 0xbf9fdf28:8
  copy constr for 0x90ed008:2
  copy constr for 0x90ed010:4
  copy constr for 0x90ed018:8
  ......
       constr for 0x90ed028:2
       constr for 0x90ed030:4
       constr for 0x90ed038:8
  ......
       constr for 0x90ed048
       constr for 0x90ed049
       constr for 0x90ed04a
  </code></pre>