隐式转换类模板
Implicit conversion class templates
本文关键字:转换 更新时间:2023-10-16
我正在尝试实现一个具有线性代数运算的矩阵类。我想使该类可用于一些值类型,如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
.
但是,我尝试了2种方法,但没有成功。
方法 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;
并为每种情况编写专业化,但是,我无法让编译器正确推断模板参数。
这两种方法似乎都不正确。
谁能给我指一个方向?
您可以使用方法 3 并使用 C++11 的自动返回类型扣除来为您计算类型。 用
template<typename K>
auto operator+(const Matrix<K> &other) const -> Matrix<decltype(std::declval<T>() + std::declval<K>())>;
这表示返回的矩阵将具有添加到K
的T
的类型。
您将无法使用此规则创建自定义规则,但它将遵循标准的促销/转换规则。
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>>{}, "!" );
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 是否可以从int转换为enum类类型
- 了解 GLM- openGL 中的相机转换
- 将无符号char*转换为std::istream*C++