Consider:
std::vector<double> v = {1, 2, 3.14};
That’s easily done:
std::vector has no sequence constructor (until we add the one
above), so we try
{1, 2, 3.14} as a set of arguments to other constructors, that is, we try
vector(1,2,3.14). That fails, so all of the examples fail to compile when X is
std::vector.
Now add
vector(initializer_list<E>) to
vector<E> as shown above. Now, the example
works. The initializer list
{1, 2, 3.14} is interpreted as a temporary constructed like this:
Code:
double temp[] = {double(1), double(2), 3.14 } ;
initializer_list<double> tmp(temp,sizeof(temp)/sizeof(double));
vector<double> v(tmp);
That is,
the compiler constructs an array containing the initializers converted to the
desired type (here,
double). This array is passed to vector’s sequence constructor as an
initializer_list. The sequence constructor then copies the values from the array into its
own data structure for elements.