如下code我正在尝试初始化数组(原始 C-classic 和 std::array
),最大限度地减少元素类型的使用,即 S
:
#include <array>
struct S { unsigned u; int i; };
auto std_array = std::array<S, 3>{
S{0U, 0},
S{1U, 1},
S{2U, 2}
};
S raw_array[] = {
{0U, 0},
{1U, 1},
{2U, 2}
};
/* // compile error: excess elements in struct initializer
std::array<S,3> std_array_no_type = {
{0U, 0},
{1U, 1},
{2U, 2}
};
*/
std::array<S,3> std_array_one_type_only = {
S{0U, 0},
{1U, 1},
{2U, 2}
};
int main() {}
使用raw_array
我可以指定S
只有一次。但尝试同样的 std::array
不起作用(请参阅评论的 std_array_no_type
)。我必须指定 S
仅针对初始化列表中的第一个元素键入每个或(这也是问题的有趣部分)(请参阅 std_array_one_type_only
)。
那么,有没有办法定义一个初始化的 std::array
使用类型 S
的对象只有一次?如果不是,则根据标准的哪一条条款?以及为什么单一显式类型 S
允许std_array_one_type_only
被编译?
最佳答案
关于第一个问题,
So, is there a way to define an initialized
std::array
object using the typeS
only once?
您可以添加另一对 {}
,因为 std::array
实际上包含一个底层数组。
std::array<S,3> std_array_one_type_only = {{{0U, 0}, {1U, 1}, {2U, 2}}};
// ^ ^ for std::array
// ^ ^ for underlying array
// ^ ^ for the 1st element of underlying array
// ^ ^ for the 2nd element of underlying array
// ^ ^ for the 3rd element of underlying array
关于第二个问题,
And why single explicit type
S
allowsstd_array_one_type_only
to be compiled?
在 aggregate initialization ,嵌套初始化列表的大括号可以省略,
If the aggregate initialization uses
copy- (until C++14)
list-initialization syntax (T a = {args..}
or T a {args..} (since C++14)
), the braces around the nested initializer lists may be elided (omitted), in which case as many initializer clauses as necessary are used to initialize every member or element of the corresponding subaggregate, and the subsequent initializer clauses are used to initialize the following members of the object.
给定
std::array<S,3> std_array_no_type = {{0U, 0}, {1U, 1}, {2U, 2}};
// ^ ^ for std::array
// ^ ^ for underlying array
第一个 {0U, 0}
将尝试用于初始化整个底层数组,然后导致错误,例如初始化程序中的元素过多,因为 std::array
不包含任何其他子对象。
给定
std::array<S,3> std_array_no_type = {S{0U, 0}, {1U, 1}, {2U, 2}};
// ^ ^ for std::array
// ^ ^ for the 1st element of underlying array
// ^ ^ for the 2nd element of underlying array
// ^ ^ for the 3rd element of underlying array
S{0U, 0}
不能用于初始化底层数组(不满足聚合初始化的风格),那么它将用于初始化第一个元素底层数组的,即应用上面的大括号省略规则,然后使用以下 {1U, 1}
和 {2U, 2}
来初始化以下成员底层数组的。
关于c++ - 使用声明中的元素类型隐式初始化 std::array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57390870/