Search code examples
c++templatesexpression-templates

how to evaluate expression while declaring variable (in expression templates)


I'm trying to explore expression templates in C++. I'm trying to create a class for a 3D vector (basically vector of size 3) for storing coordinates, space vectors, forces, etc. which basically have three components.

So far I have only implemented class to perform summation of two vectors. The code works fine if I write vector3d z; z = x + y;. I.e. if I first declare z variable and then perform addition. However I get error when I try to write vector3d z = x + y; in one sentence. I get error: conversion from 'const ExprSum<vector3d, vector3d>' to non-scalar type 'vector3d' requested vector3d z_new = x + y ;

How should I implement to be able to use = during variable declaration?

Below is my complete code:

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

/** @brief The base expression class */

template <class A>
class Expr{
public:
  typedef std::vector<double>                  container_type;
  typedef typename container_type::size_type   size_type;
  typedef typename container_type::value_type  value_type;
  typedef typename container_type::reference   reference;

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


/** @brief The vector3d class : The vector<double> with 3 elements */

class vector3d : public Expr<vector3d> {
private:
  container_type _data;
public:
  vector3d():_data(3){}
  vector3d(const vector3d& _rhs){
      *this = _rhs;
  }

  size_type  size() const { return _data.size(); } // should return 3.

  reference operator [] (size_type i){
    assert(i < _data.size());
    return _data[i];
  }

  value_type operator [] (size_type i) const {
    assert(i < _data.size());
    return _data[i];
  }

  template <class A>
  void operator = (const Expr<A>& _rhs){
    _data.resize(_rhs.size());
    for(size_t i = 0; i < _data.size(); i++){
      _data[i] = _rhs[i];
    }
  }

};

/** @brief class for summation of vectors  */

template <class A, class B>
class ExprSum : public Expr <ExprSum<A,B> >{
private:
  A _u;
  B _v;
public:

  typedef vector3d::value_type value_type;

  ExprSum(const Expr<A>& a, const Expr<B>& b): _u(a), _v(b) {}

  value_type operator [] (size_t i) const { return (_u[i] + _v[i]); }

  size_t size() const { return _u.size(); }
};


/** @brief wrapper function of ExprSum class */
template <class A, class B>
ExprSum <A,B> const operator + (Expr<A> const& u, Expr<B> const& v){
  return ExprSum <A,B> (u,v);
}


int main()
{
    vector3d x,y;

    x[0] = 1;
    x[1] = 2;
    x[2] = 3;

    y[0] = 5;
    y[1] = 4;
    y[2] = 0;

    // works fine
    vector3d z;
    z = x + y;

    vector3d z_new = x + y ; // get error

    return 0;
}

Solution

  • It is because the operator + you've defined produces an Expr<ExprSum<A,B>>, which can only be cast to an ExprSum<A,B>. The constructor of vector3d is expecting a const vector3d &.

    Like the operator = within vector3d, you will need to define a similar vector3d(const Expr<T> &) constructor.