Hello,
The template class looks as follows:
template <class DATA_TYPE,class INT_TYPE>
class Matrix
{
public:
DATA_TYPE *data; //data container
INT_TYPE Rows,Cols; //number of columns and rows
};
Let us consider a situation in which there are four template instantations.
typedef Matrix<float,unsigned int> FloatMatrix;
typedef Matrix<double,unsigned int> DoubleMatrix;
typedef Matrix<signed int,unsigned int>IntMatrix;
typedef Matrix<unsigned int,unsigned int>uIntMatrix;
I have a function that prints a matrix in the console window. It actually does exactly the same for Float- and DoubleMatrix classes and behaves identically for Int- and uIntMatrix classes. But it behaves differently for integer and real matrices.
My question is:Is there any better way for defining the display function instead of writing it four times? If I make one template, it will not work properly for real matrices or for int matrices.
template<class DATA_TYPE,class INT_TYPE>
Display(const Matrix<DATA_TYPE,INT_TYPE> &M)
{
//do sth
}
If I make specializations then I will have four definitions:
template<class INT_TYPE>
Display(const Matrix<double,INT_TYPE> &M)
{
//do sth
}
template<class INT_TYPE>
Display(const Matrix<float,INT_TYPE> &M)
{
//do sth
}
template<class INT_TYPE>
Display(const Matrix<int,INT_TYPE> &M)
{
//do sth
}
template<class INT_TYPE>
Display(const Matrix<unsigned int,INT_TYPE> &M)
{
//do sth
}
Is there any better solution?
Thanks in advance for any suggestions.