C ++ 11:为什么有些规则类型不应该有`std :: hash`专用的原因吗? [英] C++11: Are there reasons why some Regular Types should not have `std::hash` specialised?

查看:111
本文介绍了C ++ 11:为什么有些规则类型不应该有`std :: hash`专用的原因吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于常规类型,我的意思是在元素编程中定义Stepanov,基本上说,存在相等的概念,并且相互比较的副本的对象相等。



所以当你有一个常规类型 T 时,等式关系是可传递的( a == b&& amp ; b == c => a == c ),您可以定义一个(非平凡的)哈希函数,该函数与equal( a == b => h(a)== h(b))。始终。



但该标准不包含许多 std :: hash 专精。例如。 std :: complex 没有一个,并且都没有这些容器,除了 vector< bool> bitset

的明显例外。

所以我想知道这里的设计原理是什么。



或者,有不同的问题:是否有理由不提供 std :: hash 为您自己的类型提供了专业化,前提是它们是规则的,并且相等性是可传递的?

解决方案 div>

是的。



当一个类型具有以下两个属性时,我认为您不应该定义 std :: hash $ b


  • 没有一种有效的方法来一致地创建一个覆盖 all 用于描述相等性的数据。

  • 没有有效的和/或直观的方法来选择一个一致的数据子集来散列。 / li>

With a Regular Type, I mean the definition of Stepanov in Elements of Programming, basically, that there's the concept of equality and that objects which are copies of each other compare equal.

So when you have a Regular Type T, and the equality relation is transitive (a == b && b == c => a == c), you can define a (non-trivial) hash function which is consistent with the definition of equality (a == b => h(a) == h(b)). Always.

But the standard doesn't include many std::hash specialisations. E.g. std::complex doesn't have one, and neither have the containers, with the notable exceptions of vector<bool> and bitset.

So I'm wondering what the design principle is here.

Or, asked differently: Are there reasons not to provide std::hash specialisations for your own types, provided they are regular and equality is transitive?

解决方案

Yes.

When a type has the following two properties I do not think you should define std::hash:

  • There is no efficient way to consistently create a quality hash that covers all the data used to describe equality.

  • There is no efficient and/or intuitive way to select a consistent subset of data to hash.

这篇关于C ++ 11:为什么有些规则类型不应该有`std :: hash`专用的原因吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆