[英]How to reduce a number of parameters of a template template parameter
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: 通过使用antoher(非侵入式)模板机制:
//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. 问题是 - C ++中是否有一种语法来支持这一点。
Best Regards, 最好的祝福,
Marcin 马尔钦
You can use boost mpl bind for this 你可以使用boost mpl bind
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. 如果你把eval放在里面就行了。 This probably will be the easiest way to solve your issue. 这可能是解决问题的最简单方法。 I hope I did this time get your problem better ;-) 我希望这次能让你的问题变得更好;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.