I inherited my class from vector and I would like to be able to assign list to my class like to vector.
My code is as follows:
#include <vector> using namespace std; template<typename T> class Matrix : public vector<vector<T>> { public: Matrix( vector<vector<T>> && m ) : vector<vector<T>>( m ) {} // Tried this approach, but it doesn't work // Matrix(std::initializer_list<std::initializer_list<T>> l){ // } } int main() { Matrix<int> m({{0, 1}, {2, 3}}); // it works // Matrix<int> m = {{0, 1}, {2, 3}}; // error: no instance of constructor "Matrix<T>::Matrix [with T=int]" matches the argument list -- argument types are: ({...}, {...}) }
Matrix<int> m = {{{0, 1}, {2, 3}}};For the gory details, obligatory The Nightmare of Initialization in C++ by Nicolai Josuttis. An hour long presentation on the initialization syntax pain point in C++.{}pair is for theMatrix<int>class, the next{}pair is for the outer vector. And the innermost{}pairs is for the inner vectors.