c++模板操作符+如何使不同的实例与它一起工作

C++ template operator+ how to make diffrent instances work with it

本文关键字:实例 工作 一起 何使不 操作符 c++      更新时间:2023-10-16

首先我有矩阵模板Matrix和此模板矩阵的专门化。但是专门化是用构造函数创建的,其中参数是矩阵的大小。在示例代码A和Z有相同的维度。我想用加法运算符来处理它。但是编译器说:error: no match for 'operator+' in 'Z + A'。那么我需要怎么写算子+来和矩阵一起工作呢?

下面的矩阵模板代码:

template<typename T,int Roz>
    class Matrix
    {
    public:
    T tab[Roz][Roz];
    int z=Roz;
    Matrix()
    {
        for(int i=0;i<Roz;++i)
                for(int j=0;j<Roz;++j)
                    tab[i][j]=0;
    }
    T& operator()(int x,int y){
        return tab[x-1][y-1];
    }
            //Problematic operator
    Matrix& operator+(Matrix<T,Roz> b)
    {
            Matrix<T,Roz> tmp;
            for(int i=0;i<Roz;++i)
                for(int j=0;j<Roz;++j)
                    tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
    }

    friend ostream& operator<<(ostream& out, Matrix<T,Roz> &v)
    {
        for(int i=0;i<v.z;++i)
                {
                    for(int j=0;j<v.z;++j)
                        out<<v.tab[i][j]<<" ";
                    out<<endl;
                }
        return out;
    }
    };
    //Specialization template
template<class T>
class Matrix<T,0> 
{
private:
     Matrix()
    {
    }
public:
    vector<vector<T> > tab;
    int z=0;
    Matrix(int z)
    {
        for(int i=0;i<z;++i)
            tab.push_back(vector<T>(z));
        this->z = z;
        for(int i=0;i<z;++i)
                for(int j=0;j<z;++j)
                    tab[i][j]=0;
    }
    T& operator()(int x,int y){
        return tab[x-1][y-1];
    }
            //Problematic operator
    Matrix& operator+(Matrix<T,0> b)
    {
            Matrix<T,0> tmp(b.z);
            for(int i=0;i<z;++i)
                for(int j=0;j<z;++j)
                    tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
    }
            //Problematic operator  
     Matrix& operator+(Matrix<T,0> &b)
    {
            Matrix<T,0> tmp(b.z);
            for(int i=0;i<z;++i)
                for(int j=0;j<z;++j)
                    tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];


    }

    friend ostream& operator<<(ostream& out, Matrix<T,0> &v)
    {
        for(int i=0;i<v.z;++i)
                {
                    for(int j=0;j<v.z;++j)
                        out<<v.tab[i][j]<<" ";
                    out<<endl;
                }
        return out;
    }
};

当我尝试添加例如矩阵<int,3> and Matrix <Int,> this会出错。我必须如何在两个模板中定义运算符+,它们将一起工作?我可以在专门化模板中重载operator+吗?

当我尝试添加A + z时,下面的代码出错

int main()
{
Matrix<int,3> A, B;
Matrix<int, 4> C;
Matrix<int, 0> Z(3);
A(1,1)=1;
B(1,1)=2;
Z(1,1)=1;

Z + A;//<------- here error between diffrent instance of Matrix template 
}

我真的不知道为什么要将不同维度的矩阵相加,但是为了做到这一点,您必须将方法转换为矩阵参数维度的方法模板:

#include <type_traits>
template <int Roz2>
Matrix& operator+(Matrix<T,Roz2> b)
{
        // check matrix argument size compatibility
        std::static_assert(Roz2 >= Roz, "incompatible matrices for operator+()"); 
        Matrix<T,Roz> tmp;
        for(int i=0;i<Roz;++i)
            for(int j=0;j<Roz;++j)
                tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}

由于Matrix<T,0>是处理动态维度的专门化,因此必须专门化它的运算符:

// in the generic template
Matrix& operator+(Matrix<T,0> b)
{
        assert (b.z < Roz, "incompatible dynamic matrix for operator+");
        Matrix<T,0> tmp(b.Roz);;
        for(int i=0;i<Roz;++i)
            for(int j=0;j<Roz;++j)
                tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}

和在Matrix<T,0>中:

template <int Roz>
Matrix& operator+(Matrix<T,Roz> b)
{
        assert(Roz >= z,"....");
        Matrix<T,0> tmp(b.z);
        for(int i=0;i<z;++i)
            for(int j=0;j<z;++j)
                tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}
然而,在上面的解中,算子是不可交换的。为了做到这一点,我们需要做一些改变:
 // define a min operator as constexpr in case the existing one is not 
template<typename T> constexpr
T const& rmin(T const& a, T const& b) {
    return a > b ? b : a;
}
// generic operator
template <int Roz2>
Matrix<T,rmin(Roz,Roz2)>& operator+(Matrix<T,Roz2> b)
{
        constexpr int R = min(Roz,Roz2);
        Matrix<T,R> tmp;
        for(int i=0;i<R;;++i)
            for(int j=0;j<R;++j)
                tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}
// specialized operator on Roz=0 argument
Matrix<T,0>& operator+(Matrix<T,0> b)
{
        const int r = min(z,b.z);
        Matrix<T,0> tmp(r);
        for(int i=0;i<r;;++i)
            for(int j=0;j<r;++j)
                tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}
// specialized operator in Roz=0 template
   Matrix& operator+(Matrix<T,Roz> b)
{
        return b + *this;
}

您将不得不复制const &参数的操作符代码(或者可能只是摆脱非const参数版本,在这种情况下似乎不是很有用)。

注意,在运算符的非交换版本中,使用静态断言来限制对等于或大于等于维数的矩阵使用运算符(这是c++ 11的一个特性)。

请参阅有关min不是constexpr函数模板的主题的问题以了解更多详细信息。