我想用一个对象列表来初始化向量或数组。这对于向量有效,但不适用于数组:
struct Widget
{
string name;
vector<int> list;
};
struct Object
{
string name;
vector<int> list;
Object(string _name, vector<int> _list) : name(_name), list(_list) { }
};
int main()
{
const vector<Widget> vw = {
{"vw1", {1,2,3}},
{"vw2", {1,2,3}} };
const array<Widget,2> aw = {
{"aw1", {1,2,3}},
{"aw2", {1,2,3}} };
const vector<Object> vo = {
{"vo1", {1,2,3}},
{"vo2", {1,2,3}} };
const array<Object,2> ao = {
{"ao1", {1,2,3}},
{"ao2", {1,2,3}} };
return 0;
}
来自clang的错误:
widget.cpp:36:9: error: excess elements in struct initializer
{"aw2", {1,2,3}} };
^~~~~~~~~~~~~~~~
widget.cpp:41:10: error: no viable conversion from 'const char [4]' to 'Object'
{"ao1", {1,2,3}},
^~~~~
widget.cpp:41:17: error: no matching constructor for initialization of 'Object'
{"ao1", {1,2,3}},
^~~~~~~
什么是向量和数组之间的区别,是什么导致数组类型不支持这种语法?