【问题标题】:Expression template with CRTP as lvalue以 CRTP 作为左值的表达式模板
【发布时间】:2014-07-31 16:56:51
【问题描述】:

我正在编写一个将表达式模板与 CRTP 结合使用的库。源文件可以在这里找到:https://github.com/mspraggs/pyQCD/tree/master/lib/include/base

表达式模板基于 Wikipedia 文章中关于该主题的示例。我在这里列出代码以防 Wiki 文章将来发生变化:

#include <vector>
#include <cassert>

template <typename E>
// A CRTP base class for Vecs with a size and indexing:
class VecExpression {
public:
  typedef std::vector<double>         container_type;
  typedef container_type::size_type   size_type;
  typedef container_type::value_type  value_type;
  typedef container_type::reference   reference;

  size_type  size()                  const { return static_cast<E const&>(*this).size(); }
  value_type operator[](size_type i) const { return static_cast<E const&>(*this)[i];     }

  operator E&()             { return static_cast<      E&>(*this); }
  operator E const&() const { return static_cast<const E&>(*this); }
};

// The actual Vec class:
class Vec : public VecExpression<Vec> {
  container_type _data;
public:
  reference  operator[](size_type i)       { return _data[i]; }
  value_type operator[](size_type i) const { return _data[i]; }
  size_type  size()                  const { return _data.size(); }

  Vec(size_type n) : _data(n) {} // Construct a given size:

  // Construct from any VecExpression:
  template <typename E>
  Vec(VecExpression<E> const& vec) {
    E const& v = vec;
    _data.resize(v.size());
    for (size_type i = 0; i != v.size(); ++i) {
      _data[i] = v[i];
    }
  }
};

template <typename E1, typename E2>
class VecDifference : public VecExpression<VecDifference<E1, E2> > {
  E1 const& _u;
  E2 const& _v;
public:
  typedef Vec::size_type size_type;
  typedef Vec::value_type value_type;
  VecDifference(VecExpression<E1> const& u, VecExpression<E2> const& v) : _u(u), _v(v) {
    assert(u.size() == v.size());
  }
  size_type size() const { return _v.size(); }
  value_type operator[](Vec::size_type i) const { return _u[i] - _v[i]; }
};

template <typename E>
class VecScaled : public VecExpression<VecScaled<E> > {
  double _alpha; 
  E const& _v;
public:
  VecScaled(double alpha, VecExpression<E> const& v) : _alpha(alpha), _v(v) {}
  Vec::size_type size() const { return _v.size(); }
  Vec::value_type operator[](Vec::size_type i) const { return _alpha * _v[i]; }
};

// Now we can overload operators:

template <typename E1, typename E2>
VecDifference<E1,E2> const
operator-(VecExpression<E1> const& u, VecExpression<E2> const& v) {
  return VecDifference<E1,E2>(u,v);
}

template <typename E>
VecScaled<E> const
operator*(double alpha, VecExpression<E> const& v) {
  return VecScaled<E>(alpha,v);
}

我想要做的是添加另一个表达式模板,它允许分配给原始模板对象的一部分(上面代码中的 Vec 类,以及我链接到的代码中的 LatticeBase 类)。可能的用法:

Vec myvector(10);
Vec another_vector(5);
myvector.head(5) = another_vector; // Assign first 5 elements on myvector
myvector.head(2) = another_vector.head(2); // EDIT

所以我要创建一个新函数 Vec::head,它会返回 Vec 对象的一部分的表达式模板。我不知道这将如何适应我目前拥有的框架。特别是我有以下问题/cmets:

  1. 我已经看到了我希望在不使用 CRTP 的表达式模板中实现的示例。在这种情况下使用 CRTP 可以获得什么?有什么意义吗?我应该放弃它并遵循我找到的其他示例吗?
  2. 在当前框架中,对 Vec 类中的 _data 成员的赋值由 Vec 类中的复制构造函数处理。如果我想使用 Vec::head 返回的表达式模板,这将不起作用,因为赋值发生在保存数据的类中,而不是表达式模板中。
  3. 我尝试在新的表达式模板中创建一个赋值运算符,但这不适用于上面的代码,因为所有表达式模板成员都是 const 引用,因此赋值运算符在编译时被删除。我可以将成员切换为值而不是引用吗?如果需要额外的存储,这会影响性能吗?这是否可行(如果我更改表达式的存储副本而不是表达式本身)?

总的来说,我对如何在上面的代码中添加可用作左值的表达式模板感到困惑。任何有关这方面的指导将不胜感激。

【问题讨论】:

    标签: c++ templates c++11 crtp expression-templates


    【解决方案1】:

    试试这个:

    #include <vector>
    #include <cassert>
    
    template <typename E>
    // A CRTP base class for Vecs with a size and indexing:
    class VecExpression {
    public:
        typedef std::vector<double>         container_type;
        typedef container_type::size_type   size_type;
        typedef container_type::value_type  value_type;
        typedef container_type::reference   reference;
    
        size_type  size()                  const { return static_cast<E const&>(*this).size(); }
        value_type operator[](size_type i) const { return static_cast<E const&>(*this)[i]; }
    
        operator E&()             { return static_cast<E&>(*this); }
        operator E const&() const { return static_cast<const E&>(*this); }
    };
    
    class VecHead;
    
    // The actual Vec class:
    class Vec : public VecExpression<Vec> {
        container_type _data;
    public:
        reference  operator[](size_type i)       { return _data[i]; }
        value_type operator[](size_type i) const { return _data[i]; }
        size_type  size()                  const { return _data.size(); }
    
        Vec(size_type n) : _data(n) {} // Construct a given size:
    
        // Construct from any VecExpression:
        template <typename E>
        Vec(VecExpression<E> const& vec) {
            E const& v = vec;
            _data.resize(v.size());
            for (size_type i = 0; i != v.size(); ++i) {
                _data[i] = v[i];
            }
        }
    
        VecHead head(size_type s);
    };
    
    class VecHead : public VecExpression< VecHead >
    {
        Vec::size_type _s;
        Vec& _e;
    public:
    
        typedef Vec::size_type size_type;
        typedef Vec::value_type value_type;
        VecHead(std::size_t s, Vec& e)
            : _s(s)
            , _e(e)
        {
            assert(_e.size() >= _s);
        }
    
        size_type size() const { return _s; }
        value_type operator[](Vec::size_type i) const { assert(i < _s);  return _e[i]; }
    
        VecHead& operator = (const VecHead& rhs)
        {
            return operator=(static_cast<const VecExpression<VecHead>&>(rhs));
        }
    
        template <typename E>
        VecHead& operator = (const VecExpression<E>& rhs)
        {
            assert(rhs.size() >= _s);
            for (size_type i = 0; i < _s && i < rhs.size(); ++i)
                _e[i] = rhs[i];
            return *this;
        }
    };
    
    VecHead Vec::head(size_type s)
    {
        VecHead aHead(s, *this);
        return aHead;
    }
    
    template <typename E1, typename E2>
    class VecDifference : public VecExpression<VecDifference<E1, E2> > {
        E1 const& _u;
        E2 const& _v;
    public:
        typedef Vec::size_type size_type;
        typedef Vec::value_type value_type;
        VecDifference(VecExpression<E1> const& u, VecExpression<E2> const& v) : _u(u), _v(v) {
            assert(u.size() == v.size());
        }
        size_type size() const { return _v.size(); }
        value_type operator[](Vec::size_type i) const { return _u[i] - _v[i]; }
    };
    
    template <typename E>
    class VecScaled : public VecExpression<VecScaled<E> > {
        double _alpha;
        E const& _v;
    public:
        VecScaled(double alpha, VecExpression<E> const& v) : _alpha(alpha), _v(v) {}
        Vec::size_type size() const { return _v.size(); }
        Vec::value_type operator[](Vec::size_type i) const { return _alpha * _v[i]; }
    };
    
    // Now we can overload operators:
    
    template <typename E1, typename E2>
    VecDifference<E1, E2> const
        operator-(VecExpression<E1> const& u, VecExpression<E2> const& v) {
            return VecDifference<E1, E2>(u, v);
    }
    
    template <typename E>
    VecScaled<E> const
        operator*(double alpha, VecExpression<E> const& v) {
            return VecScaled<E>(alpha, v);
    }
    
    int main()
    {
        Vec myvector(10);
        Vec another_vector(5);
        for (int i = 0; i < 5; ++i)
            another_vector[i] = i;
    
        myvector.head(5) = another_vector; // Assign first 5 elements on myvector
        assert(myvector.head(5).size() == 5);
        for (int i = 0; i < 10; ++i)
        {
            assert(myvector[i] == (i < 5 ? static_cast<double>(i) : 0.));
        }
    
        //! Added test due to comment vec1.head(2) = vec2.head(2) doesn't work.
        Vec vec1(10), vec2(10);
        for (int i = 0; i < 10; ++i)
            vec2[i] = 2 * (vec1[i] = i);
    
        vec1.head(2) = vec2.head(2);
        for (int i = 0; i < 10; ++i)
        {
            if (i < 2)
            {
                assert(vec1[i] == vec2[i]);
            }
            else
            {
                assert(vec1[i] != vec2[i]);
            }
        }
    
        return 0;
    }
    

    【讨论】:

    • 谢谢!这在大多数情况下效果很好,但如果我想做类似vec1.head(2) = vec2.head(2) 的事情,那么编译会失败,error: non-static reference member ‘Vec&amp; VecHead::_e’, can’t use default assignment operator。抱歉,我没有在标题中明确说明这个用例。
    • 嗯,我刚刚在 vc12 上尝试过,它可以工作。你用的是什么编译器?我已将测试用例添加到示例中。这包括你的情况吗?如果没有,您可以发布代码吗?
    • 我使用的是 gcc 4.8.2。我认为 gcc 建议因为 VecHead 类包含引用成员,所以不能使用默认赋值运算符,因为当你有引用成员时,gcc 会删除默认赋值运算符(我认为)。
    • 啊,我明白了!我需要专门使用 VecHead 作为参数定义第二个赋值运算符。我会将其添加到您的解决方案中并标记已回答的问题。
    • 我添加了一个版本...你可以使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多