If the only types which can exist in the matrix are integer types then just cast it to long
:
template<class T>
void print_matrix(const int& num_rows, const int& num_cols, T** M)
{
for (int row = 0; row < num_rows; row++) {
for (int col = 0; col < num_cols; col++) {
std::cout << std::setw(5) << static_cast<long>(M[row][col]);
}
std::cout << std::endl;
}
}
If this is not what you want then tell me and I'll provide another solution.
Another solution is to create a metafunction to determine what to cast to:
template<typename T>
struct matrix_print_type {
typedef T type;
};
template<>
struct matrix_print_type<char> {
typedef int type; // cast chars to ints
};
template<class T>
void print_matrix(const int& num_rows, const int& num_cols, T** M)
{
for (int row = 0; row < num_rows; row++) {
for (int col = 0; col < num_cols; col++) {
std::cout << std::setw(5) << static_cast<typename matrix_print_type<T>::type>(M[row][col]);
}
std::cout << std::endl;
}
}
You could also use overloads or enable_if.