5

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

template<class TFeature> struct Controller {};

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

typedef Controller<Feature::FeatureType,int> DefaultController;

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

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

4

4 回答 4

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;
于 2012-10-21T12:53:38.480 回答
1

您将两个模板参数传递给Controller类,但您已声明它只接受一个。你需要类似下面的东西吗?

typedef Controller<Feature<Controller<int>,int>::FeatureType> DefaultController;
于 2012-10-21T12:25:20.297 回答
0

一种选择是使用虚拟子类而不是 typedef:

struct DefaultController : public Controller<Feature<DefaultController,int>::FeatureType> {};
于 2012-10-21T12:20:57.390 回答
0

似乎您正在尝试将 2 个参数传递给控制器​​模板,而它只能接受一个。

于 2012-10-21T12:27:24.290 回答