c++templatescrtpexpression-templates

How can I combine templated derived class in CRTP with derived class expression templates?


My goal is to implement a vector class Vec that allows for efficient computation of arithmetic expressions like auto vecRes = vecA + vecB * vecC. This is a known problem and a solution using the Curiously Recurring Template Pattern (CRTP) can be found on wikipedia.

I started by adopting an implementation for hardcoded vector element type double including a derived class for addition,

template <typename Exn>
class VecExn
{
public:
    double operator[] ( int32_t idx ) const { return static_cast<Exn const&>( *this )[idx]; }
    int32_t size() const { return static_cast<Exn const&>( *this ).size(); }
};

class Vec: public VecExn<Vec>
{
public:
    Vec() {}
    Vec( std::initializer_list<double> iniLis ) : eles_( iniLis ) {  }
    template < typename Exn1 >
    Vec( VecExn<Exn1> const& exn ) : eles_( exn.size() )
    {
        for( int32_t idx = 0; idx < exn.size(); ++idx )
            eles_[idx] = exn[idx];
    }
    template < typename Exn1 >
    Vec& operator=( VecExn<Exn1> const& exn )
    {
        for( int32_t idx = 0; idx < exn.size(); ++idx )
            eles_[idx] = exn[idx];
        return *this;
    }

    double operator[] ( int32_t idx ) const { return eles_[idx]; }
    double& operator[] ( int32_t idx ) { return eles_[idx]; }
    int32_t size() const { return eles_.size(); };
private:
    std::vector<double> eles_;
};


template <typename Lhs, typename Rhs>
class VecSum: public VecExn<VecSum<Lhs, Rhs> >
{
public:
    VecSum( Lhs const& lhs, Rhs const& rhs ) : lhs_( lhs ), rhs_( rhs ) {}
    double operator[] ( int32_t idx ) const { return lhs_[idx] + rhs_[idx]; }
    int32_t size() const { return lhs_.size(); };
private:
    Lhs const& lhs_;
    Rhs const& rhs_;
};

which works as it should.

However, in my approach to replace double by a template, the base class gets a template template argument and the plain Vec becomes, of course, templated.

template < typename Typ, template<typename> typename Exn >
class VecExn
{
public:
    Typ operator[] ( int32_t idx ) const { return static_cast<Exn<Typ> const&>( *this )[idx];}
    int32_t size() const { return static_cast<Exn<Typ> const&>( *this ).size(); }
};

template <typename Typ>
class Vec: public VecExn<Typ, Vec >
{
public:
    Vec() {}
    Vec( std::initializer_list<Typ> iniLis ) : eles_( iniLis ) {  }
    template < typename Exn1 >
    Vec( VecExn<Exn1> const& exn ) :eles_(exn.size() )
    {
        for( int32_t idx = 0; idx < exn.size(); ++idx )
            eles_[idx] = exn[idx];
    }
    template < typename Exn1 >
    Vec& operator=( VecExn<Exn1> const& exn )
    {
        for( int32_t idx = 0; idx < exn.size(); ++idx )
            eles_[idx] = exn[idx];
        return *this;
    }

    Typ operator[] ( int32_t idx ) const { return eles_[idx]; }
    Typ& operator[] ( int32_t idx ) { return eles_[idx]; }
    int32_t size() const { return eles_.size(); };
private:
    std::vector<Typ> eles_;
};

The problem arises in the VecSum class definition which had been a template class already in the hardcoded double case and is now not recognized as being of the correct form for VecExn.

template <typename Typ, template <typename> typename Lhs, template <typename> typename Rhs>
class VecSum: public VecExn<Typ, VecSum<Typ, Lhs, Rhs> >  // ERROR: does not match the template parameter list for template parameter 'Exn'
{
public:
    VecSum( Lhs<Typ> const& lhs, Rhs<Typ> const& rhs ) : lhs_( lhs ), rhs_( rhs ) {}
    Typ operator[] ( int32_t idx ) const { return lhs_[idx] + rhs_[idx]; }
    int32_t size() const { return lhs_.size(); };
private:
    Lhs<Typ> const& lhs_;
    Rhs<Typ> const& rhs_;
};

How can I resolve this?


Solution

  • As pointed out by @super and @Jarod42 the solution is very simple:

    Don't use a template template argument in the base class or the expression templates for operators but instead replace the return type of double operator[] by auto.

    template <typename Exn>
    class VecExn
    {
    public:
        auto operator[] ( int32_t idx ) const { return static_cast<Exn const&>( *this )[idx]; }
        int32_t size() const { return static_cast<Exn const&>( *this ).size(); }
    };
    
    template <typename Typ>
    class Vec: public VecExn<Vec<Typ>>
    {
    public:
        Vec() {}
        Vec( std::initializer_list<Typ> iniLis ) : eles_( iniLis ) {  }
        template < typename Exn1 >
        Vec( VecExn<Exn1> const& exn ) : eles_( exn.size() )
        {
            for( int32_t idx = 0; idx < exn.size(); ++idx )
                eles_[idx] = exn[idx];
        }
        template < typename Exn1 >
        Vec& operator=( VecExn<Exn1> const& exn )
        {
            for( int32_t idx = 0; idx < exn.size(); ++idx )
                eles_[idx] = exn[idx];
            return *this;
        }
    
        Typ operator[] ( int32_t idx ) const { return eles_[idx]; }
        Typ& operator[] ( int32_t idx ) { return eles_[idx]; }
        int32_t size() const { return eles_.size(); };
    private:
        std::vector<Typ> eles_;
    };
    
    
    template <typename Lhs, typename Rhs>
    class VecSum: public VecExn<VecSum<Lhs, Rhs> >
    {
    public:
        VecSum( Lhs const& lhs, Rhs const& rhs ) : lhs_( lhs ), rhs_( rhs ) {}
        auto operator[] ( int32_t idx ) const { return lhs_[idx] + rhs_[idx]; }
        int32_t size() const { return lhs_.size(); };
    private:
        Lhs const& lhs_;
        Rhs const& rhs_;
    };