给定一个基类,它包含公共静态方法以及所有派生类的一般逻辑(例如创建它们)。子类模板的参数化类型被传递给基类模板(连同子类的类型,以便在基类的静态方法中访问其自定义静态方法)。这些类型的组合是类型定义的新“类型”(这是在基类中完成的,以避免重复某些代码)。然后他们在派生类的公共部分重新定义(类似)。我会在包含助手的类中使用这些类型定义,如下所示:
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
template< class DERIVED >
class HELPERS;
template< class V, class DERIVED >
struct BASE
{
typedef typename V::value_type F;
static
F some_common_operation()
{
return helpers.approximate_x(DERIVED::value()); // some common logic (HELPERS::approximate_x() here) depending on the value of DERIVED::value()
}
static
DERIVED create_biased(F const & x)
{
return DERIVED(F(0.5) * (x + some_common_operation()));
}
protected :
BASE(F const & x_)
: x(x_)
{ ; }
F x;
private :
static
HELPERS< DERIVED > const helpers;
};
template< class V, class DERIVED >
HELPERS< DERIVED > const BASE< V, DERIVED >::helpers;
template< class V >
class DERIVED
: public BASE< V, DERIVED< V > >
{
typedef BASE< V, DERIVED< V > > B;
friend B;
public :
typedef typename B::F F;
DERIVED(F const & x_)
: B(x_)
{ ; }
F shape(F const & y) const
{
return y * x;
}
private :
static constexpr
F value()
{
return F(2.0L); // custom data
}
using B::x;
};
// template< class > class DERIVED1;...
template< class D >
struct HELPERS // set of helpers, that operates on classes derived from BASE
{
typedef typename D::F F; // error: no type named <F> in <class DERIVED<std::vector<double> >>
F approximate_x(F const & x) const
{
using std::sqrt;
return sqrt(x);
}
};
int main()
{
DERIVED< std::vector< double > > d(2.0L);
return EXIT_SUCCESS;
}
我正在尝试在 helpers 类中获取定义,但出现错误(g++ -std=gnu++11 a.cpp)。
a.cpp: In instantiation of «struct HELPERS<DERIVED<std::vector<double> > >»:
a.cpp:44:26: required from «struct BASE<std::vector<double>, DERIVED<std::vector<double> > >»
a.cpp:47:7: required from «class DERIVED<std::vector<double> >»
a.cpp:97:39: required from here
a.cpp:85:27: error: no type named «F» in «class DERIVED<std::vector<double> >»
怎么了?Typedef 及其所有“祖先”都可以在类链中访问(放置在公共部分中)。