I created a matrix class:
template <typename T>
class Matrix
{
static_assert(std::is_arithmetic<T>::value,"");
public:
Matrix(size_t n_rows, size_t n_cols);
Matrix(size_t n_rows, size_t n_cols, const T& value);
void fill(const T& value);
size_t n_rows() const;
size_t n_cols() const;
void print(std::ostream& out) const;
T& operator()(size_t row_index, size_t col_index);
T operator()(size_t row_index, size_t col_index) const;
bool operator==(const Matrix<T>& matrix) const;
bool operator!=(const Matrix<T>& matrix) const;
Matrix<T>& operator+=(const Matrix<T>& matrix);
Matrix<T>& operator-=(const Matrix<T>& matrix);
Matrix<T> operator+(const Matrix<T>& matrix) const;
Matrix<T> operator-(const Matrix<T>& matrix) const;
Matrix<T>& operator*=(const T& value);
Matrix<T>& operator*=(const Matrix<T>& matrix);
Matrix<T> operator*(const Matrix<T>& matrix) const;
private:
size_t rows;
size_t cols;
std::vector<T> data;
};
I want to provide a ctor taking an std:initializer_list. I have to ask also the number of rows and columns. I can check if the std:initializer_list size is equal to rows*columns and lunch an exception if is not, but I don’t like very much this solution. If the std:initializer_list is too long, I prefer to use std:initializer_list elements while the matrix is full: the problem is that I don’t now how access the std:initializer_list element by element. Can someone help me? Do you have a better solution to initialize the matrix with std:initializer_list?
You can get at the elements of an
initializer_list<T>with the member functionsbegin()andend(). In this respect, it acts like a container.