c++ - std::less<Pointer> 的严格总顺序

标签 c++ pointers comparison language-lawyer

这个问题来自this comment :

If you have say two vectors a and b, the total order is permitted to be &a[0], &b[0], &a[1], &b[1], &a[2], &b[2], ..., i.e., with the elements interleaved.


允许这个顺序吗?
我对标准了解不多。如果我只阅读与 std::less 直接相关的部分,这似乎是正确的。 .
我发现 Herb Sutter 的 gcpp 库也有类似的用法(link):
    //  Return whether p points into this page's storage and is allocated.
    //
    inline
    bool gpage::contains(gsl::not_null<const byte*> p) const noexcept {
        //  Use std::less<> to compare (possibly unrelated) pointers portably
        auto const cmp = std::less<>{};
        auto const ext = extent();
        return !cmp(p, ext.data()) && cmp(p, ext.data() + ext.size());
    }

最佳答案

是的,不同的数组(不是同一个完整对象的一部分)可以在排序中交错,但每个数组必须单独正确排序——这意味着全序必须与内置运算符建立的偏序一致。事实a+1指向紧跟在 *a 之后的元素与无关数组的问题无关,因为偏序正是索引和指针顺序之间的明显关系属于完整的对象。 (实际上,“立即之后”在这里是循环的,因为唯一可观察的即时性是在数组索引本身中。整数转换不需要尊重它,所以你看不到“真实地址”。)

关于c++ - std::less<Pointer> 的严格总顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69310688/

相关文章:

c++ - c++ 中没有 std::tuple 的 std::iterator 有什么原因吗?

C++ FTP 写入文件不起作用

c++ - 运算符 < 和 > 如何与指针一起使用?

不明白为什么会有这种行为

python - 比较在 python 中作为输入传递的 2 个 sha512 哈希值

excel - 使用 Excel 计算统计显着性

c++ - HP-UX 和 Solaris 10 之间的 POSIX 线程行为不同

c++ - SWIG 包装库中 __cxa_allocate_exception 期间的段错误

c++ - 制作板的拷贝

javascript - jQuery 功能到 YUI 3 的映射?