3

I'd like to initialize a std::array of std::pair via std::initializer_list.

pair<int, int> p = {3,4};//ok
array<pair<char,char>, 3> p = { make_pair('{','}'), make_pair('[',']'), make_pair('(',')') };//ok
array<pair<char,char>, 3> p = { {'{','}'}, {'[',']'}, {'(',')'} };//not ok

Why doesn't my third option work? Moreover this works fine, as well:

vector<pair<char, char>> brackets = { {'{','}'}, {'[',']'}, {'(',')'} };
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
user3455638
  • 569
  • 1
  • 6
  • 17

1 Answers1

5

Initializing std::array with a braced initializer list is a bit tricky, because you need an extra set of braces (as it is an aggregate):

array<pair<char,char>, 3> p = {{ {'{','}'}, {'[',']'}, {'(',')'} }};
                               ^                                 ^

std::vector is different, because using a braced initializer list will result in the std::initializer_list constructor being called, and not using aggregate initialization like std::array.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162