Basic doubt about expression templates
Hello all,
I am trying to use expression templates/template metaprogramming to solve matrix systems at compile time. Since I am just starting, I need some help to make sure I am on the right track.
I tried out the following basic template in a file called "Matrix.h"(file attached). Below is the constructor for a two dimensional array.
Code:
template<class T, unsigned int r=1, unsigned int c=1>
class Matrix{
private:
unsigned int rows, columns;
T **mat_data;
public:
Matrix() {
std::cout << "Constructor MATRIX" << std::endl;
rows=r;
columns=c;
create_matrix();
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
mat_data[i][j]=0;
std::cout << "checking" << std::endl;
}
}
}
void create_matrix() {
try {
mat_data=new T *[rows];
}
catch (std::bad_alloc xa) {
std::cout << "Out of memory.\n";
exit(1);
}
for (int row_count=0;row_count<rows;row_count++) {
try {
mat_data[row_count]=new T [columns];
}
catch (std::bad_alloc xa) {
std::cout << "Out of memory.\n";
exit(1);
}
}
return;
}
};
Just for checking, I made the empty constructor Matrix() produce an array with all rows x columns elements initialized to 0. I need to check if this is created at compile time. So I added the cout "checking".
The main program has this code:
Code:
Matrix<float, 2, 2> m1;
Should this produce an array m1 of 2x2 with all 0 and with four lines of "checking" when we compile using g++ main.cpp?
Thanks in advance.