问题描述
概念 equality_comparable_with< T,U>
旨在声明可以将类型为 T
和 U
的对象彼此比较,如果相等,则具有预期的含义.很好.
The concept equality_comparable_with<T, U>
is intended to declare that objects of type T
and U
can be compared equal to each other, and if they are, then this has the expected meaning. That's fine.
但是,该概念还需要存在 common_reference_t< T& U&
. common_reference
及其附带功能的主要推动力似乎是启用代理迭代器,使其具有表示此类迭代器的 reference
和 value_type
之间的关系的地方.
However, this concept also requires common_reference_t<T&, U&>
to exist. The primary impetus for common_reference
and its attendant functionality seems to be to enable proxy iterators, to have a place to represent the relationship between reference
and value_type
for such iterators.
那太好了,但是...与测试一个 T
和一个 U
是否可以彼此相等有什么关系?为什么标准要求 T
和 U
具有共同的引用关系,只是为了让您比较它们是否相等?
That's great, but... what does that have to do with testing if a T
and a U
can be compared equal to one another? Why does the standard require that T
and U
have a common reference relationship just to allow you to compare them equal?
这会产生奇怪的情况,在这种情况下,很难拥有两种在逻辑上不具有合理可比性的公共参照关系的类型.例如, vector< int>
和 pmr :: vector< int>
在逻辑上应该是可比的.但这不是因为两个其他不相关的类型之间没有合理的公共引用.
This creates oddball situations where it's very difficult to have two types which don't reasonably have a common-reference relationship that are logically comparable. For example, vector<int>
and pmr::vector<int>
logically ought to be comparable. But they can't be because there's no reasonable common-reference between the two otherwise unrelated types.
推荐答案
这可以追溯到 Palo Alto报告,§3.3和D.2.
This goes all the way back to the Palo Alto report, §3.3 and D.2.
为使交叉类型概念在数学上合理,您需要定义交叉类型比较的含义.对于 equality_comparable_with
, t == u
通常表示 t
和 u
相等,但这甚至意味着什么两个不同类型的值相等?该设计表示,交叉类型相等性是通过将它们映射到普通(引用)类型来定义的(必须进行此转换才能保留值).
For the cross-type concepts to be mathematically sound, you need to define what the cross-type comparison means. For equality_comparable_with
, t == u
generally means that t
and u
are equal, but what does it even mean for two values of different types to be equal? The design says that cross-type equality is defined by mapping them to the common (reference) type (this conversion is required to preserve the value).
如果不希望使用 equality_comparable_with
的强大公理,则该标准使用仅博览会的概念 weakly-equality-comparable-with
,该概念不需要通用参考.然而,这是语义可憎".(用Casey Carter的话来说),因此仅出于说明目的:它允许具有 t == u
和 t2 == u
但具有 t!=t2
(这实际上是哨兵所必需的).
Where the strong axioms of equality_comparable_with
is not desirable, the standard uses the exposition-only concept weakly-equality-comparable-with
, and that concept doesn't require a common reference. It is, however, a "semantic abomination" (in the words of Casey Carter) and is exposition-only for that reason: it allows having t == u
and t2 == u
but t != t2
(this is actually necessary for sentinels).
这篇关于`equality_comparable_with`是否需要要求`common_reference`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!