如何声明自引用模板类型 [英] How to declare a self referencing template type

查看:112
本文介绍了如何声明自引用模板类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况就像这个例子:

I've a situation that is like this contrived example:

template<class TFeature> struct Controller {};

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

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

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

The Controller is templated to accept features and my problem is that some of the features need the type of the controller as a template parameter. This makes the typedef on the last line of the sample not compile.

这是可能还是需要重新思考设计?

Is this possible or do I need to rethink the design?

推荐答案

为了做到这一点,你应该做一些元程序的魔法(相信我不是一个容易的任务)。但是如果你真的痴迷它,并且使用 boost 是一个选项,你可以看看 boost :: mpl 可以有这样的:

In order to accomplish this you should do some meta programming magic(and believe me it is not an easy task). But if you really nead it and using boost is an option for you take a look at boost::mpl and you can have something like this:

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;

这篇关于如何声明自引用模板类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆