3


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

2
  • Can you make your examples more concrete? Commented Oct 10, 2010 at 18:21
  • In the example the Two_Parameter_Template is a checking policy which can throw an exception of type T1. I need to predefine the type of exception in advance (so that the number of "visible" parameters of this policy will be 1 instead of 2)and pass this policy to another template. Commented Oct 10, 2010 at 18:48

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
I just realize that your code almost works anyway. See edit above.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.