template question

I am attempting to write template matrix classes just for practice. I had a question regarding the design. As i see it, there are two ways i can go about it.

1
2
3
template <class T, int Row, int Col>
class Matrix{
};

or
1
2
3
4
5
template <class T>
class Matrix{
      int row;
      int col;
};


What are the advantages or disadvantages of either of these methods. Technically, they achieve the same effect. But the first approach seems to be favored. A lot of the open source implementations follow the first technique. Can someone with experience explain why this is so ?
I am not familiar with why either is favored but I can tell you that the first template requires not only the data type that will be stored in the matrix but also the dimensions of the matrix. You must send all 3 arguments in to get it to compile.

The second example just takes the data type to be stored into the matrix and your constructor can decide the default size as well as having set size or resize methods that may be called after it is initialized.

Now you could just as easily change the first one to:


1
2
3
4
template <class T>
class Matrix{
       Matrix (int Row, int Col);
};



Then you could declare it like:

Matrix<int> m;

or

Matrix<int> m(10, 10);


Your first example must be declared like:


Matrix<int, 10, 10> m;

Last edited on
Even in the first case, I can have a resize() function by having row and col as private members which get initialized through the template parameter list. Although in my opinion this constitutes abusing the template functionality.

But is the second approach bad ? If yes, why ?
Like I said, the second approach is fine as long as you provide a parameterized constructor that sets the dimensions.

I myself prefer it that way because if you look at all the STL templates, none of them make you provide non-templated arguments.

They create several constructors for that instead.

Could you imagine having to declare a vector like:

vector<int, 5> v;

for a vector of size 5?

No, instead you can just do:

vector<int> v;

or

vector<int> v(5);



Last edited on
I myself favor the second approach too. However, I wanted to know if I have missed something obvious. Perhaps not.
Yea, I don't really have much experience with C++ other than the 3 classes I had to take for my CS degree, which I am finishing up next semester. I do enjoy the language though and I wish it was compatible with ASP.net because I really like it but C# is good enough for me as far as server-side languages go. (I will most likely go for a web dev job after I graduate)

If you want to mimic the STL templates just browse this site and look at their declarations. All of them take classes as their parameters and that is why they are so easy to use. The second approach is definitely the way to go. Just have plenty of constructors and methods to make it robust.
Last edited on
In the first approach you can avoid having to dynamically allocate the memory. This is good for performance because dynamic allocations is slow and it's also better for cache locality. Because the dimensions is known at compile time the compiler might also be able to optimize the code better.

The second approach takes somewhat more memory but if you want to be able to resize the matrix this is the way to do it.

Could you imagine having to declare a vector like:

vector<int, 5> v;

std::array work that way
std::array<int, 5> a;
Last edited on
1
2
matrix<int, 4, 2> m1;
matrix<int, 5, 4> m2;
m1 and m2 are of different type.
matrix<int> m1(4,2), m2(5,4); They are of the same type now.

Suppose that you overload the operators + - * /
In the first case, if the sizes mismatch you will end with a compile error
With the second method the most that you could expect is a runtime exception

Take a look at std::valarray, its size can change dinamically and if operate with objects of different size you get undefined behaviour. (even the assignment has that issue)
Topic archived. No new replies allowed.