When writing the matrix class, it was decided to use the following approach:

class Matrix { public: Matrix(int _size) :m_size(_size) {}; Matrix(std::initializer_list<std::initializer_list<int>> _input):m_matrix(_input) {}; private: int m_size; std::vector<std::vector<int>> m_matrix; }; 

But he is not a worker, because no suitable constructor for nested vector
What constructor can you think of that would be possible to initialize the form:

 Matrix MyMatrix{{1,2},{3,4}}; 

    2 answers 2

    Try the following approach:

      Matrix(std::initializer_list<std::vector<int>> _input) : m_matrix(_input) {}; 

    The std::vector has a constructor

     vector(initializer_list<value_type>, const allocator_type& __a = allocator_type()) 

    Accordingly, when instantiating a template, we have the following constructor, which accepts the initialization list:

     vector(initializer_list<vector<int>>, const allocator_type& __a = allocator_type()) 

    In this case, initializer_list<vector<int>> not identical to initializer_list<initializer_list<int>> , therefore your initial method did not work.

    • Your option works fine, it remains only to understand the difference. And why this template works. - Barny Gamble

    You can initialize a member in a slightly different way, leaving the same constructor signature:

     #include <iterator> Matrix(std::initializer_list<std::initializer_list<int>> _input) : m_matrix(std::begin(_input), std::end(_input)) { }