Why Sortable concept requires totally ordered value type, while std::sort only requires “less than” comparable?

99封情书 提交于 2019-12-04 23:55:19

The Concepts TS does not conceptualize the standard library. It was merely an example; nothing more.

The Ranges TS version of sort requires Sortable, which defaults its comparison class to std::less<>. However, it would seem that std::less<>::operator() imposes the TotallyOrdered requirement on the types of its parameters. So that's where it comes from. There's a note about this in P0021R0 (PDF):

[Editor’s note: Remove table [lessthancomparable] in [utility.arg.requirements]. Replace uses of LessThanComparable with TotallyOrdered (acknowledging that this is a breaking change that makes type requirements stricter). Replace references to [lessthancomparable] with references to [concepts.lib.compare.totallyordered]]

Emphasis added. The general issues surrounding this appear to be on-hold, pending other language features (like implicit creation of all other operators based solely on operator< or somesuch).

You could simply use a (sane) version of a comparison function. Or you could just use the std::sort iterator version, which won't use concepts of any kind.


It should also be noted that, with the introduction of the "spaceship operator" in C++20 (the earliest we could see the Ranges TS integrated into the standard), this whole discussion effectively becomes moot. A simple auto operator<=>(const MyType &) = default; declaration in the class, and suddenly your type is totally ordered.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!