tags:

views:

56

answers:

1

Hi,
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

+1  A: 

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 ;-)

Vinzenz
Thank you for precise answer. The difficult part is that I need to bind only the first parameter of the "Two_Parameter_Template" template, leaving the rest unspecified. The template cannot be "fully" concretized after binding. The second parameter has to be passed later.
Marcin
I just realize that your code almost works anyway. See edit above.
Vinzenz
Thats it! I can believe that I've posted this Question because of such a stupid typo :). The template's syntax can drive one nuts... Thanks a lot for spotting the error.
Marcin