首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >混合类型的Boost操作符-转换和私有成员

混合类型的Boost操作符-转换和私有成员
EN

Stack Overflow用户
提问于 2014-02-09 17:03:07
回答 1查看 350关注 0票数 3

我使用Boost-操作器来构造一个矩阵类。(玩具项目)。但是,当我想要混合不同元素类型的矩阵时,我会遇到问题。

基本上,我有一个模板类Matrix<T>,其中T是该矩阵的元素类型。我使用Boost-操作符来定义Matrix<T>实例之间的运算符(例如,按元素进行添加)、Matrix<T>T之间的运算符(例如标量乘法),如果可能的话,还可以在Matrix<T>Matrix<U>之间定义操作符(例如,实矩阵加复矩阵)。

boost操作符支持一个或两个模板参数。如果要在两个相同类型的对象之间使用运算符,则需要一个操作符,如果要混合运算符,则需要两个操作符。

代码语言:javascript
代码运行次数:0
运行
复制
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>作为第二个参数,因为这样我的类就会有两个模板参数,并且类型将取决于我可以操作的矩阵。

代码语言:javascript
代码运行次数:0
运行
复制
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,但这也不起作用。编译器抱怨类型不完整。

代码语言:javascript
代码运行次数:0
运行
复制
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>的转换构造函数。然而,现在我有了一个问题,那就是这是两种不同的类型,而且我无法访问私有成员。所以,我要么需要公开比我想要的更多的东西,要么找到一种不同的方法来做这件事。

你会如何实现这样的事情?

完整的法典

代码语言:javascript
代码运行次数:0
运行
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-09 19:27:05

我就是这样做的:使用一个朋友模板函数(请参阅操作者超载:成员和非会员之间的决定):

代码语言:javascript
代码运行次数:0
运行
复制
template<typename T>
class Matrix
{
public:
    template<typename> friend class Matrix;

然后以后

代码语言:javascript
代码运行次数:0
运行
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21662099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档