本文介绍了关于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节:

我认为大多数这些事情的原因是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:

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的设计的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:40