[英]Passing partially specialized template as a template parameter
I have a class template, expecting some other template as a parameter: 我有一个类模板,期望一些其他模板作为参数:
template<
class Key,
template <typename K,template <typename T> class Allocator> class Policy
>
class container {
Policy<Key,Allocator>* _policy;
//some code here
};
and usually i use it with the policy class like this: 通常我将它与策略类一起使用,如下所示:
template <class Key,template <typename T> class Allocator> class policy {
//some code
};
but what if i have to pass additional template parameter to policy class? 但是如果我必须将额外的模板参数传递给策略类呢? Something like:
就像是:
template <time_t Age,class Key,template <typename T> class Allocator> class policy_3 {
//some code
};
What can i do, to allow users of that class, pass the Age paratemeter without touching others? 我该怎么办,允许该类用户通过Age paratemeter而不接触其他人? For example:
例如:
typedef container<key_type,policy_3<100500> > containerWithAge;
You have two options: binding, and rebinding. 您有两个选择:绑定和重新绑定。
In binding, you adapt the ternary policy into a binary one, as expected by the template-template parameter Policy
: 在绑定中,您可以根据模板模板参数
Policy
预期将三元策略调整为二进制Policy
:
template <typename Key, template <typename T> class Allocator>
struct policy_3_100500 : ternary_policy<100500,Key,Allocator> {};
and use policy_3_100500
instead of policy_3<100500>
. 并使用
policy_3_100500
而不是policy_3<100500>
。
To be closer to the syntax you're shooting for, you can use a nested class: 为了更接近您正在拍摄的语法,您可以使用嵌套类:
template <time_t Age>
struct policy_3 {
template <typename Key, template <typename T> class Allocator>
struct type : ternary_policy<Age,Key,Allocator> {};
};
and use policy_3<100500>::type
instead of policy_3<100500>
. 并使用
policy_3<100500>::type
而不是policy_3<100500>
。
The only way to get exactly the syntax you want is to move the ::type
into the class using the policy. 获得所需语法的唯一方法是使用策略将
::type
移动到类中。 That's the second option: rebinding (this is also used in std::allocator, btw). 这是第二个选项:重新绑定(这也用于std :: allocator,btw)。 In this case, you pass the
Policy
as a normal template parameter, and assume a template metafunction, say bind
, to exist: 在这种情况下,您将
Policy
作为普通模板参数传递,并假设模板元函数(例如bind
)存在:
template <time_t Age>
struct policy_3 {
template <typename Key, template <typename T> class Allocator>
struct bind : ternary_policy<Age,Key,Allocator> {};
};
While structually identical to the second option, the difference lies in who calls bind
: In the first option (binding), it's the user of the policy class (by passing policy<100500>::type
explicitly). 虽然结构上与第二个选项相同,但区别在于谁调用
bind
:在第一个选项(绑定)中,它是策略类的用户(通过明确地传递policy<100500>::type
)。 Here, it's the class using the policy: 在这里,它是使用该策略的类:
template <typename Key, typename Policy>
struct container {
typename Policy::template bind<Key,std::allocator<Key>> * _policy;
// ...
}:
As a general note, Policy classes are not usually passed as template-template arguments, but as normal template arguments (precisely because they may have a varying number of arguments themselves). 总的来说,Policy类通常不作为模板模板参数传递,而是作为普通的模板参数传递(正是因为它们本身可能有不同数量的参数)。 The classes using the policy then assume a certain inner structure (typedefs, functions, meta functions, constants) to be present in the policy, of which
bind
is just an example. 然后,使用策略的类假定在策略中存在某个内部结构(typedef,函数,元函数,常量),其中
bind
只是一个示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.