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

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.

Leave a Comment