gpt4 book ai didi

C++11 模板,确定返回类型

转载 作者:IT老高 更新时间:2023-10-28 22:40:03 26 4
gpt4 key购买 nike

我正在构建一个矩阵库,我正在尝试使用 policy-based design .所以我的基类是提供存储方法和一些访问功能。我还有一个提供数学函数的函数矩阵。这很好用,但是运算符(operator)有一个大问题*因为返回类型。我会用一些代码来解释它。

提供堆栈存储的基类:

template < typename T, unsigned int rows, unsigned int cols>
class denseStackMatrix {
public:
typedef T value_type;

private:
value_type grid[rows][cols];
const unsigned int rowSize;
const unsigned int colSize;

然后我有提供数学功能的矩阵类:

template <typename MatrixContainer >
class matrix : public MatrixContainer {
public:
typedef MatrixContainer Mcontainer;

matrix<Mcontainer>& operator +(const matrix<Mcontainer>&);
matrix<Mcontainer>& operator *(const matrix<Mcontainer>&);

operator+ 始终有效,operator* 仅适用于方阵。所以我们仍然需要一个用于所有矩阵的。就是这样错误的。我已经尝试了几件事,但没有任何效果。我在 c++0x 的帮助下寻找这样的东西(使用c++0x 不是必需的)你会注意到“???” :)

friend auto operator * (const matrix<T1>& matrix1, const matrix<T2>& matrix2)
-> decltype(matrix<???>);

问题的一个例子

matrix<denseStackMatrix<int,3,2> > matrix1;
matrix<denseStackMatrix<int,2,4> > matrix2;
matrix<denseStackMatrix<int,3,4> > matrix3 = matrix1 * matrix2;

这里它会提示类型,因为它不匹配任何两个参数类型。但是编译器需要在编译时知道类型,我不知道如何提供。

我知道设计还有其他选择,但我真的在为这种情况寻找解决方案..

谢谢!

最佳答案

接受@hammar 的想法,但部分特化以允许正常语法,如问题所示:

template<class MatrixContainer>
class matrix;

template<
template<class,int,int> class MatrixContainer,
class T, int rows, int cols
>
class matrix< MatrixContainer<T,rows,cols> >{
typedef MatrixContainer<T,rows,cols> Mcontainer;
typedef matrix<Mcontainer> this_type;
static int const MyRows = rows;
static int const MyCols = cols;

public:
template<int OtherCols>
matrix<MatrixContainer<T,MyRows,OtherColls> > operator*(matrix<MatrixContainer<T,MyCols,OtherCols> > const& other){
typedef matrix<MatrixContainer<T,MyCols,OtherCols> > other_type;
typedef matrix<MatrixContainer<T,MyRows,OtherCols> > result_type;
// ...
}
};

编辑:正如您在评论中所说,您还可以使用它来创建一个不使用具有行和列大小作为模板参数的 MatrixContainer 的矩阵:

template<
template<class> class MatrixContainer,
class T
>
class matrix< MatrixContainer<T> >{
typedef MatrixContainer<T> Mcontainer;
typedef matrix<Mcontainer> this_type;

public:
// normal matrix multiplication, return type is not a problem
this_type operator*(this_type const& other){
// ensure correct row and column sizes, e.g. with assert
}

// multiply dynamic matrix with stack-based one:
template<
template<class,int,int> class OtherContainer,
int Rows, int Cols
>
this_type operator*(matrix<OtherContainer<T,Rows,Cols> > const& other){
// ensure correct row and column sizes, e.g. with assert
}
};

用法:

// stack-based example
matrix<DenseStackMatrix<int,3,2> > m1;
matrix<DenseStackMatrix<int,2,4> > m2;
matrix<DenseStackMatrix<int,3,4> > m3 = m1 * m2;

// heap-based example
matrix<DenseHeapMatrix<int> > m1(3,2);
matrix<DenseHeapMatrix<int> > m2(2,4);
matrix<DenseHeapMatrix<int> > m3 = m1 * m2;

关于C++11 模板,确定返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6145247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com