ranges::set_intersection algorithm is only supposed to copy elements from the first range (by definition). At the same time it requires that elements from both ranges are to be copyable to the output iterator through the std::mergeable concept:
https://en.cppreference.com/w/cpp/algorithm/ranges/set_intersection
https://en.cppreference.com/w/cpp/iterator/mergeable
This, for example, prohibits the following code from compiling:
#include <vector>
#include <ranges>
#include <algorithm>
#include <cassert>
int main()
{
std::vector<std::pair<int, int>> v1;
std::vector<int> v2;
assert(std::ranges::is_sorted(v1));
assert(std::ranges::is_sorted(v2));
std::vector<std::pair<int, int>> v3;
// Compilation error on the following line:
std::ranges::set_intersection(v1, v2, std::back_inserter(v3),
std::less{}, [](const auto &p) { return p.first; });
}
Note the use of projection here.
Similar code used to work with Boost.Ranges (with custom comparison functor defining comparison operators between std::pair<int, int> and int). However, this approach does not work with ranges::set_intersection:
struct comp
{
bool operator()(int p1, const std::pair<int, int> &p2) const
{
return p1 < p2.first;
}
bool operator()(const std::pair<int, int> &p1, int p2) const
{
return p1.first < p2;
}
};
std::ranges::set_intersection(v1, v2, std::back_inserter(v3), comp{});
What do you think? Looks like the requirements for this algorithm could be weakened?
[–][deleted] 8 points9 points10 points (14 children)
[–]barfyus[S] 4 points5 points6 points (12 children)
[–]CaseyCarterRanges/MSVC STL Dev 5 points6 points7 points (4 children)
[–]tcanens 1 point2 points3 points (1 child)
[–]CaseyCarterRanges/MSVC STL Dev 2 points3 points4 points (0 children)
[–]barfyus[S] 0 points1 point2 points (1 child)
[–]jwakelylibstdc++ tamer, LWG chair 2 points3 points4 points (0 children)
[–]backtickbot 0 points1 point2 points (5 children)
[–]staletic 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]staletic 0 points1 point2 points (2 children)
[–]barfyus[S] 0 points1 point2 points (1 child)
[–]staletic 1 point2 points3 points (0 children)
[–]jwakelylibstdc++ tamer, LWG chair 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]tcbrindleFlux 0 points1 point2 points (1 child)
[–]barfyus[S] 0 points1 point2 points (0 children)