简体   繁体   English

如何减少模板模板参数的参数

[英]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.

相关问题 减少模板参数的数量 - Reduce number of template parameters 模板模板参数,模板参数数量错误 - Template template parameter with wrong number of template parameters 模板模板参数的模板参数数量的偏特化 - partial specialization on the number of template parameters of template template parameter 限制可变参数模板参数包中的参数数量 - Limit the number of parameters in a variadic template parameter pack 具有参数数量的成员函数模板,具体取决于整数模板参数 - Member function template with the number of parameters depending on an integral template parameter 带有类型参数的模板模板参数? - Template template parameter with typed parameters? 使用模板模板获取模板参数个数 function - Get number of template parameters with template template function 如何访问模板包参数的模板参数 - How do I get access to template parameters of a template pack parameter 如何使用模板模板参数中的非默认参数实例化 - How to instantiate with other than defaulted parameters in a template template parameter 基于模板模板参数所采用的参数数量,部分特化模板的语法是什么? - What is the syntax for partially specialising a template based on the number of parameters a template template parameter takes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM