隐式转换类模板

Implicit conversion class templates

本文关键字:转换      更新时间:2023-10-16

我正在尝试实现一个具有线性代数运算的矩阵类。我想使该类可用于一些值类型,如uintucharfloatdouble

标题如下所示:

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.

但是,我尝试了2种方法,但没有成功。

方法 1operator +定义为

//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;

并为每种情况编写专业化,但是,我无法让编译器正确推断模板参数。

这两种方法似乎都不正确。

谁能给我指一个方向?

您可以使用方法 3 并使用 C++11 的自动返回类型扣除来为您计算类型。 用

template<typename K>
auto operator+(const Matrix<K> &other) const -> Matrix<decltype(std::declval<T>() + std::declval<K>())>;

这表示返回的矩阵将具有添加到KT的类型。

您将无法使用此规则创建自定义规则,但它将遵循标准的促销/转换规则。

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>>{}, "!" );