[英]Why is null std::optional considered less than any value, not more
Is there any math sense in having null std::optional being less than any keeping value? 将null std :: optional小于任何保持值是否有任何数学意义? Or is it made only for consistency and there would have been no any difference if it was "more" instead of "less"?
或者只是为了保持一致性而且如果它是“更多”而不是“更少”则没有任何差别?
Directly from the proposal (emphasis mine): 直接来自提案 (强调我的):
A number of ways of including the disengaged state in comparisons have been suggested.
已经提出了许多将脱离状态包括在比较中的方法。 The ones proposed, have been crafted such that the axioms of equivalence and strict weak ordering are preserved : disengaged
optional<T>
is simply treated as an additional and unique value ofT
equal only to itself;提出的那些,已被制作为使得等价和严格的弱序的公理被保留 :脱开
optional<T>
被简单地视为一个额外的和独特的价值T
等于不仅对本身; this value is always compared as less than any value ofT
.该值始终被比较为小于
T
任何值。
Further on it says 进一步说它
Value
nullopt
could have been as well considered greater than any value ofT
.值
nullopt
可以被认为大于任何T
值。 The choice is to a great degree arbitrary.选择在很大程度上是任意的。 We choose to stick to what
boost::optional
does.我们选择坚持
boost::optional
功能。
Taking a look at boost::optional
's take on this, we get from its documentation : 看一下
boost::optional
对此的看法,我们从它的文档中得到:
In a similar manner, type
optional<T>
isLessThanComparable
wheneverT
isLessThanComparable
.以类似的方式,类型
optional<T>
是LessThanComparable
每当T
是LessThanComparable
。 The optional object containing no value is compared less than any value ofT
.不包含任何值的可选对象的比较小于任何
T
值。 To illustrate this, if the default ordering ofsize_t
is{0, 1, 2, ...}
, the default ordering ofoptional<size_t>
is{boost::none, 0, 1, 2, ...}
.为了说明这一点,如果
size_t
的默认顺序是{0, 1, 2, ...}
,则optional<size_t>
的默认顺序是{boost::none, 0, 1, 2, ...}
。 This order does not have a practical interpretation.这个命令没有实际的解释。 The goal is to have any semantically correct default ordering in order for
optional<T>
to be usable in ordered associative containers (whereverT
is usable).目标是具有任何语义正确的默认排序,以便
optional<T>
可用于有序关联容器(T
可用的任何地方)。
So no, there is no "maths sense" to this all except for "nothing is less than something". 所以不,除了“没有什么比某些东西更少”之外,对这一切都没有“数学意义”。 It's an arbitrary choice of no practical consequence only to ensure the type can be used in ordered containers without much hassle.
只有确保类型可以在有序容器中使用而没有太多麻烦,这是任意选择,没有实际后果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.