Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is a sort of contract between the programmer and the compiler. The programmer says <code>{1,2,3,4}</code>, and the compiler creates an object of type <code>initializer_list&lt;int&gt;</code> out of it, containing the same sequence of elements in it. This contract is a requirement imposed by the language specification on the compiler implementation.</p> <p>That means, it is not the programmer who creates manually such an object but it is the compiler which creates the object, and pass that object to function which takes <code>initializer_list&lt;int&gt;</code> as argument. </p> <p>The <code>std::vector</code> implementation takes advantage of this contract, and therefore it defines a constructor which takes <code>initializer_list&lt;T&gt;</code> as argument, so that it could initialize itself with the elements in the initializer-list.</p> <p>Now <strong>suppose for a while</strong> that the <code>std::vector</code> doesn't have any constructor that takes <code>std::initializer_list&lt;T&gt;</code> as argument, then you would get this:</p> <pre><code> void f(std::initializer_list&lt;int&gt; const &amp;items); void g(std::vector&lt;int&gt; const &amp;items); f({1,2,3,4}); //okay g({1,2,3,4}); //error (as per the assumption) </code></pre> <p>As per the assumption, since <code>std::vector</code> doesn't have constructor that takes <code>std::initializer_list&lt;T&gt;</code> as argument, which <em>implies</em> you cannot pass <code>{1,2,3,4}</code> as argument to <code>g()</code> as shown above, because the compiler <em>cannot</em> create an instance of <code>std::vector</code> out of the expression <code>{1,2,3,4}</code> <strong>directly</strong>. It is because no such contract is ever made between programmer and the compiler, and imposed by the language. It is <em>through</em> <code>std::initializer_list</code>, the <code>std::vector</code> is able to create itself out of expression <code>{1,2,3,4}</code>. </p> <p>Now you will understand that <code>std::initializer_list</code> can be used wherever you need an expression of the form of <code>{value1, value2, ...., valueN}</code>. It is why other containers from the Standard library also define constructor that takes <code>std::initializer_list</code> as argument. In this way, no container depends on any other container for construction from expressions of the form of <code>{value1, value2, ...., valueN}</code>.</p> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload