简体   繁体   English

为什么null std :: optional被认为小于任何值,而不是更多

[英]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 of T equal only to itself; 提出的那些,已被制作为使得等价和严格的弱序的公理被保留 :脱开optional<T>被简单地视为一个额外的和独特的价值T等于不仅对本身; this value is always compared as less than any value of T . 该值始终被比较为小于T任何值。

Further on it says 进一步说它

Value nullopt could have been as well considered greater than any value of T . 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> is LessThanComparable whenever T is LessThanComparable . 以类似的方式,类型optional<T>LessThanComparable每当TLessThanComparable The optional object containing no value is compared less than any value of T . 不包含任何值的可选对象的比较小于任何T值。 To illustrate this, if the default ordering of size_t is {0, 1, 2, ...} , the default ordering of optional<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 (wherever T 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM