テンプレートクラスに部分的な特殊化を使用して、そのテンプレートクラスのすべての子がその特殊化を使用できるようにします。例を挙げて説明しましょう:)
template < typename T, unsigned int rows, unsigned int cols>
class BaseMatrix {...};
このクラスには、sparse、dense、diagonalなどの行列の構造を指定する子があります。
template < typename T, unsigned int rows, unsigned int cols>
class DiagonalMatrix : public BaseMatrix<T,rows,cols>{..}
次に、これらのクラスには、ストレージを指定する子が再びあります:スタック配列、ベクトル、リスト、キューなど。
template < typename T, unsigned int rows, unsigned int cols>
class StackDiagonalMatrix : public DiagonalMatrix<T, rows, cols> {..}
次に、すべての数学的機能を提供するクラスMatrixがあります。このテンプレートクラスは、operator +、operator-などを実装します。
template <typename T,
template<typename, unsigned, unsigned> class MatrixContainer,
unsigned Rows,
unsigned Cols>
class matrix;
この最後のクラスでは、次のような専門分野を書きたいと思います。
template <typename T,unsigned Rows, unsigned Cols>
class matrix<T, BaseMatrix, Rows, Cols> {};
template <typename T,unsigned Rows, unsigned Cols>
class matrix<T, DiagonalMatrix, Rows, Cols> {};
しかし、DiagonalMatrixから継承するStackDiagonalMatrixを作成すると、DiagonalMatrixの特殊化が見つかりません。実際には、特殊化はまったく見つかりません。
error: aggregate ‘matrix<int, StackDenseMatrix, 3u, 2u> matrix’ has incomplete type and cannot be defined
今、この問題の解決策はありますか?いくつかのテンプレートクラスの親のためのスペシャライゼーションを書くことができますか?
どうもありがとう!
関係する完全なソース:
template <typename T, unsigned int rows, unsigned int cols>
class BaseMatrix {
protected:
BaseMatrix(){};
static const unsigned rowSize = rows;
static const unsigned colSize = cols;
};
template <typename T, unsigned int rows, unsigned int cols>
class DenseMatrix : public BaseMatrix<T, rows, cols> {
protected:
DenseMatrix(){};
};
template <typename T, unsigned int rows, unsigned int cols>
class StackDenseMatrix : public DenseMatrix<T, rows, cols> {
public:
typedef T value_type;
private:
value_type grid[rows][cols];
StackDenseMatrix();
};
template<typename value_type, unsigned int rows, unsigned int cols>
StackDenseMatrix<value_type, rows,cols>::StackDenseMatrix () {
for (unsigned int i = 0; i < this->rowSize; i++) {
for (unsigned int j = 0; j < this->colSize; j++) {
grid[i][j] = 0;
}
}
}
template <typename T, template<typename, unsigned, unsigned> class MatrixContainer ,unsigned Rows, unsigned Cols>
class matrix;
template <typename T,unsigned Rows, unsigned Cols>
class matrix<T,BaseMatrix, Rows, Cols> {
matrix(){};
};
int main () {
matrix<int, StackDenseMatrix, 3, 2> matrix;
return 0;
}