First part :
std::initializer_list
is a really helpful feature of C++11, so I wondered how it is implemented in the standard library. From what I read here, the compiler creates an array of type T
and gives the pointer to the initializer_list<T>
.
It also states that copying an initializer_list
will create a new object referencing the same data : why is it so ? I would have guessed that it either :
- copies the data for the new
initializer_list
- moves ownership of the data to the new
initializer_list
Second part :
From just one of many online references for the std::vector
constructors:
vector (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type());
(6) initializer list constructor
Constructs a container with a copy of each of the elements in il, in the same order.
I am not comfortable with move semantics yet, but couldn't the data of il
be moved to the vector
? I am not aware of the deep implementation of std::vector
but IIRC it uses plain-old arrays.
What is the underlying structure of std::initializer_list?
Abstracted away from you, for good reason. – Abeleinitializer_list
only provides read access to its elements. – Moorfowlstd::initializer_list
is a language feature and thestd::
part is misleading to make you think it is a library feature. Even looking at the code of initializer_list there is something strange about it that makes it a special object it seems. In fact in the GCC STL there is a comment that reads: ` // The compiler can call a private constructor.` This reinforces my belief that it is not a normal class. If this is true, I wonder if one can specialize it or extend it. – Misshapen