lundi 1 avril 2019

How to get size of nested std::initializer_lists?

std::initializer_list has a member function size that returns the number of elements in the initializer list.

Considering there is no [] operator for a std::initializer_list, and a user does not want to use for-loop to access the size of each sub-list:

How can a user get the size of an inner std::initializer_list from std::initializer_list<std::initializer_list>.

For example, from the following example, could you please tell me how a user can access the size of the first nested initializer_list, {1, 2, 3}?

#include <iostream>
#include <string>

int main()
{
  std::initializer_list<std::initializer_list<int>> a = { {1, 2, 3}, {2, 3, 4} };

  std::cout << a.size() << std::endl; // Provides = 2

  // Now I want to access the size of the first nested std::initializer_list:
  // std::cout << a[0].size() << std::endl; // Does not compile

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire