我正在编写一个矩阵模板类,它可以支持行和列主要存储。理想情况下,我只想专门研究受存储格式影响的方法。但是,当我尝试专门化一种方法时(如下所示),我只得到错误消息。
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 );
};