NoSenseEtAl
NoSenseEtAl

Reputation: 30028

Why does std::filter_view not have a way to eagerly evaluate it and convert it to view whose begin in const?

filter_view does not have a const begin.

But I wonder why isn't there a member or free function on it that would return some_filter_view type that is that is same as filter_view except it has a const begin(). Obviously this method would be O(n), but it would be fine since it is explicit in code.

Is it just that it is too much work to standardize/ too big of a compile slowdown, or are there other reasons?

Basic example of what I would want:

void fn(){
    std::array<int,5> arr{1,2,3,4,5};
    const auto filtered = arr | std::views::filter([](auto){return true;});
    filtered.begin(); // does not compile
    // --would be nice if we could do 
    // const auto filtered = arr | std::views::filter([](auto){return true;}) | eval_begin;
    // --now filtered.begin() can be called
}

Upvotes: 3

Views: 551

Answers (1)

T.C.
T.C.

Reputation: 137315

This is basically "why can't we have a filter_view that caches on construction".

The answer is that it will make copies O(N) since the cached iterator are iterators into the original view, so your copied view have to find begin() again for its copy.

If you have a (non-const) filter_view already, then it's trivial to make a const-iterable view out of it with subrange. Obvious this requires the filter_view to be kept alive while the subrange is.

Upvotes: 3

Related Questions