行と列の両方のメジャーストレージをサポートできるMatrixテンプレートクラスを作成しています。理想的には、ストレージ形式の影響を受けるメソッドのみを専門にしたいです。ただし、(以下のように)メソッドを特殊化しようとすると、エラーメッセージしか表示されません。
enum MatrixStorage
{
ColumnMajor,
RowMajor
};
template< typename T,
unsigned rows,
unsigned columns,
MatrixStorage storage = ColumnMajor >
class Matrix
{
public:
T & operator () ( unsigned const & row, unsigned const & column );
};
template< typename T,
unsigned rows,
unsigned columns >
T & Matrix< T, rows, columns, ColumnMajor >::
operator () ( unsigned const & row, unsigned const & column )
{
return elements[ ( row + ( rows * column ) ) % ( rows * columns ) ];
}
template< typename T,
unsigned rows,
unsigned columns >
T & Matrix< T, rows, columns, RowMajor >::
operator () ( unsigned const & row, unsigned const & column )
{
return elements[ ( ( row * columns ) + column ) % ( rows * columns ) ];
}
エラー出力:
error C3860: template argument list following class template name must list parameters in the order used in template parameter list
error C2976: 'maths::Matrix<T,rows,columns,storage>' : too few template arguments
error C3860: template argument list following class template name must list parameters in the order used in template parameter list
他の質問で与えられた例に従うと、構文は正しいように見えます。それでも、これを機能させる唯一の方法は、クラス自体を特殊化することです(以下のように)が、これは、ストレージ形式に依存しないすべてのメソッドを複製することを意味します。
enum MatrixStorage
{
ColumnMajor,
RowMajor
};
template< typename T,
unsigned rows,
unsigned columns,
MatrixStorage storage = ColumnMajor >
class Matrix;
template< typename T,
unsigned rows,
unsigned columns >
class Matrix< T, rows, columns, ColumnMajor >
{
T & operator () ( unsigned const & row, unsigned const & column );
};
template< typename T,
unsigned rows,
unsigned columns >
class Matrix< T, rows, columns, RowMajor >
{
T & operator () ( unsigned const & row, unsigned const & column );
};