gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:37:56 26 4
gpt4 key购买 nike

让 null std::optional 小于任何保持值是否有任何数学意义?或者它只是为了保持一致性而制作的,如果它是“更多”而不是“更少”就不会有任何区别?

最佳答案

直接来自 the proposal (强调我的):

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; this value is always compared as less than any value of T.

进一步说

Value nullopt could have been as well considered greater than any value of T. The choice is to a great degree arbitrary. We choose to stick to what boost::optional does.


看看boost::optional对此,我们从 its documentation 得到:

In a similar manner, type optional<T> is LessThanComparable whenever T is LessThanComparable. The optional object containing no value is compared less than any value of 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, ...}. 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).

所以不,除了“没有什么比某物少”之外,这一切没有“数学意义”。这是一个没有实际后果的任意选择,只是为了确保该类型可以在有序的容器中使用而没有太多麻烦。

关于c++ - 为什么 null std::optional 被认为小于任何值,而不是更多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57304381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com