【发布时间】:2021-06-10 06:40:11
【问题描述】:
我的目标是实现一个向量类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 当然会变成模板。
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_;
};
我该如何解决这个问题?
【问题讨论】:
-
你把事情复杂化了。您只需将
Vec设为模板并从VecExn<Vec<T>>继承即可。完全不需要模板模板参数。 -
您可以使用
auto作为返回类型,而不是使用double作为VecSum::operator[]。 (所以你不再需要额外的参数了:-)) -
你的意思是,即使 VecExn 也不应该是模板模板参数,而是有两个模板参数(
operator[]的返回类型我需要Typ)?所以我会有VecExn<T, Vec<T>>。 -
如果你需要可以理解的类型,你可以在 Vec 内部做一个 using 声明,使得 Vec
::value_type 为 T -
是的,除了
Vec之外的任何operator[]都可以使用auto,这就是我所需要的,问题已解决,谢谢!
标签: c++ templates crtp expression-templates