How should I brace-initialize an std::array of std

2019-01-14 16:17发布

std::array<std::pair<int, int>, 2> ids = { { 0, 1 }, { 1, 2 } };

VS2013 error:

error C2440: 'initializing' : cannot convert from 'int' to 'std::pair' No constructor could take the source type, or constructor overload resolution was ambiguous`

What am I doing wrong?

2条回答
一纸荒年 Trace。
2楼-- · 2019-01-14 17:18

std::array is an aggregate. It has only one data member - an array of the specified type of the std::array specialization. According to the C++ Standard. (8.5.1 Aggregates)

2 When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer list are taken as initializers for the members of the aggregate, in increasing subscript or member order

So this record

std::array<std::pair<int, int>, 2> ids = { { 0, 1 }, { 1, 2 } };

has more initializers then there are data members in std::array.

The data member of std::array is in turn an aggregate. You have to provide for it an initializer list.

So the record will look like

std::array<std::pair<int, int>, 2> ids = { { { 0, 1 }, { 1, 2 } } };

For it would be more clear you can imagine the initialization the following way

std::array<std::pair<int, int>, 2> ids = { /* an initializer for data member of the array */ };

As the data member is aggregate then you have to write

std::array<std::pair<int, int>, 2> ids = { { /* initializers for the aggregate data member*/ } };

And at last

std::array<std::pair<int, int>, 2> ids = { { { 0, 1 }, { 1, 2 } } };
查看更多
地球回转人心会变
3楼-- · 2019-01-14 17:21

Add another pair of braces.

std::array<std::pair<int, int>, 2> ids = { { { 0, 1 }, { 1, 2 } } };

std::array<T, N> is an aggregate class containing a member of type T[N]. Usually, you can initialise that the same way you would a plain T[N] array, but when you're dealing with a non-aggregate element type, you may need to be more explicit.

查看更多
登录 后发表回答