std :: hash模板部分专门化 [英] std::hash template partial specialization
问题描述
我写了一些类与模板:
template <class T, class Allocator = ::std::allocator<T> >
class my_list;
我应该为这个类写:: std :: hash specializtion。我该怎么办?
简单的部分专门化:
I should write ::std::hash specializtion for this class. 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 ...;
}
};
}
但是我不能写简单的部分特化,因为它被C ++ ISO禁止:
But I can't write simple partial specialization, because it forbidden by C++ ISO:
ISO / IEC 14882第三版2011-09-01
ISO/IEC 14882 Third edition 2011-09-01
17.6 .4.2.1命名空间std [namespace.std]
17.6.4.2.1 Namespace std [namespace.std]
2如果C ++程序声明了...的任何成员类的显式或部分特化,它的行为是未定义的
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.
我可以做什么?
推荐答案
您引用的段落不适用。您偏向专门化类模板( std :: hash
),而不是 成员类模板 std :: hash
不是任何类别或类别范本的成员。
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
is not a member of any class or class template.
对于你的情况,同一节的第1段适用,并且允许在涉及至少一个用户定义类型时进行专门化(强调我):
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):
如果向命名空间中添加声明或定义 std
或命名空间中的
命名空间,C ++程序的行为是未定义的std除非另有规定。 只有当声明取决于用户定义的类型时,程序可以将任何标准库模板的模板专用化
添加到命名空间 std
,并且专业化符合原始模板的标准库要求,并且没有明确禁止
。
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. 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 :: hash模板部分专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!