Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must a std::ranges::filter_view object be non-const for querying its elements?

#include <ranges>
#include <iostream>
#include <string_view>

using namespace std::literals;

int main()
{
    auto fn_is_l = [](auto const c) { return c == 'l'; };

    {
        auto v = "hello"sv | std::views::filter(fn_is_l);
        std::cout << *v.begin() << std::endl; // ok
    }

    {
        auto const v = "hello"sv | std::views::filter(fn_is_l);
        std::cout << *v.begin() << std::endl; // error
    }
}

See: https://godbolt.org/z/vovvT19a5

<source>:18:30: error: passing 'const std::ranges::filter_view<
                       std::basic_string_view<char>, main()::
                       <lambda(auto:15)> >' as 'this' argument discards
                       qualifiers [-fpermissive]
   18 |         std::cout << *v.begin() << std::endl; // error
      |                       ~~~~~~~^~
In file included from <source>:1:/include/c++/11.1.0/ranges:1307:7: 
     note: in call to 'constexpr std::ranges::filter_view<_Vp, 
           _Pred>::_Iterator std::ranges::filter_view<_Vp, Pred>
           ::begin() [with _Vp = std::basic_string_view<char>; _Pred =
           main()::<lambda(auto:15)>]'
 1307 |       begin()
      |       ^~~~~

Why must a std::ranges::filter_view object be non-const for querying its elements?

like image 549
xmllmx Avatar asked May 24 '21 06:05

xmllmx


People also ask

What is begin_ in filter_view?

an object of std::optional -like type (shown here as begin_ for exposition only) that caches an iterator to the first element of base_ that satisfies the pred_. The begin_ may be present only if filter_view models forward_range . Returns whether the derived view is empty. Provided if it satisfies sized_range or forward_range.

Is filter_view potentially-throwing or non-static?

either both are potentially-throwing or else neither is potentially-throwing (i.e. noexcept(e) == noexcept(f) ). Typical implementations of filter_view hold two or three non-static data members:

What does the filter_view function model?

filter_view models the concepts bidirectional_range, forward_range, input_range, and common_range when the underlying view V models respective concepts. either both are constant subexpressions or else neither is a constant subexpression, and

What is a filter in functional programming?

In functional programming, filter is a higher-order function that processes a data structure (usually a list) in some order to produce a new data structure containing exactly those elements of the original data structure for which a given predicate returns the boolean value true.


2 Answers

In order to provide the amortized constant time complexity required by range, filter_view::begin caches the result in *this. This modifies the internal state of *this and thus cannot be done in a const member function.

like image 179
cpplearner Avatar answered Oct 26 '22 23:10

cpplearner


The time complexity requirement here comes from the description of filter_view::begin() in [range.filter.view]:

constexpr iterator begin();

Returns: {*this, ranges​::​find_­if(base_­, ref(*pred_­))}.

Remarks: In order to provide the amortized constant time complexity required by the range concept when filter_­view models forward_­range, this function caches the result within the filter_­view for use on subsequent calls.

That is to say, the implementation needs to internally cache the iterator found by ranges​::​find_if that satisfies the predicate, which allows each subsequent call to begin() to simply return the cached value in constant time, just like libstdc++ does:

template<input_range _Vp, indirect_unary_predicate<iterator_t<_Vp>> _Pred>
class filter_view : public view_interface<filter_view<_Vp, _Pred>> {
  _Vp _M_base = _Vp();
  __box<_Pred> _M_pred;
  _CachedPosition<_Vp> _M_cached_begin;

public:
  // ...
  constexpr _Iterator
  begin() {
    if (_M_cached_begin._M_has_value())
      return {this, _M_cached_begin._M_get(_M_base)};
   
    auto __it = ranges::find_if(_M_base, std::ref(*_M_pred));
    _M_cached_begin._M_set(_M_base, __it);
    return {this, std::move(__it)};
  }
};

Since it needs to set the cache value inside filter_view when calling begin() for the first time, this makes the begin() unable to be const-qualified.

It is worth noting that other range adaptors with similar time complexity requirements include drop_view, drop_while_view, split_view, reverse_view, and C++23's chunk_by_view.

Among them, drop_while_view, split_view and chunk_by_view are never const-iterable, because they do not have a const-qualified begin(), just like filter_view.

like image 38
康桓瑋 Avatar answered Oct 27 '22 01:10

康桓瑋