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

坚强是说给别人听的谎言 提交于 2019-11-27 08:37:42

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.

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 } } };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!