gpt4 book ai didi

c++ - 使用嵌套初始化列表聚合初始化 std::array of subobjects

转载 作者:搜寻专家 更新时间:2023-10-31 02:14:17 25 4
gpt4 key购买 nike

用嵌套的大括号初始化器列表初始化聚合类型(例如 std::array)及其子对象的正确方法是什么?我不想直接调用子类型的构造函数。

这是一个反复出现的问题,我总是对下面的代码不起作用感到惊讶,因为指定了元素的类型,因此编译器可以推导出正确的构造函数。

请注意,示例类型 A 不一定是聚合(但当然它必须支持花括号初始化列表)。

#include <array>    

struct A
{
int values[4];
};

int main()
{
std::array<A, 2> arr{{ 0, 1, 2, 3 }, { 4, 5, 6, 7 }};

// Works only if A is an aggregate, also looks confusing, I don't want to do this
//std::array<A, 2> arr{ 0, 1, 2, 3, 4, 5, 6, 7 };

// I don't want to do this neither
//std::array<A, 2> arr{A{ 0, 1, 2, 3 }, A{ 4, 5, 6, 7 }};

return 0;
}

但我得到的只是错误

error: too many initializers for 'std::array<A, 2ul>'

最佳答案

您可以在子对象的初始化周围添加大括号,例如

std::array<A, 2> arr{{{ 0, 1, 2, 3 }, { 4, 5, 6, 7 }}};

std::array<A, 2> arr{{ 0, 1, 2, 3 }, { 4, 5, 6, 7 }};不起作用,因为对于 brace elision ,

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.

请注意第一个初始化子句 { 0, 1, 2, 3 }可用于 std::array 的整个内部数组的初始化(其余元素将被初始化为零)。然后 { 4, 5, 6, 7}成为多余的条款。

关于c++ - 使用嵌套初始化列表聚合初始化 std::array of subobjects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40112072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com