Specialize template structure with template class as parameter

I am trying to customize my template creation skills (I know very little) by creating a library containing matrices and operations on these matrices. Basically, I want my matrix to be very strongly typed (data type and size known at compile time), and I also want to be able to automatically subtract the type of transpose matrix.

template< typename TDataType, size_t rows, size_t cols > class MyMatrix

Matrices can be nested, therefore it TDataTypecan be an integral type, but also MyMatrix<...>, forcing the data type for the transposed matrix to be not necessarily the same as the original matrix, for example: Transpose( MyMatrix< MyMatrix< char, 2, 3 >, 4, 6 > ) ==> MyMatrix< MyMatrix< char, 3, 2 >, 6, 4 >(the data type of the external matrix has changed)

My first attempt at swapping with a transpose type:

template< typename TDataType >
struct Transpose
  {
  typedef TDataType type;
  };

template<>
struct Transpose< MyMatrix<TDataType, rows, cols> >
  {
  typedef MyMatrix<typename Transpose<TDataType>::type, cols, rows> type;
  };

, Transpose-template MyMatrix ( TDataType).

, ( , ):

template< typename TMatrixType, typename TDataType, size_t rows, size_t cols >
struct Transpose
  {
  typedef TMatrixType type;
  };

template< typename TDataType, size_t rows, size_t cols >
struct Transpose< MyMatrix<TDataType, rows, cols>, TDataType, rows, cols >
  {
  typedef MyMatrix< typename Transpose<TDataType,TDataType,rows,cols>::type, cols, rows > type;
  };

, ; , ?


( , , - ). !

@Bo Persson @Will A: , ( ) , , , (, - 32- ) . , , , , , , , ( , ").

@Bo Perrson: , , , , . , MyMatrix , - Transpose-struct.

@VJo: , . T MyMatrix <... > , Transpose<T> , T. (char, int, double...) , , .

+3
4

, .

:

template< typename TDataType, size_t rows, size_t cols > class MyMatrix

:

template< typename T, size_t rows, size_t cols >
MyMatrix< T, cols, rows > Transpose( const MyMatrix< T, rows, cols > & m )
{
  MyMatrix< T, cols, rows > res;
  // implementation
  return res;
}
+3

/cols ?

, . , .

0

, - , .

, , Will A, , col ?

0

, ( ...)

template<typename T, unsigned rows, unsigned cols>
struct MyMatrix
{
  typedef T value_type;
  T stuff[rows][cols];  // or whatever                                      
};

// For basic types, transpose is identity.                                  
template<typename T>
struct Transpose {
  typedef T result_type;
  result_type operator()(const T & in) {
    return in;
  }
};

// For more complex types, specialize and invoke recursively.
template<typename T, unsigned rows, unsigned cols>
struct Transpose<MyMatrix<T, rows, cols> > {
  typedef MyMatrix<Transpose<T>, cols, rows> result_type;
  result_type operator()(const MyMatrix<T, rows, cols> & in) {
    Transpose<T> transposer;
    // (invoke transposer on each element of in and build result)           
  }
};

Transpose - ; , . unary_function result_type typedef ...

0

All Articles