我们可以在C ++ 14中省略std :: array的双括号吗? [英] Can we omit the double-braces for std::array in C++14?
问题描述
我现在正在阅读C ++ 14的标准草案,也许我的法律术语有点生疏,但我找不到提及允许如下所示初始化的任何提示
I'm reading through the draft standard for C++14 right now, and perhaps my legalese is a bit rusty, but I can't find any mention of allowing initializations like the following
std::array<int, 3> arr{1,2,3};
合法。 (编辑:显然,以上是C ++ 11中的合法语法。)当前,在C ++ 11中,我们必须将std :: array初始化为
being legal. ( Apparently the above is legal syntax in C++11.) Currently in C++11 we have to initialize std::array as
std::array<int, 3> arr{{1,2,3}}; // uniform initialization + aggregate initialization
或
std::array<int, 3> arr = {1,2,3};
我想我听说某个地方他们正在放松C ++ 14中的规则,所以我们没有使用统一初始化时不必使用双括号方法,但是我找不到实际的证明。
I thought I heard somewhere that they were relaxing the rules in C++14 so that we didn't have to use the double-brace method when using uniform initialization, but I can't find the actual proof.
注意:我关心这个的原因是因为我当前正在开发multi_array-类型,不想像
NOTE: The reason I care about this is because I am currently developing a multi_array - type and don't want to have to initialize it like
multi_array<int, 2, 2> matrix = {
{{ 1, 2 }}, {{ 3, 4 }}
};
推荐答案
实际上,您可以在C ++ 11中编写以下内容
Actually you can write the following in C++11 also:
std::array<int, 3> arr{1,2,3};
这是完全有效的语法。
什么是不是在C ++ 11中允许的 strong>类似于 案例(请参见该主题;我不想在这里再写一次,这是一篇很长的文章)。因此,如果您这样问,是的,我们可以省略C ++ 14中的多余花括号。这是建议:
What is not allowed in C++11 though is something like this case (see that topic; I don't want to write this here again, it is a long post). So if you ask that then, yes, we can omit the extra braces in C++14. This is the proposal:
引言说
该文档提出了从聚合初始化中略去括号的规则,以便进行数组的初始化和班级聚合更加统一。为了支持具有单个成员子集合的类集合类型,这些子集合的行为类似于其数组对应对象(即
std :: array
)。
This document proposes a slight relaxation of the rules for eliding braces from aggregate initialization in order to make initialization of arrays and class aggregates more uniform. This change is required in order to support class aggregate types with a single member subaggregate that behave similarly to their array counterparts (i.e.
std::array
).
希望有帮助。
这篇关于我们可以在C ++ 14中省略std :: array的双括号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!