什么是std :: initializer_list的底层结构? [英] What is the underlying structure of std::initializer_list?

查看:213
本文介绍了什么是std :: initializer_list的底层结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一部分



std :: initializer_list 的C ++ 11,所以我想知道如何在标准库中实现。从我在这里中读取的内容,编译器创建了 T 并且给出 initializer_list< T>



的指针。 code> initializer_list 将创建一个引用相同数据的新对象:为什么会这样?我会猜到它:




  • 复制新 initializer_list

  • 将资料的拥有权移至新 initializer_list



第二部分



std :: vector 构造函数:

  vector(initializer_list< value_type> il,
const allocator_type& allocator_type());




(6)初始化列表构造函数 / p>

使用 il 中的每个元素的副本构建一个容器。


< blockquote>

我不习惯move语义,但是不能将 il 的数据移动到 vector ?我不知道 std :: vector 的深层实现,但是IIRC它使用了普通的数组。

解决方案


std :: initializer_list

的基础结构是什么? / blockquote>

最有可能的,只是一对指针,或指针和大小。 C ++ 11标准的第18.9 / 2段甚至在(非规范)注释中提到这一点:


code> initializer_list< E> 提供对 const E 类型的对象数组的访问。 [注意:
一对指针或一个指针加一个长度将是 initializer_list 的明显表示。
initializer_list 用于实现8.5.4中规定的初始化列表。复制初始化程序列表执行
不复制基础元素。 -end note]


此外:


我不习惯move语义,但是不能将 il 的数据移动到向量?


,您不能从 initializer_list 的元素移动,因为 initializer_list 应该是不可变的(参见上面引用段落的第一句)。这也是为什么只有 const - 限定成员函数可以访问这些元素的原因。


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?

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.

这篇关于什么是std :: initializer_list的底层结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆