更新时间:2023-11-13 23:25:10
[alg.sorting]/2
Compare
是一个函数对象类型 (20.9).应用于Compare
类型对象的函数调用操作的返回值,当上下文转换为bool
(第 4 条)时,产生true
,如果调用的第一个参数小于第二个参数,否则为false
.Compare comp
始终用于假设排序关系的算法.假设comp
不会通过解引用的迭代器应用任何非常量函数.
[alg.sorting]/2
Compare
is a function object type (20.9). The return value of the function call operation applied to an object of typeCompare
, when contextually converted tobool
(Clause 4), yieldstrue
if the first argument of the call is less than the second, andfalse
otherwise.Compare comp
is used throughout for algorithms assuming an ordering relation. It is assumed thatcomp
will not apply any non-constant function through the dereferenced iterator.
[alg.sorting]/3 对于所有采用 Compare
的算法,有一个版本使用 operator 代替.也就是说,
comp(*i, *j) != false
默认为 *i .
[alg.sorting]/3 For all algorithms that take Compare
, there is a version that uses operator<
instead. That is, comp(*i, *j) != false
defaults to *i < *j != false
.
[res.on.functions]/1 在某些情况下(替换函数、处理函数、对用于实例化标准库模板组件的类型的操作),C++ 标准库依赖于由一个 C++ 程序.如果这些组件不满足他们的要求,标准不会对实现提出任何要求.
[res.on.functions]/1 In certain cases (replacement functions, handler functions, operations on types used to instantiate standard library template components), the C++ standard library depends on components supplied by a C++ program. If these components do not meet their requirements, the Standard places no requirements on the implementation.
强调我的.通过向标准库函数提供不满足函数要求的组件,您的程序表现出未定义的行为.
Emphasis mine. Your program exhibits undefined behavior by way of supplying to a standard library function a component that doesn't meet the function's requirements.
顺便说一下,clang 拒绝了 std::sort
和 std::stable_sort
.
By the way, clang rejects both std::sort
and std::stable_sort
.