range-v3 icon indicating copy to clipboard operation
range-v3 copied to clipboard

Can't use for_each with move-only range

Open JohelEGP opened this issue 5 years ago • 2 comments

https://godbolt.org/z/ZZWbk7:

#include <iterator>
#include <range/v3/algorithm/for_each.hpp>

void f()
{
    int x;
    ranges::for_each(
        std::move_iterator{&x},
        std::move_iterator{&x + 1},
        [](int&&)
        {
        });
}

The invocable<F &, iter_value_t<I> &> requirement overconstraints the simplest algorithm which is even a first class citizen. I suggest changing the indirectly_unary_invocable<F, projected<I, P>> constraint to the minimal invocable<projected<I, P>, iter_reference_t<I>>.

JohelEGP avatar Nov 12 '19 00:11 JohelEGP

This is actually how the constraints of for_each are currently specified in the C++20 draft. We'll need to do some hard thinking about whether we should loosen the constraints of the for_each algorithm and make them inconsistent with the others. We already loosened the requirements of transform this way. @CaseyCarter, thoughts?

ericniebler avatar Dec 04 '19 01:12 ericniebler

This should be solved by:

WG21 Number Title Author
P2997R0 Removing the common reference requirement from the indirectly invocable concepts Barry Revzin, Tim Song

JohelEGP avatar Oct 17 '23 16:10 JohelEGP