问题描述:


I need to adapt a two parameter template to a one parameter template.

I would like to bind the first parameter of a template:

template<class T1, class T2>

struct Two_Parameter_Template {

// Two ctor's

Two_Parameter_Template() { };

template<class Param>

Two_Parameter_Template(Param) { /* ... */ }

};

by using antoher (non intrusive) template mechanism:

//bind the first parameter of a template "bound", this template should work as

//an adapter reducing the number of parameters required

template<class X, template<class, class> class bound>

struct bind_1st {

template<class T> // this is the resulting one parameter template

struct eval {

eval () {

bound<X, T>();

}

template<class T2>

eval (T2 obj) {

bound<X, T>(obj);

}

};

};

So that I could use this template later, as a paremeter for another template, with one less parameter of it's own (something like bellow):

template <template<class> class One_Parameter_Template>

struct SomeTemplate {

//...

};

// Later in the code

typedef SomeTemplate<bind_1st<Bound_Param_class, Two_Parameter_Template> >::eval ConcreteClass;

The question is - is there a syntax in C++ to support this.

Best Regards,

Marcin

网友答案:

You can use boost mpl bind for this

However it will not do exactly how you would like it to behave

Edit: I saw you made one little mistake in your code that it does not work as you expected:

typedef SomeTemplate< bind_1st<Bound_Param_class, Two_Parameter_Template>::eval > ConcreteClass;

If you put the eval inside it will work. This probably will be the easiest way to solve your issue. I hope I did this time get your problem better ;-)

相关阅读:
Top