I have a pair of iterator, and I would like to use ranges::views::filter(some_predicate) on it (with the pipe operator). AFAIU I should first convert my pair of iterator into a view. I tried to use ranges::subrange(first, last) to do so, but I’m getting horrible error messages.
Note1: I’m using C++14 and range-v3 version 0.9.1 (the last version compatible with gcc-5.5). If the solution differs when using C++17/20 and/or when using C++20 std::ranges, I’m also interested to know what changed.
Note2: I find the documentation of range-v3 severely lacking, so I’m using cppreference.com. If you know a better documentation, I’m very interested.
EDIT:
In my real code, I’m wrapping a java-style legacy iterator (that has a next() method instead of operator++/operator*. I’m wrapping them in a C++-compatible wrapper. Then I tried to convert that wrapper into a view, and finally filter it. I reproduce a minimal example on godbolt. This use iterator_range as suggested, but it still doesn’t compile (see the second edit below).
#include "range/v3/all.hpp"
#include "range/v3/iterator_range.hpp"
class LegacyIteratorWrapper {
public:
using value_type = int;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
using iterator_category = std::input_iterator_tag;
// The type isn’t default-constructible, the error comes from here
LegacyIteratorWrapper() = delete;
static LegacyIteratorWrapper create();
reference operator*() const;
pointer operator->();
LegacyIteratorWrapper& operator++();
LegacyIteratorWrapper operator++(int);
friend bool operator==(const LegacyIteratorWrapper& a, const LegacyIteratorWrapper& b);
friend bool operator!=(const LegacyIteratorWrapper& a, const LegacyIteratorWrapper& b);
};
void foo()
{
LegacyIteratorWrapper begin { LegacyIteratorWrapper::create() };
LegacyIteratorWrapper end { LegacyIteratorWrapper::create() };
ranges::iterator_range<LegacyIteratorWrapper, LegacyIteratorWrapper> rng {begin, end};
auto _ = rng
| ranges::views::filter(
[&](auto _) { return true; }
)
;
}
In file included from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/reference_wrapper.hpp:24:0,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/detail/variant.hpp:33,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/iterator/common_iterator.hpp:26,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/interface.hpp:24,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/ref.hpp:25,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action/action.hpp:29,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action.hpp:17,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/all.hpp:17,
from <source>:1:
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/pipeable.hpp: In instantiation of 'constexpr auto ranges::operator|(Arg&&, Pipe) [with Arg = ranges::iterator_range<LegacyIteratorWrapper, LegacyIteratorWrapper>&; Pipe = ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_>; bool CPP_false_ = false; typename concepts::detail::identity<typename std::enable_if<(static_cast<bool>(((! is_pipeable_v<Arg>) && is_pipeable_v<Pipe>)) || CPP_false_), void>::type>::invoke<int> <anonymous> = 0]':
<source>:33:11: required from here
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/pipeable.hpp:63:53: error: no matching function for call to 'ranges::pipeable_access::impl<ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_> >::pipe(ranges::iterator_range<LegacyIteratorWrapper, LegacyIteratorWrapper>&, ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_>&)'
return pipeable_access::impl<Pipe>::pipe(static_cast<Arg &&>(arg), pipe);
^
In file included from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/range_fwd.hpp:22:0,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action/action.hpp:21,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action.hpp:17,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/all.hpp:17,
from <source>:1:
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/view.hpp:114:35: note: candidate: template<class Rng, class Vw> static constexpr auto ranges::views::view<View>::pipe(Rng&&, Vw&&, concepts::detail::enable_if_t<concepts::detail::Nil, (static_cast<bool>((viewable_range<Rng> && invocable<View&, Rng>)) || concepts::detail::CPP_false(concepts::detail::Nil{}))>) [with Rng = Rng; Vw = Vw; View = ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_]
static constexpr auto CPP_fun(pipe)(Rng && rng, Vw && v)( //
^
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/view.hpp:114:35: note: template argument deduction/substitution failed:
<source>: In function 'void foo()':
<source>:33:11: error: 'void _' has incomplete type
)
^
ASM generation compiler returned: 1
In file included from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/reference_wrapper.hpp:24:0,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/detail/variant.hpp:33,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/iterator/common_iterator.hpp:26,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/interface.hpp:24,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/ref.hpp:25,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action/action.hpp:29,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action.hpp:17,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/all.hpp:17,
from <source>:1:
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/pipeable.hpp: In instantiation of 'constexpr auto ranges::operator|(Arg&&, Pipe) [with Arg = ranges::iterator_range<LegacyIteratorWrapper, LegacyIteratorWrapper>&; Pipe = ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_>; bool CPP_false_ = false; typename concepts::detail::identity<typename std::enable_if<(static_cast<bool>(((! is_pipeable_v<Arg>) && is_pipeable_v<Pipe>)) || CPP_false_), void>::type>::invoke<int> <anonymous> = 0]':
<source>:33:11: required from here
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/functional/pipeable.hpp:63:53: error: no matching function for call to 'ranges::pipeable_access::impl<ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_> >::pipe(ranges::iterator_range<LegacyIteratorWrapper, LegacyIteratorWrapper>&, ranges::views::view<ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_>&)'
return pipeable_access::impl<Pipe>::pipe(static_cast<Arg &&>(arg), pipe);
^
In file included from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/range_fwd.hpp:22:0,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action/action.hpp:21,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/action.hpp:17,
from /opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/all.hpp:17,
from <source>:1:
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/view.hpp:114:35: note: candidate: template<class Rng, class Vw> static constexpr auto ranges::views::view<View>::pipe(Rng&&, Vw&&, concepts::detail::enable_if_t<concepts::detail::Nil, (static_cast<bool>((viewable_range<Rng> && invocable<View&, Rng>)) || concepts::detail::CPP_false(concepts::detail::Nil{}))>) [with Rng = Rng; Vw = Vw; View = ranges::make_pipeable_fn::operator()(Fun) const [with Fun = ranges::detail::bind_back_fn_<ranges::views::cpp20_filter_fn, foo()::<lambda(auto:15)> >]::_]
static constexpr auto CPP_fun(pipe)(Rng && rng, Vw && v)( //
^
/opt/compiler-explorer/libs/rangesv3/0.9.1/include/range/v3/view/view.hpp:114:35: note: template argument deduction/substitution failed:
<source>: In function 'void foo()':
<source>:33:11: error: 'void _' has incomplete type
)
^
Execution build compiler returned: 1
EDIT2 (SOLVED): I got an error because LegacyIteratorWrapper isn’t default constructible. This is required to satisfy the regular trait (emulated by range-v3), which is required by C++ iterator, and which include being default-constructible.