std :: type_info :: hash_code()的唯一性和“应该"的含义 [英] std::type_info::hash_code() uniqueness and the meaning of "should"

查看:239
本文介绍了std :: type_info :: hash_code()的唯一性和“应该"的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否要保证相同的std::type_info::hash_code()值表示相同的类型?

Is it meant to be guaranteed that same std::type_info::hash_code() values imply same types?

Cplusplus.com 似乎这样宣称:

对于任何两个相等的type_info对象,此函数返回相同的值,对于不同的类型,返回不同的值. [重点突出]

Cppreference 似乎相反:

返回一个未指定的值,该值对于对象是相同的,引用相同的类型. 没有其他保证,特别是,值可以在同一程序的调用之间改变. [重点突出]

Returns an unspecified value, which is identical for objects, referring to the same type. No other guarantees are given, in particular, the value can change between invocations of the same program. [Emphasis mine]

相关标准段落为:

§p18.7.1 p7-8

§ p18.7.1 p7-8

size_t hash_code()const noexcept;

size_t hash_code() const noexcept;

7返回:一个未指定的值,除了在程序的一次执行内,对于任何两个相等的type_info对象,它应返回相同的值.

7 Returns: An unspecified value, except that within a single execution of the program, it shall return the same value for any two type_info objects which compare equal.

8备注:实现为两个不相等的type_info对象返回不同的值. [重点突出]

8 Remark: an implementation should return different values for two type_info objects which do not compare equal. [Emphasis mine]

上面上下文中的"应该"是什么意思?如果第8段是必需的,那么除非运行时对程序中的所有符号名称进行某种全局唯一性以确保没有哈希冲突,否则似乎无法实现,这对于标准来说似乎是一个很大的负担.专注于实现,尤其是对于名为hash_code()的函数. (Itanium实际上需要这样做,但这显然是超出标准的额外要求.)

What's the meaning of "should" supposed to be in the context above? If paragraph 8 is meant to be a requirement, then it seems impossible to fulfill unless the runtime does some kind of global uniquing over all symbol names in a program to ensure lack of hash collision, which seems to be a pretty big burden for the standard to foist upon implementations, especially for a function called hash_code(). (Itanium actually requires this, but it's explicitly an extra requirement above the standard.)

如果"应该"不具有约束力,则该言论似乎毫无意义,并且是标准中的缺陷,因为要求实施人员尝试满足一项难以满足的要求.无论如何,依赖都没有任何价值,只会引起混乱和支离破碎.有人知道为什么会在那里吗?

If "should" is not meant to be binding, then the remark seems to be a pointless one and a defect in the standard, since asking implementations to try to fulfill a difficult requirement that cannot be relied upon anyway provides no value and only invites confusion and fragmentation. Anyone know why it's there?

编辑:缺陷"一词可能太过强烈,但至少应该澄清一个可能的混淆点,因为它显然误导了至少一个参考站点,并传递性地误导了任何依赖的站点至此.此外,如果在运行时进行全局唯一化,实际上可以满足要求(只要实现支持的类型数小于size_t的范围),并且尚不清楚该标准是否试图将其建议为理想的实施策略.

EDIT: Maybe "defect" was too strong a word, but at least it's a point of possible confusion that should be clarified, since it's apparently misled at least one reference site and transitively misled anyone relying upon it. Furthermore, it actually is possible to fulfill the requirement (as long as the number of types supported by the implementation is smaller than the range of size_t) if global uniquing is done at runtime, and it's unclear if the standard is trying to suggest this as the ideal implementation strategy or not.

推荐答案

在我看来,含义很清楚:这不是绝对要求,因为在某些情况下可能无法满足,但实现应该尝试尽可能产生唯一的值.

The meaning looks pretty clear to me: it's not an absolute requirement because it may be impossible to meet under some circumstances, but the implementation should attempt to produce unique values to the extent possible.

我会注意到散列代码通常也是如此-您尝试产生唯一的值,但这并不总是可能的.

I'd note that the same is true of hash codes in general -- you try to produce values that are unique, but it's not always possible.

该标准包含很多不可执行的信息.相当一部分(但不是全部)都是显式的Note形式,但这并不意味着Note以外的所有非规范性内容都是缺陷.

The standard contains a lot of information that's not enforceable. Quite a bit (but certainly not all) is in the form of explicit Notes, but that doesn't mean everything non-normative outside a Note is a defect.

如果有人想知道ISO关于如何编写标准的说法,他们可以使用

in case anybody wants to know what the ISO says about how standards should be written, they have a page of guidelines.

这篇关于std :: type_info :: hash_code()的唯一性和“应该"的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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