我正在尝试使用线性代数运算来实现Matrix类。我想让这个类适用于一些值类型,例如uint
,uchar
,float
,double
。
标题如下所示:
template<typename T>
class Matrix{
public:
Matrix(int width, int height);
Matrix(const Matrix<T> & other);
virtual ~Matrix();
unsigned int width() const { return width_; }
unsigned int height() const { return height_; };
T * data() const { return data_ptr_; };
private:
T * data_ptr_;
unsigned int width_;
unsigned int height_;
}
源文件如下所示。
template<typename T>
Matrix<T>::Matrix(int width, int height ): width_(width), height_(height)
{
data_ptr_ = new T[width * height];
}
template<typename T>
Matrix<T>::Matrix(const Matrix<T> & other): Matrix(other.width(), other.height() )
{
memcpy(data_ptr_, other.data(), width_ * height_ * sizeof(T);
}
template<typename T>
Matrix<T>::~Matrix()
{
delete []data_ptr_;
}
template class Matrix<double>;
template class Matrix<float>;
...
现在我要定义一个operator +
,它将返回一个普通c ++转换在添加两个值时所做类型的Matrix
,即
Matrix<double> + Matrix<float> => Matrix<double>
Matrix<int> + Matrix<float> => Matrix<float>
我希望能够在没有明确转换的情况下执行此操作。例如
Matrix<float> float_matrix(10,20);
Matrix<int> int_matrix(10,20);
auto sum_matrix = float_matrix + int_matrix;
所以总和应该是float
类型。
然而,我尝试了两种方法却没有成功。
方法1
将operator +
定义为
//inside class definition
Matrix<T> operator+(const Matrix<T> &other) const;
并定义隐式构造函数,如
//inside the class declaration
template<typename K>
Matrix(const Matrix<K> & other);
并仅以明显的层次顺序实例化它们:
uchar-> uint->float->double
,但我仍然需要手动转换不同类型的操作数。
方法2
将operator +
定义为
//inside the class declaration
template<typename K, typename R>
Matrix<R> operator+(const Matrix<K> &other) const;
并为每个案例编写特化,但是,我无法让编译器正确地推导出模板参数。
这两种方法似乎都不正确。
有人能给我指点吗?
答案 0 :(得分:3)
您可以使用方法3并使用C ++ 11的自动返回类型演绎来为您找出类型。使用
template<typename K>
auto operator+(const Matrix<K> &other) const -> Matrix<decltype(std::declval<T>() + std::declval<K>())>;
这表示返回的矩阵将具有添加到T
的{{1}}的类型。
您无法使用此功能制作自定义规则,但会遵循标准的促销/转化规则。
答案 1 :(得分:1)
来自MathanOliver的方法3的变体:定义operator+()
(关于主题建议:永远定义operator+=()
作为方法而operator+()
作为外部函数)不是作为方法而是作为外部函数(你如果需要,可以使它成为Matrix
的朋友。
template <typename T1, typename T2,
typename Tr = decltype(std::declval<T1>() + std::declval<T2>())>
Matrix<Tr> operator+ (Matrix<T1> const & m1, Matrix<T2> const & m2)
{
// something useful
return {m1.width(), m1.height()};
}
以下是完整的编译示例
#include <cstring>
#include <utility>
template <typename T>
class Matrix
{
public:
Matrix(unsigned int width, unsigned int height)
: width_(width), height_(height)
{ data_ptr_ = new T[width * height]; }
Matrix(const Matrix<T> & other)
: Matrix(other.width(), other.height() )
{ std::memcpy(data_ptr_, other.data(), width_ * height_ * sizeof(T)); }
virtual ~Matrix()
{ delete []data_ptr_; }
unsigned int width() const
{ return width_; }
unsigned int height() const
{ return height_; };
T * data() const
{ return data_ptr_; };
private:
T * data_ptr_;
unsigned int width_;
unsigned int height_;
};
template <typename T1, typename T2,
typename Tr = decltype(std::declval<T1>() + std::declval<T2>())>
Matrix<Tr> operator+ (Matrix<T1> const & m1, Matrix<T2> const & m2)
{
return {m1.width(), m1.height()};
}
int main ()
{
Matrix<int> m1{1, 2};
Matrix<float> m2{1, 2};
auto m3 = m1 + m2;
auto m4 = m2 + m1;
static_assert( std::is_same<decltype(m3), Matrix<float>>{}, "!" );
static_assert( std::is_same<decltype(m4), Matrix<float>>{}, "!" );
return 0;
}
获取返回矩阵的类型为默认模板值(Tr
),如果需要,可以显式使用其他类型,如下所示
auto m5 = operator+<int, float, int>(m1, m2);
static_assert( std::is_same<decltype(m5), Matrix<int>>{}, "!" );