title: Why is not initializer_list in a class decleared a "const std::initializer_list & li"?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8193102/initializer-list-and-move-semantics">initializer_list and move semantics</a>  </p>
  </blockquote>
  
  
  
  <p>Environment: Linux, g++-4.7</p>
  
  <p>I used std::vector and a class of my own to test this. And I found that when using the std::initializer_list to construct a vector, it actually calls the copy constructor of the custom class to make a temporary object. Therefore, I consider it really unefficient and I used "const std::initializer_list &amp; li" to replace it.</p>
  
  <p>Why is it really common in STL library?? Eg:
  </p>
  
  <pre><code>// This is in STL: stl_vector.h
  vector(initializer_list&lt;value_type&gt; __l, const allocator_type &amp; __a = allocator_type())
  //...
  </code></pre>
  
  <p>Is there actually something skiped my mind?</p>
  
  <p>My test code is shown below:
  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;initializer_list&gt;
  
  class Test
  {
  public:
      Test(const Test &amp; t) : v(t.v) {
          std::cout &lt;&lt; "Copy Cons" &lt;&lt; std::endl;
      }
      Test(Test &amp;&amp; t) : v(std::move(t.v)) {
          std::cout &lt;&lt; "MC" &lt;&lt; std::endl;
      }
      Test(int val) : v(val) {}
  private:
      int v;
  };
  
  int main()
  {
      std::vector&lt;Test&gt; vv({Test(0), Test(1), Test(2)});
  
      return 0;
  }//main
  </code></pre>
  
  <p>It's output:</p>
  
  <pre><code>Copy Cons
  Copy Cons
  Copy Cons
  </code></pre>