C++ Logo

std-proposals

Advanced search

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

From: Peter Neiss <peter.stefan.neiss_at_[hidden]>
Date: Sun, 14 Sep 2025 19:08:07 +0200
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.

Am So., 14. Sept. 2025 um 18:42 Uhr schrieb Ell via Std-Proposals <
std-proposals_at_[hidden]>:

> On 9/14/25 16:51, Peter Neiss via Std-Proposals wrote:
> > The std::set_intersection algorithm forbids overlap of the input ranges
> > with the output range, but if first1 or first2 and result would be
> > equal, the algorithm could still work(see cppreference for possible
> > implementation).
> > This would allow inplace execution without memory allocation, which is a
> > big plus.
> >
> > 26.8.7.4 set_intersection (wording)
> >
> > replace: The resulting range does not overlap with either of the
> > original ranges. <https://eel.is/c++draft/set.intersection#2.sentence-2>
> > with: result may be equal to first1 or first2, otherwise the resulting
> > range does not overlap with either of the original ranges.
> >
> >
> > PS: I am quite sure my statement is correct and I am not making a fool
> > out of myself. Still confirmation of my claim would be great.
>
> If the input ranges overlap, the output can generally only coincide with
> the first range (the source for the copy). Otherwise you can get wrong
> results with a weak comparator. It might be a better idea to have a
> separate "inplace" version, that could move matching elements instead of
> copying them, and avoid moving elements into themselves. Ditto for
> set_difference().
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2025-09-14 17:08:23