JdlCR
JdlCR

Reputation: 165

multidimensional array template class with expression templates

I am trying to understand expression templates by implementing my own multidimensional array class. Basically, I allocate a continuous chuck of memory and then I compute offsets with the () operator.

Now I want to overload the +,/,*,- with expression templates. The example illustrated in wikipedia is quite illustrative, but it assumes that the datablock is of type double. I would like to have the datatype as a template parameter. I have tried to implement this but I always fail. This is the code so far:

    namespace ader{

    template<class E, typename T> class vexp{
        inline unsigned size()const{return static_cast<E const&>(*this).size();};
        inline T operator[](const unsigned &i) const{ return static_cast<E const&>(*this)[i];}
    };

// ***************************************************************************** // 

    template<class E1, class E2, typename T>
        class vsum:
        public vexp<vsum<E1,E2,T>,T>{
        const E1 &_u;
        const E2 &_v;
        public:
        vsum(const E1 &u, const E2 &v): _u(u), _v(v){};
        inline T operator[](const unsigned &i)const {return _u[i] + _v[i];};
        inline unsigned size()const{return _u.size();};
    };


// ***************************************************************************** // 


    template<typename T, unsigned nDer> class aDer: public ader::vexp<aDer<T,nDer>,T>{
        protected:
        unsigned n;
        T d[nDer+1];

        public:
        unsigned size()const{return n;};

        T  operator[](const unsigned &i)     {return d[i];};
        T &operator[](const unsigned &i)const{return d[i];};

        aDer():n(nDer), d{0}{};
        aDer(const T &in): n(nDer), d{0}{d[0] = in;};
        aDer(const T &in, const unsigned &idx): n(nDer), d{0}{d[0] = in; d[idx+1] = T(1);};
        template<template<typename,unsigned> class U> aDer(const vexp<U<T,nDer>,T> &in){
            for(unsigned ii=0; ii<=nDer; ++ii) d[ii] = in[ii];
        }
    };

    template< class E1, class E2, typename T>
        vsum<E1,E2,T> operator+(const E1 &u, const E2 &v){return vsum<E1,E2,T>(u,v);};


};

The error message:

main2.cc: In function ‘int main()’:
main2.cc:15:27: error: no match for ‘operator+’ (operand types are ‘ader::aDer<float, 2>’ and ‘ader::aDer<float, 2>’)
ader::aDer<float,2> c= a+b;

Is there anything clearly wrong in the code?

EDIT1: the content of main2.cc:

#include "aut2.h"
#include <iostream>

using namespace std;

int main(){

    ader::aDer<float,2> a(1.220334, 1);
    ader::aDer<float,2> b(3.0, 0);

    ader::aDer<float,2> c= a+b;

    cerr<<c[0]<<endl;

}

Upvotes: 0

Views: 119

Answers (1)

r3mus n0x
r3mus n0x

Reputation: 6154

Your operator+ has a non-deducible parameter T. You need to get rid of this parameter and infer the T from E1 and E2.

One way to achieve that would be to define your operator+ like this:

template <class E1, class E2>
auto operator+(const E1 &u, const E2 &v) -> vsum<E1, E2, decltype(u[0] + v[0])>
{
    return { u,v };
}

Another way would be to get rid of the T parameter in all your classes altogether by using auto and decltype(auto) instead:

template <class E> class vexp {
    ...
    inline decltype(auto) operator[](const unsigned &i) const { return static_cast<E const&>(*this)[i]; }
};

In the above code operator[] will return whatever type E::operator[] is returning.

Note that decltype(auto) and auto without a trailing return type specification is a C++14 feature.

Upvotes: 1

Related Questions