Question of typedef templates
-
Let's say I have MatrixBase's base class, where the designs, multipliers, sliding, access to data, etc. are implemented:
template< int rows, int columns, typename type > class MatrixBase { public: MatrixBase(); MatrixBase( std::initializer_list<type> list ); ~MatrixBase();
MatrixBase<rows, columns, type>& operator=( const MatrixBase<rows, columns, type>& other ); template<int otherColumns> MatrixBase<rows, otherColumns, type> operator*( MatrixBase<columns, otherColumns, type>& other ); type& operator()( int row, int column );
};
And the inherited MatrixSquare class, which has methods of characteristic square matrix, such as the Determinor, Transponsion, etc.:
template< int dimension, typename type >
class MatrixSquare : public MatrixBase<dimension, dimension, type> {
public:
MatrixSquare();
MatrixSquare( std::initializer_list<type> list ) : MatrixBase<dimension, dimension, type>(list) {};
~MatrixSquare();MatrixSquare<dimension, type> inverse(); MatrixSquare<dimension, type> transponse(); type& determinant();
?
And now I want to identify a template that would determine which class to be selected depending on the parameters. Something like that:
Matrix<3,3,double> -> MatrixSquare<3,double>
Matrix<1,4,int> -> MatrixBase<1,4,int>
As long as I've thought about it,
template <int rows, int columns, typename type>
using Matrix = MatrixBase<rows, columns, type>;template <int rows, typename type>
using Matrix = MatrixSquare<rows, type>;
I'd like one more thing:
template <int rows, int rows, typename type>
using Matrix = MatrixSquare<rows, type>;
Can we do that?
-
Yeah. This can be accomplished by specializing in classrooms:
template< int rows, int columns, typename type > class Matrix { int matrixType() const {return 0;} // общие функции для всех типов матриц };
template<int size, typename type > // специализация для квадратных матриц
class Matrix<size, size, type>
{
int matrixType() const {return 1;}
// частные реализации общих функции + частные функции
};// проверяем
auto t1 = Matrix<3,4,int>().matrixType(); // 0
auto t2 = Matrix<5,5,int>().matrixType(); // 1
However, it will be necessary to carry out common functions as many times as we have specializations, resulting in duplication of the code. In order to solve this problem, general implementation in the third grade can be tried and all specializations from it:
template< int rows, int columns, typename type >
class MatrixBase
{
// общие функции для всех типов матриц
};template< int rows, int columns, typename type >
class Matrix : public MatrixBase<rows,columns,type>
{
int matrixType() const {return 0;}
// ...
};template<int size, typename type > // специализация для квадратных матриц
class Matrix<size, size, type> : public MatrixBase<size, size, type>
{
int matrixType() const {return 1;}
// частные функции
};
Class
MatrixSquare
Now it's just a chablon pseudonym (for C+11 and above):template<int dimention, typename type>
using MatrixSquare = Matrix<dimention, diemention, type>;