[英]How to specialize template template parameter for std::vector in C++
I need a specialization of this code: 我需要此代码的特殊化:
template < class T, class Alloc, template <class, class> class VECTOR >
void function(const VECTOR<T, Alloc> &argument);
for a template std::vector. 用于模板std :: vector。 For now, it works with any 2-argument template ("class VECTOR") argument.
目前,它可以与任何2参数模板(“类VECTOR”)参数一起使用。
In other words: 换一种说法:
// A, B -- any unpredictable types
function< std::vector <A, B> > (arg); // specialization
function< Some2ArgsTemplate <A, B> > (arg); // general
function< SomeOther2ArgsTemplate <A, B> > (arg); // general
Is it possible to implement that with no knowledge about A and B? 是否有可能在不了解A和B的情况下实现? Any C++11/C++14 ideas are OK.
任何C ++ 11 / C ++ 14想法都可以。
Don't specialize, overload: 不要专心,重载:
template < class T, class Alloc>
void function(const std::vector<T, Alloc> &argument);
This will be preferred in you pass in a std::vector
as opposed to any other 2-type class template (eg std::list
). 在传递
std::vector
而不是任何其他2型类模板(例如std::list
)时,这将是首选方法。 And it will be preferred in a consistent, easy-to-reason about way. 并且它将以一致,易于理解的方式被首选。 Also, you can't partially specialize function templates anyway.
另外,无论如何,您不能部分专门化功能模板。
Explicit function template specialization is... problematic . 显式功能模板专业化是... 问题 。 The order that you write your specializations could be significant!
您写专业的顺序可能很重要!
Note that this syntax isn't correct: 请注意,此语法不正确:
function< Some2ArgsTemplate <A, B> > (arg);
Explicitly specified template arguments go left-to-right. 明确指定的模板参数从左到右。 So here you're specifying
T
to be Some2ArgsTemplate <A, B>
... which probably prevents deduction from succeeding. 因此,在这里您将
T
指定为Some2ArgsTemplate <A, B>
...,这可能会阻止推导成功。 You shouldn't need to explicitly specify anything: 您不需要显式指定任何内容:
Some2ArgTemplate<A, B> arg = ...;
function(arg); // deduce T=A, Alloc=B, VECTOR=Some2ArgTemplate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.