Tuesday, November 6, 2018

c++ - Strange typenames and constructor in a template



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




template  >
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!


Answer



template  >
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 a; // matrix >
matrix b; // matrix >
matrix > c; // obvious

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

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...