I want to initialize a vector or an array with a list of objects. It works for vectors, but not for arrays:
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;
}
The error from 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}},
^~~~~~~
What is the difference between vector and array, which prevents the array type from supporting this syntax?