What is the underlying structure of std::initializer_list?

不想你离开。 提交于 2019-12-05 07:44:49

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!