`std :: less'如何工作? [英] How does `std::less` work?

查看:842
本文介绍了`std :: less'如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指针关系运算子未定义总订单( C ++ 11标准的第5.9节):



Pointer relational operators do not define a total order (§ 5.9 of the C++11 standard):


如果两个指针 p q 指向不是同一对象或同一数组或不同函数的元素的成员的不同对象,或者如果它们中只有一个为空,则 p p> q p <= q p> = q 未指定。

If two pointers p and q of the same type point to different objects that are not members of the same object or elements of the same array or to different functions, or if only one of them is null, the results of p<q, p>q, p<=q, and p>=q are unspecified.

std :: less 文档说:


std :: less 对于任何指针类型都会产生一个总次序,即使内置的运算符<

The partial specialization of std::less for any pointer type yields a total order, even if the built-in operator< does not.

如何从部分订单收取此总订单?

How does it yield this total order from a partial order?

我无法通过查看 / usr / include / c ++ / 4.9 / bits / stl_function.h 来回答此问题 struct less 定义:

I am unable to answer to this question by looking at /usr/include/c++/4.9/bits/stl_function.h for struct less definitions:

  template<typename _Tp = void>
    struct less;

  template<typename _Tp>
    struct less : public binary_function<_Tp, _Tp, bool>
    {
      bool
      operator()(const _Tp& __x, const _Tp& __y) const
      { return __x < __y; }
    };

  template<>
    struct less<void>
    {
      template <typename _Tp, typename _Up>
        auto
        operator()(_Tp&& __t, _Up&& __u) const
        noexcept(noexcept(std::forward<_Tp>(__t) < std::forward<_Up>(__u)))
        -> decltype(std::forward<_Tp>(__t) < std::forward<_Up>(__u))
        { return std::forward<_Tp>(__t) < std::forward<_Up>(__u); }

      typedef __is_transparent is_transparent;
    };


推荐答案


这个总订单从部分订单?

How does it yield this total order from a partial order?

标准很少说如何相反,它说需要什么。这是正确的。该标准要求 std :: less 在§20.9.6/ 14中提供总订单:

The standard rarely says how something should be accomplished. Instead, it says what is required. And this is exactly the case. The standard is requiring std::less to provide a total order, in §20.9.6/14:


对于模板greater,less,greater_equal和less_equal,任何指针类型的特殊化都会产生一个总阶数,即使内置的运算符<>,< =,> =没有。

For templates greater, less, greater_equal, and less_equal, the specializations for any pointer type yield a total order, even if the built-in operators <, >, <=, >= do not.

操作符< 在这方面的行为是未指定根据§5.9/ 4(您在您的问题中的报价)。

while operator<'s behaviour in this regard is unspecified according to §5.9/4 (the quote you have in your question).

未定义的行为在§1.3.25中定义为:

Unspecified behaviour is defined in §1.3.25 to mean:

$ [...]

在你的具体实现中,运算符< 已经提供了一个总顺序(可能是因为你的指针类型被实现为32位或64位地址,这可以很容易解释为类似于无符号整数,产生一个总排序),因此 std :: less 只是将其参数转发给该运算符。

In your specific implementation, operator< already provides a total order (probably because your pointer type is implemented as a 32 bits or 64 bits address, which can be easily interpreted as something similar to an unsigned integer, yielding a total order), therefore std::less simply forwards its arguments to that operator.

这篇关于`std :: less'如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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