强排序和弱排序的实际含义 [英] Practical meaning of strong_ordering and weak_ordering

查看:605
本文介绍了强排序和弱排序的实际含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关C ++ 20的

I've been reading a bit about C++20's consistent comparison (i.e. operator<=>) but couldn't understand what's the practical difference between strong_ordering and weak_ordering (same goes for the _equality version for this manner).
Other than being very descriptive about the substitutability of the type, does it actually affect the generated code? Does it add any constraints for how one could use the type?
Would love to see a real-life example that demonstrates this.

推荐答案

它会增加使用类型的约束吗?

Does it add any constraints for how one could use the type?

一个非常重要的约束(原始文档没有打算)是 P0732 指示类类型可以用作非类型模板参数. weak_ordering在这种情况下还不够,因为模板等效性是如何工作的.

One very significant constraint (which wasn't intended by the original paper) was the adoption of the significance of strong_ordering by P0732 as an indicator that a class type can be used as a non-type template parameter. weak_ordering isn't sufficient for this case due to how template equivalence has to work.

通常,某些算法可能只需要weak_ordering,而另一些算法则需要strong_ordering,因此能够注释该类型可能意味着编译错误(提供的排序不充分),而不是仅满足以下条件:算法在运行时的要求,因此只是未定义的行为.但是我所知道的标准库和Ranges TS中的所有算法都只需要weak_ordering.我不知道一个需要strong_ordering放在我头顶上的东西.

Generally, it's possible that some algorithms simply require weak_ordering but other algorithms require strong_ordering, so being able to annotate that on the type might mean a compile error (insufficiently strong ordering provided) instead of simply failing to meet the algorithm's requirements at runtime and hence just being undefined behavior. But all the algorithms in the standard library and the Ranges TS that I know of simply require weak_ordering. I do not know of one that requires strong_ordering off the top of my head.

它实际上会影响生成的代码吗?

Does it actually affect the generated code?

除了需要strong_ordering的情况以外,或者算法根据比较类别明确选择不同的行为,否.

Outside of the cases where strong_ordering is required, or an algorithm explicitly chooses different behavior based on the comparison category, no.

这篇关于强排序和弱排序的实际含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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