简体   繁体   English

我可以假设 std::initializer_list 是作为数组实现的吗

[英]Can I assume that std::initializer_list is implemented as an array

In order words, can one assume:换句话说,可以假设:

for (const auto &r : someInitializerList)
     assert(&r == &someInitializerList.begin()[&r - &*someInitializerList.begin()]);

From the documentation :文档

The underlying array is a temporary array of type const T[N], in which each element is copy-initialized (except that narrowing conversions are invalid) from the corresponding element of the original initializer list.底层数组是一个 const T[N] 类型的临时数组,其中每个元素都是从原始初始化列表的相应元素复制初始化的(除了缩小转换无效)。
The lifetime of the underlying array is the same as any other temporary object, except that initializing an initializer_list object from the array extends the lifetime of the array exactly like binding a reference to a temporary (with the same exceptions, such as for initializing a non-static class member).底层数组的生命周期与任何其他临时对象相同,除了从数组初始化一个 initializer_list 对象会延长数组的生命周期,就像将引用绑定到临时对象一样(有相同的例外,例如初始化非-静态类成员)。 The underlying array may be allocated in read-only memory.底层数组可以分配在只读内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM