2

我的目标是实现一个向量类Vec,允许高效计算算术表达式,如auto vecRes = vecA + vecB * vecC. 这是一个已知问题,可以在维基百科上找到使用奇怪重复模板模式 (CRTP) 的解决方案。

我首先采用硬编码向量元素类型的实现,double包括用于添加的派生类,

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_;
};

可以正常工作。

然而,在我用模板替换的方法中double,基类得到一个模板模板参数Vec,当然,plain 变成了模板化的。

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_;
};

问题出现在VecSum类定义中,该类定义已经是硬编码double情况下的模板类,现在不被识别为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_;
};

我该如何解决这个问题?

4

1 回答 1

1

正如@super 和@Jarod42 所指出的,解决方案非常简单:

不要在基类中使用模板模板参数或运算符的表达式模板,而是替换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_;
};
于 2021-03-15T10:27:16.270 回答