如何声明自引用模板类型

And*_*own 5 c++ templates

我的情况就像这个人为的例子:

template<class TFeature> struct Controller {};

template<class TController,typename T> struct Feature {
  typedef Feature<TController,T> FeatureType;
};

typedef Controller<Feature::FeatureType,int> DefaultController;
Run Code Online (Sandbox Code Playgroud)

Controller模板化接受功能,我的问题是某些功能需要控制器的类型作为模板参数.这使得样本最后一行的typedef无法编译.

这是可能的还是我需要重新考虑设计?

Big*_*oss 2

为了实现这一点,你应该做一些元编程魔法(相信我,这不是一件容易的事)。但如果你真的需要它并且使用boost是一个选项,你可以看一下boost::mpl,你可以有这样的东西:

template< class ControlerT >
struct FeatureEx {
    typedef ControlerT controler_type;
};
template< class FeatureT >
struct ControlerEx {
    typedef ControlerEx<FeatureT> this_type;
    typedef typename boost::mpl::apply<
        FeatureT, boost::mpl::identity<this_type>
    >::type feature_type;

    feature_type const& get_feature() const {return f_;}

private:
    feature_type f_;
};

typedef ControlerEx<FeatureEx<boost::mpl::placeholders::_1> > DefaultControler;
Run Code Online (Sandbox Code Playgroud)