关于std :: initializer_list的设计的问题 [英] questions regarding the design of std::initializer_list

查看:182
本文介绍了关于std :: initializer_list的设计的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于 std :: initializer_list 的设计有一些疑问。
我在 [support.initlist] 中没有找到答案。



为什么有一个明确定义的默认构造函数? / p>

为什么这个构造函数不是 constexpr



方法 size()不是 constexpr



为什么没有给出大小 initializer_list (如专门 std :: tuple_size )的特征?



为什么不能静态访问它的元素(如专门 std :: get )?



sizeof 应用于 initializer_list

解决方案

从C ++标准的第18.9节:


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


我认为大多数这些事情的原因是std :: initializer_list实际上不是一个容器。它没有值语义,它有指针语义。这是显而易见的最后一部分的引用:复制初始化列表不复制基础元素。看到它们只是为了初始化的目的,我不认为这是令人惊讶的是,你没有得到更多的强大的容器,如元组的所有细节。


I have some questions regarding the design of std::initializer_list. I didn't find answers in [support.initlist].

Why does it have an explicitly defined default constructor?

Why this constructor is not constexpr?

Why the method size() is not constexpr?

Why there's no traits giving the size of initializer_list (like specializing std::tuple_size)?

Why it's not possible to statically access its elements (like specializing std::get)?

What happens when sizeof is applied to initializer_list?

解决方案

From section 18.9 of the C++ Standard:

An object of type initializer_list 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 ]

I think the reason for most of these things is that std::initializer_list isn't actually a container. It doesn't have value semantics, it has pointer semantics. Which is made obvious by the last portion of the quote: Copying an initializer list does not copy the underlying elements. Seeing as they were intended solely for the purpose of initializing things, I don't think it's that surprising that you don't get all the niceties of more robust containers such as tuples.

这篇关于关于std :: initializer_list的设计的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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