C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Relaxing precondition on std::set_intersection algorithm

From: Ell <ell_se_at_[hidden]>
Date: Sun, 14 Sep 2025 20:29:08 +0300
On 9/14/25 20:08, Peter Neiss wrote:
> Ok. I have not thought about overlapping input ranges. Classical
> aliasing problem. Modifying the first range at a later position, through
> the second maybe makes the range unsorted. But when I try to construct
> an example, the sorting seems to make this impossible and it still works
> out correctly.
> I am not familiar with weak comparators and when they are allowed in the
> stl. Is sorting by a weak comparator enough for a range to be sorted ?
> Please give an example that breaks with overlapping input ranges. If
> this is a problem inplace_set_intersection is a good idea, otherwise i
> would prefer changing the precondition on std::set_intersection.
In the extreme case, you can consider all values equivalent:

    #include <array>
    #include <span>
    #include <algorithm>
    #include <print>

    int main () {
        std::array in = {1, 2, 3, 4};

        auto in1 = std::span (in).subspan (0, 3);
        auto in2 = std::span (in).subspan (1, 3);

        // all values are equivalent
        auto comp = [] (auto...) { return false; };

        auto test = [&] (const char* name, auto&& out) {
            std::ranges::set_intersection (
                in1, in2,
                out.begin (),
                comp
            );

            std::println ("{}: {}", name, out);
        };

        test ("out-of-place ", std::array<int, 3> {});
        test ("in-place (in1)", in1);
        test ("in-place (in2)", in2);
    }

Output:

    out-of-place : [1, 2, 3]
    in-place (in1): [1, 2, 3]
    in-place (in2): [1, 1, 1]

https://godbolt.org/z/xzKqq3nrT

Received on 2025-09-14 17:29:20