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?
std::array
is an aggregate. It has only one data member - an array of the specified type of thestd::array
specialization. According to the C++ Standard. (8.5.1 Aggregates)So this record
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
For it would be more clear you can imagine the initialization the following way
As the data member is aggregate then you have to write
And at last
Add another pair of braces.
std::array<T, N>
is an aggregate class containing a member of typeT[N]
. Usually, you can initialise that the same way you would a plainT[N]
array, but when you're dealing with a non-aggregate element type, you may need to be more explicit.