三向比较运算符始终有效吗? [英] Is the three-way comparison operator always efficient?

查看:139
本文介绍了三向比较运算符始终有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Herb Sutter,在他的太空飞船"提案中运算符(第12页底部的2.2.2节)说:

Herb Sutter, in his proposal for the "spaceship" operator (section 2.2.2, bottom of page 12), says:

基于<=>的所有内容及其返回类型:此模型具有主要优势,与以前的C ++提议和其他语言的功能相比,该提议具有一些独特之处:

Basing everything on <=> and its return type: This model has major advantages, some unique to this proposal compared to previous proposals for C++ and the capabilities of other languages:

[...]

(6)效率,包括最终实现零开销抽象以进行比较:绝大多数比较始终是单次通过.对于同时支持部分排序​​和相等性的类型,唯一的例外是生成<=>=.对于<,单次通过对于实现零开销原则是至关重要的,以避免重复进行相等比较,例如struct Outer { Employeee; /*more members*/ };中使用的struct Employee { string name; /*more members*/ }; –今天的比较违反了零开销抽象,因为Outer上的operator<执行冗余的相等比较,因为它执行的if (e != that.e) return e < that.e;遍历的相等前缀 e.name两次(如果名称相等,也两次遍历Employee其他成员的相等前缀),并且通常无法对其进行优化.正如卡米斯基(Kamiński)所指出的,零开销抽象是C ++的支柱,并且首次实现以进行比较是这种基于<=>的设计的显着优势.

(6) Efficiency, including finally achieving zero-overhead abstraction for comparisons: The vast majority of comparisons are always single-pass. The only exception is generated <= and >= in the case of types that support both partial ordering and equality. For <, single-pass is essential to achieve the zero-overhead principle to avoid repeating equality comparisons, such as for struct Employee { string name; /*more members*/ }; used in struct Outer { Employeee; /*more members*/ }; – today’s comparisons violates zero-overhead abstraction because operator< on Outer performs redundant equality comparisons, because it performs if (e != that.e) return e < that.e; which traverses the equal prefix of e.name twice (and if the name is equal, traverses the equal prefixes of other members of Employee twice as well), and this cannot be optimized away in general. As Kamiński notes, zero-overhead abstraction is a pillar of C++, and achieving it for comparisons for the first time is a significant advantage of this design based on <=>.

但是他给出了这个例子(第1.4.5节,第6页):

But then he gives this example (section 1.4.5, page 6):

class PersonInFamilyTree { // ...
public:
  std::partial_ordering operator<=>(const PersonInFamilyTree& that) const {
    if (this->is_the_same_person_as ( that)) return partial_ordering::equivalent;
    if (this->is_transitive_child_of( that)) return partial_ordering::less;
    if (that. is_transitive_child_of(*this)) return partial_ordering::greater;
    return partial_ordering::unordered;
  }
  // ... other functions, but no other comparisons ...
};

是否将operator>(a,b)定义为a<=>b > 0不会导致大量开销? (尽管形式与他所讨论的不同).该代码将首先测试是否相等,然后测试less,最后测试greater,而不是仅直接测试greater.

Would define operator>(a,b) as a<=>b > 0 not lead to large overhead? (though in a different form than he discusses). That code would first test for equality, then for less, and finally for greater, rather than only and directly testing for greater.

我在这里想念东西吗?

推荐答案

是否将operator>(a,b)定义为a<=>b > 0不会导致大量开销?

Would define operator>(a,b) as a<=>b > 0 not lead to large overhead?

这会导致一些开销.但是,开销的大小是相对的-在运行比较的成本与程序的其余部分相比可以忽略不计的情况下,通过实现一个运算符而不是五个运算符来减少代码重复可能是可以接受的折衷方案.

It would lead to some overhead. The magnitude of the overhead is relative, though - in situations when costs of running comparisons are negligible in relation to the rest of the program, reducing code duplication by implementing one operator instead of five may be an acceptable trade-off.

但是,该建议并不建议删除其他比较运算符,而推荐使用<=>:如果要重载其他比较运算符,则可以随意执行:

However, the proposal does not suggest removing other comparison operators in favor of <=>: if you want to overload other comparison operators, you are free to do it:

要笼统:不要限制固有的内容.不要随意限制完整的用途.避免特殊情况和局部特征. –例如,本文支持所有七个比较运算符和运算,包括通过<=>添加三向比较.它还支持所有五个主要的比较类别,包括部分订单.

Be general: Don’t restrict what is inherent. Don’t arbitrarily restrict a complete set of uses. Avoid special cases and partial features. – For example, this paper supports all seven comparison operators and operations, including adding three-way comparison via <=>. It also supports all five major comparison categories, including partial orders.

这篇关于三向比较运算符始终有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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