I am looking into the Eigen matrix class library and the docs state that a Matrix template class can be instantiated using the following template parameters:
Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
where Scalar
is the type of the Matrix coefficients (double, float, int, complex, etc...). RowsAtCompileTime
and ColsAtCompileTime
are the Matrix dimensions respectively.
For example, a 4x4 Matrix of floats could be instantiated as follows:
Matrix<float, 4, 4> matrix;
The Eigen docs state that in the previous case, because the rows are known at compile time, and for "small-sized" Matrices, memory for the Matrix elements is statically allocated.
However if you choose to allocate memory dynamically (for large matrices or because you don't know at compile time the number of rows and columns), you can use the following template instantiation, which will allocate memory dynamically:
Matrix<float, Dynamic, Dynamic> matrix;
My question is how is this achieved conceptually using the C++ template mechanism? Since I am mostly interested in the C++ techniques used for achieving this, could someone please give me a short example using a Stack type where the memory allocation is controlled in a similar way? Thanks!