C ++模板矩阵类 - 方阵专业化(C++ template matrix class - square matrix specialisation)

我正在创建一个简单的C ++矩阵模板类,其定义如下:

template<uint n, uint m, typename T = double>
class Matrix {
private:
    T data[n][m];

    static Matrix<n, m, T> I;

public:
    Matrix();
    Matrix(std::initializer_list<T> l);

    T& at(uint i, uint j);  // one-based index
    T& at_(uint i, uint j); // zero-based index

    template<uint k> Matrix<n, k, T> operator*(Matrix<m, k, T>& rhs);
    Matrix<m, n, T> transpose();
    Matrix<n, m, T> operator+(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T>& operator+=(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T> operator-(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T>& operator-=(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T> operator*(const T& rhs);
    Matrix<n, m, T>& operator*=(const T& rhs);
    Matrix<n, m, T> operator/(const T& rhs);
    Matrix<n, m, T>& operator/=(const T& rhs);

    static Matrix<n, m, T> identity();
};
 

( uint定义为unsigned int )

最终函数Matrix<n, m, T> identity()旨在使用基本单例模式返回静态I成员,该成员是单位矩阵。 显然,单位矩阵仅为方形矩阵定义,所以我尝试了这个:

template<uint n, typename T>
inline Matrix<n, n, T> Matrix<n, n, T>::identity() {
    if (!I) {
        I = Matrix<n, n, T>();
        for (uint i = 0; i < n; ++i) {
            I.at(i, i) = 1;
        }
    }
    return I;
}
 

这给出了错误C2244 'Matrix<n,n,T>::identity': unable to match function definition to an existing declaration 。

我的印象是我可以对模板进行某种特殊化,其中列数和行数相等。 我不确定这是否可能,但你的帮助将不胜感激。

I'm creating a simple C++ matrix template class, with the following definition:

template<uint n, uint m, typename T = double>
class Matrix {
private:
    T data[n][m];

    static Matrix<n, m, T> I;

public:
    Matrix();
    Matrix(std::initializer_list<T> l);

    T& at(uint i, uint j);  // one-based index
    T& at_(uint i, uint j); // zero-based index

    template<uint k> Matrix<n, k, T> operator*(Matrix<m, k, T>& rhs);
    Matrix<m, n, T> transpose();
    Matrix<n, m, T> operator+(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T>& operator+=(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T> operator-(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T>& operator-=(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T> operator*(const T& rhs);
    Matrix<n, m, T>& operator*=(const T& rhs);
    Matrix<n, m, T> operator/(const T& rhs);
    Matrix<n, m, T>& operator/=(const T& rhs);

    static Matrix<n, m, T> identity();
};
 

(uint is defined as an unsigned int)

The final function Matrix<n, m, T> identity() aims to return the static I member which is the identity matrix using a basic singleton pattern. Obviously the identity matrix is only defined for square matrices so I tried this:

template<uint n, typename T>
inline Matrix<n, n, T> Matrix<n, n, T>::identity() {
    if (!I) {
        I = Matrix<n, n, T>();
        for (uint i = 0; i < n; ++i) {
            I.at(i, i) = 1;
        }
    }
    return I;
}
 

Which gives the error C2244 'Matrix<n,n,T>::identity': unable to match function definition to an existing declaration.

My impression was that I could do some sort of specialisation of the template where the number of columns and rows are equal. I'm not sure if this is even possible, but your help would be much appreciated.

最满意答案

尝试这个:

static Matrix<n, m> identity() { static_assert(n == m, "Only square matrices have a identity"); return {}; //TODO }

见: http : //cpp.sh/7te2z

Try this:

static Matrix<n, m> identity() { static_assert(n == m, "Only square matrices have a identity"); return {}; //TODO }

See: http://cpp.sh/7te2z

C ++模板矩阵类 - 方阵专业化(C++ template matrix class - square matrix specialisation)

我正在创建一个简单的C ++矩阵模板类,其定义如下:

template<uint n, uint m, typename T = double>
class Matrix {
private:
    T data[n][m];

    static Matrix<n, m, T> I;

public:
    Matrix();
    Matrix(std::initializer_list<T> l);

    T& at(uint i, uint j);  // one-based index
    T& at_(uint i, uint j); // zero-based index

    template<uint k> Matrix<n, k, T> operator*(Matrix<m, k, T>& rhs);
    Matrix<m, n, T> transpose();
    Matrix<n, m, T> operator+(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T>& operator+=(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T> operator-(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T>& operator-=(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T> operator*(const T& rhs);
    Matrix<n, m, T>& operator*=(const T& rhs);
    Matrix<n, m, T> operator/(const T& rhs);
    Matrix<n, m, T>& operator/=(const T& rhs);

    static Matrix<n, m, T> identity();
};
 

( uint定义为unsigned int )

最终函数Matrix<n, m, T> identity()旨在使用基本单例模式返回静态I成员,该成员是单位矩阵。 显然,单位矩阵仅为方形矩阵定义,所以我尝试了这个:

template<uint n, typename T>
inline Matrix<n, n, T> Matrix<n, n, T>::identity() {
    if (!I) {
        I = Matrix<n, n, T>();
        for (uint i = 0; i < n; ++i) {
            I.at(i, i) = 1;
        }
    }
    return I;
}
 

这给出了错误C2244 'Matrix<n,n,T>::identity': unable to match function definition to an existing declaration 。

我的印象是我可以对模板进行某种特殊化,其中列数和行数相等。 我不确定这是否可能,但你的帮助将不胜感激。

I'm creating a simple C++ matrix template class, with the following definition:

template<uint n, uint m, typename T = double>
class Matrix {
private:
    T data[n][m];

    static Matrix<n, m, T> I;

public:
    Matrix();
    Matrix(std::initializer_list<T> l);

    T& at(uint i, uint j);  // one-based index
    T& at_(uint i, uint j); // zero-based index

    template<uint k> Matrix<n, k, T> operator*(Matrix<m, k, T>& rhs);
    Matrix<m, n, T> transpose();
    Matrix<n, m, T> operator+(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T>& operator+=(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T> operator-(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T>& operator-=(const Matrix<n, m, T>& rhs);
    Matrix<n, m, T> operator*(const T& rhs);
    Matrix<n, m, T>& operator*=(const T& rhs);
    Matrix<n, m, T> operator/(const T& rhs);
    Matrix<n, m, T>& operator/=(const T& rhs);

    static Matrix<n, m, T> identity();
};
 

(uint is defined as an unsigned int)

The final function Matrix<n, m, T> identity() aims to return the static I member which is the identity matrix using a basic singleton pattern. Obviously the identity matrix is only defined for square matrices so I tried this:

template<uint n, typename T>
inline Matrix<n, n, T> Matrix<n, n, T>::identity() {
    if (!I) {
        I = Matrix<n, n, T>();
        for (uint i = 0; i < n; ++i) {
            I.at(i, i) = 1;
        }
    }
    return I;
}
 

Which gives the error C2244 'Matrix<n,n,T>::identity': unable to match function definition to an existing declaration.

My impression was that I could do some sort of specialisation of the template where the number of columns and rows are equal. I'm not sure if this is even possible, but your help would be much appreciated.

最满意答案

尝试这个:

static Matrix<n, m> identity() { static_assert(n == m, "Only square matrices have a identity"); return {}; //TODO }

见: http : //cpp.sh/7te2z

Try this:

static Matrix<n, m> identity() { static_assert(n == m, "Only square matrices have a identity"); return {}; //TODO }

See: http://cpp.sh/7te2z