Search code examples
c++stdarraybracesinitialization-list

How to pass array of arrays to a template class with non-type parameters


I would assume the below code would work to initialize the Matrix class, but for Matrix C I get the following:

error C2440: 'initializing': cannot convert from 'initializer list' to 'Math::Linear::Matrix<int,2,2>'

        template<class T, unsigned int Rows, unsigned int Cols>
        class Matrix
        {
        public:
            Matrix(std::array<std::array<T,Cols>,Rows> ArrayArray)
            {
            
            }
        }

    std::array<std::array<int, 2>, 2> A = {{ {{1,1}} , {{1,1}} }};
    Matrix<int, 2, 2> B = A;
    Matrix<int, 2, 2> C = {{ {{1,1}} , {{1,1}} }};

Solution

  • To use initializer list initialization, you need one more { }.

    std::array<std::array<int, 2>, 2> A = {{ {{1,1}} , {{1,1}} }}  ;
    Matrix<int, 2, 2> C =               { {{ {{1,1}} , {{1,1}} }} };