Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the underlying structure of std::initializer_list?

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.

like image 822
teh internets is made of catz Avatar asked Jun 03 '13 10:06

teh internets is made of catz


People also ask

What is std :: initializer_list?

An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T .

What is the use of initializer list in C++?

Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.

What is uniform initialization in C++?

Uniform initialization is a feature in C++ 11 that allows the usage of a consistent syntax to initialize variables and objects ranging from primitive type to aggregates. In other words, it introduces brace-initialization that uses braces ({}) to enclose initializer values.


1 Answers

What is the underlying structure of std::initializer_list?

Most likely, just a pair of pointers, or a pointer and a size. Paragraph 18.9/2 of the C++11 Standard even mentions this in a (non-normative) note:

An object of type initializer_list<E> provides access to an array of objects of type const E. [ Note: A pair of pointers or a pointer plus a length would be obvious representations for initializer_list. initializer_list is used to implement initializer lists as specified in 8.5.4. Copying an initializer list does not copy the underlying elements. —end note ]

Moreover:

I am not comfortable with move semantics yet, but couldn't the data of il be moved to the vector?

No, you can't move from the elements of an initializer_list, since elements of an initializer_list are supposed to be immutable (see the first sentence of the paragraph quoted above). That's also the reason why only const-qualified member functions give you access to the elements.

like image 130
Andy Prowl Avatar answered Sep 20 '22 22:09

Andy Prowl