5

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;

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?

Andy Brown
  • 11,766
  • 2
  • 42
  • 61

4 Answers4

2

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;
BigBoss
  • 6,904
  • 2
  • 23
  • 38
  • I believe this is the approach that could work for me and I've marked it as my accepted answer. Thanks very much to all who took the time to answer. – Andy Brown Oct 21 '12 at 15:36
1

You are passing to the Controller class two template parameters, but you have declared it to take only one. Do you need something like the following?

typedef Controller<Feature<Controller<int>,int>::FeatureType> DefaultController;
Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
0

One option would be to use a dummy subclass instead of a typedef:

struct DefaultController : public Controller<Feature<DefaultController,int>::FeatureType> {};
bdonlan
  • 224,562
  • 31
  • 268
  • 324
0

Seems that you're trying to pass 2 params to you Controller template, while it can accept only one.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85