[英]std::hash template partial specialization
I wrote some class with template: 我用模板写了一些类:
template <class T, class Allocator = ::std::allocator<T> >
class my_list;
I should write ::std::hash specializtion for this class. 我应该为这个类编写:: std :: hash specializtion。 How can I do that?
我怎样才能做到这一点? Simple partial specialization:
简单的部分专业化:
namespace std {
template <class T, class Allocator>
class hash<my_list<T, Allocator> >{
public :
size_t operator()(const my_list<T, Allocator> &x ) const{
return ...;
}
};
}
But I can't write simple partial specialization, because it forbidden by C++ ISO: 但我不能写简单的部分特化,因为它被C ++ ISO禁止:
ISO/IEC 14882 Third edition 2011-09-01
ISO / IEC 14882第三版2011-09-01
17.6.4.2.1 Namespace std [namespace.std]
17.6.4.2.1命名空间std [namespace.std]
2 The behavior of a C++ program is undefined if it declares ... an explicit or partial specialization of any member class template of a standard library class or class template.
2如果C ++程序声明了标准库类或类模板的任何成员类模板的显式或部分特化,则它的行为是未定义的。
What can I do? 我能做什么?
The paragraph you're quoting does not apply. 您引用的段落不适用。 You're partialy specialising a class template (
std::hash
), not a member class template of a standard library class or class template.
您正在专门化一个类模板 (
std::hash
),而不是标准库类或类模板的成员类模板。 std::hash
is not a member of any class or class template. std::hash
不是任何类或类模板的成员。
For your case, paragraph 1 of the same section applies, and that allows specialisation when at least one user-defined type is involved (emphasis mine): 对于您的情况,同一部分的第1段适用,并且当涉及至少一个用户定义的类型时允许专门化(强调我的):
The behavior of a C++ program is undefined if it adds declarations or definitions to namespace
std
or to a namespace within namespace std unless otherwise specified.如果C ++程序向命名空间
std
或命名空间std
中的命名空间添加声明或定义,则它是未定义的,除非另有说明。 A program may add a template specialization for any standard library template to namespacestd
only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.只有当声明取决于用户定义的类型并且特化符合原始模板的标准库要求且未明确禁止时, 程序才可以将任何标准库模板的模板特化添加到命名空间
std
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.