我使用Boost-操作器来构造一个矩阵类。(玩具项目)。但是,当我想要混合不同元素类型的矩阵时,我会遇到问题。
基本上,我有一个模板类Matrix<T>
,其中T
是该矩阵的元素类型。我使用Boost-操作符来定义Matrix<T>
实例之间的运算符(例如,按元素进行添加)、Matrix<T>
和T
之间的运算符(例如标量乘法),如果可能的话,还可以在Matrix<T>
和Matrix<U>
之间定义操作符(例如,实矩阵加复矩阵)。
boost操作符支持一个或两个模板参数。如果要在两个相同类型的对象之间使用运算符,则需要一个操作符,如果要混合运算符,则需要两个操作符。
template<typename T>
class Matrix : boost::addable<Matrix<T>> // Add another matrix of same type.
boost::multiplyable2<Matrix<T>,T> // Scalar multiplication with a `T`.
但是,我不能将Matrix<U>
作为第二个参数,因为这样我的类就会有两个模板参数,并且类型将取决于我可以操作的矩阵。
template<typename T, typename U>
class Matrix : boost::addable2<Matrix<T,U>,Matrix<U,?>> // Now I have two template arguments.
// That's certainly not what I want!
我也尝试过实现我自己版本的boost::addable
,但这也不起作用。编译器抱怨类型不完整。
template<class Derived>
class Addable {
template<class Other>
friend Derived operator+(Derived lhs, const Other &rhs) {
return lhs += rhs;
}
template<class Other>
friend Derived operator+(const Other &lhs, Derived rhs) {
return rhs += lhs;
}
};
另一种方法是定义从Matrix<U>
到Matrix<T>
的转换构造函数。然而,现在我有了一个问题,那就是这是两种不同的类型,而且我无法访问私有成员。所以,我要么需要公开比我想要的更多的东西,要么找到一种不同的方法来做这件事。
你会如何实现这样的事情?
完整的法典
#include <cassert>
#include <utility>
#include <complex>
#include <vector>
#include <algorithm>
#include <iostream>
#include <boost/operators.hpp>
typedef double Real;
typedef std::complex<Real> Complex;
template<typename T>
class Matrix : boost::addable<Matrix<T>>
{
public:
Matrix() = default;
template<typename U>
Matrix(const Matrix<U> &other)
: m_(other.m()), n_(other.n()),
data_(other.data_.begin(), other.data_.end()) { }
Matrix(size_t m, size_t n) : m_(m), n_(n), data_(m*n) { }
Matrix(size_t m, size_t n, const T &initial)
: m_(m), n_(n), data_(m*n, initial) { }
size_t m() const { return m_; }
size_t n() const { return n_; }
size_t size() const {
assert(m_*n_ == data_.size());
return data_.size();
}
const T &operator()(size_t i, size_t j) const { return data_[i*m_ + j]; }
T &operator()(size_t i, size_t j) { return data_[i*m_ + j]; }
void fill(const T &value) {
std::fill(data_.begin(), data_.end(), value);
}
Matrix &operator+=(const Matrix &other) {
assert(dim_match(other));
for (int i = 0; i < size(); ++i) {
data_[i] += other.data_[i];
}
return *this;
}
friend std::ostream &operator<<(std::ostream &o, const Matrix &m) {
if (m.size() == 0) {
o << "()" << std::endl;
return o;
}
for (int i = 0; i < m.m(); ++i) {
o << "( ";
for (int j = 0; j < m.n() - 1; ++j) {
o << m(i,j) << ", ";
}
o << m(i, m.n() - 1) << " )" << std::endl;
}
return o;
}
private:
bool dim_match(const Matrix &other) {
return n_ == other.n_ && m_ == other.m_;
}
private:
int m_, n_;
typedef std::vector<T> Store;
Store data_;
};
int main() {
Matrix<Real> A(2,3, 1.);
Matrix<Complex> B(2,3, Complex(0,1));
auto C = Matrix<Complex>(A) + B;
std::cout << A << std::endl;
std::cout << B << std::endl;
std::cout << C << std::endl;
}
发布于 2014-02-09 11:27:05
我就是这样做的:使用一个朋友模板函数(请参阅操作者超载:成员和非会员之间的决定):
template<typename T>
class Matrix
{
public:
template<typename> friend class Matrix;
然后以后
template <typename T1, typename T2>
Matrix<typename std::common_type<T1, T2>::type>
operator+(Matrix<T1> const& a, Matrix<T2> const& b)
{
Matrix<typename std::common_type<T1, T2>::type> result(a);
return (result += b);
}
注意使用common_type
可以得到一个合理的结果类型(您可能希望在那里介绍您自己的特性,以满足您的特定需求)
看吧,住在Coliru
https://stackoverflow.com/questions/21662099
复制