I root caused a problem in my code to mismatched projections in std::ranges::partial_sort_copy
. After reading cppreference, I couldn't think of a legitimate situation where proj1
& proj2
needed to be different.
What's the motivation for this algorithm providing two different projections?
You need it when there is a mismatch between the type of the input range, and the type of the output range. Consider the following example:
This outputs:
In this example, it would be wasteful if the destination range contained
std::string
objects, because we could just as well store the shortest three strings as aconst char*
orstd::string_view
. As a result, there are two different projections applied here.Technically you could always create a single projection that covers both cases:
However, providing two separate projections is often more concise.