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

13,678

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 } } };
Share:
13,678
Neil Kirk
Author by

Neil Kirk

Updated on June 07, 2022

Comments

  • Neil Kirk
    Neil Kirk almost 2 years
    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?