Question

I am trying to understand a template class in C++. First, I would like to understand what this line means:

template <typename T, typename Ord = columns, typename All = abc::allocator<T,16> >
class matrix

where columns and allocator are respectively a struct and a class defined somewhere else (the second in the namespace abc). What troubles me is the fact that it seems to have a typename which has already been initialized. What does this mean? Should I also initialize the typename of Ord and All when I want to use this template?

Besides, there is also this only constructor:

explicit matrix(unsigned int rows = 0, unsigned int cols = 0, T init = T())

but it seems to have already been initialized. And what should init mean?

I assure you that I looked at all the code, but there is nothing that helps to understand better. Thank you for your attention.

Edit: Thank you everybody for your answers. Just a little reassurance (I am a noob in C++):

int const& operator() operator()(unsigned int i, unsigned int j) const

This method means that, when we initialize the class foo, we can call it by foo()(1,2), where i=1 and j=2. Am i right? And what do the two "const" refer to?

Thank you again!

Was it helpful?

Solution

template <typename T, typename Ord = columns, typename All = abc::allocator<T,16> >
class matrix
{ 
    //...
};

Those are default template parameters, they work just as default function arguments - you can specify them, but if you don't, they are defaulted.

And you can see an example of usage of default arguments of the function.


Bottom line - all the following lines are correct:

matrix<int> a; // matrix<int, columns, abc::allocator<int, 16> >
matrix<int, rows> b; // matrix<int, rows, abc::allocator<int, 16> >
matrix<int, columns, abc::other_allocator<int, 32> > c; // obvious

matrix<int> a = matrix<int>(); // constructor called with 0, 0 and 
// int() - default constructed T - in this case, int -  as arguments
matrix<int> a(1, 2); // constructor called with 1, 2 and int() as arguments
matrix<int> a(1, 2, 100); // obvious

OTHER TIPS

It means that the user of the class has set a reasonable default value- as in, you can provide one, but you don't have to. The same is of the constructor arguments. As for what the arguments mean, only you can answer that.

It's a default value. If the the template argument isn't specified, it will take the default.
Just like you have default values in functions:

void blah(int a = 0) { }

As you know in C++ function arguments can have default values and in case that user do not provide that parameters, then C++ compiler use that default value for them. Now in constructor init has a default value of T() which means value of type using its default constructor, for example if T=int then T() mean 0 and if it is an std::string it is an empty string. You can even use this syntax for other parameters:

explicit matrix(
    unsigned int rows = unsigned int(),
    unsigned int cols = unsigned int(),
    T init = T());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top