【发布时间】:2021-03-19 10:41:33
【问题描述】:
我正在尝试定义和初始化一个结构数组。
#include <iostream>
#include <array>
int main() {
struct row{
double a0;
double a1;
};
//method 0: this way works
row c[2] ={{1.0,2.0},{3.0,4.0}};
//method 1: declare and initialization in same line
//std::array<row, 2> a = { {1.0, 2.0}, {3.0, 4.0} };//error: Excess elements in struct initializer
std::array<row, 2> a = {{ {1.0, 2.0}, {3.0, 4.0} }}; //double brace
//method 2, declare, then initialize in different line
std::array<row, 2> b;
//b = { {1.0, 2.0}, {3.0, 4.0} };//error: No viable overloaded '='
b = { { {1.0, 2.0}, {3.0, 4.0} } }; //double brace
return 0;
}
现在我发现 this post 的双括号有效。
只是想知道为什么我们需要额外的大括号来初始化结构数组?
【问题讨论】: