0

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!

Pippo
  • 1,543
  • 3
  • 21
  • 40

4 Answers4

1

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Thank you for your answer. However, T() (which is defined only there) seems that init should be an empty "something". Am I correct? – Pippo Oct 21 '12 at 15:09
  • @Pippo The expression `T()` returns a value-initialized object of type `T` – zero for the numeric types, and a default-constructed object for class types. – Philipp Oct 21 '12 at 15:35
  • If it has a default constructor. Else, it will value initialize each member – Puppy Oct 21 '12 at 22:25
1
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
Martin York
  • 257,169
  • 86
  • 333
  • 562
Griwes
  • 8,805
  • 2
  • 43
  • 70
0

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) { }
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
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());
BigBoss
  • 6,904
  • 2
  • 23
  • 38