C++ 如何将一对迭代器转换为视图?

C++ 如何将一对迭代器转换为视图?,c++,range-v3,C++,Range V3,我有一对迭代器,我想在上面使用ranges::views::filter(some_谓词)(使用管道操作符)。首先,我应该将迭代器对转换为视图。我曾尝试使用ranges::subrange(first,last)来执行此操作,但收到了可怕的错误消息 注1:我使用的是C++14和range-v3版本0.9.1(最新版本与gcc-5.5兼容)。如果使用C++17/20和/或使用C++20 std::ranges时解决方案不同,我还想知道发生了什么变化 注2:我发现range-v3严重不足,所以我使用

我有一对迭代器,我想在上面使用
ranges::views::filter(some_谓词)
(使用管道操作符)。首先,我应该将迭代器对转换为视图。我曾尝试使用
ranges::subrange(first,last)
来执行此操作,但收到了可怕的错误消息

注1:我使用的是C++14和range-v3版本0.9.1(最新版本与gcc-5.5兼容)。如果使用C++17/20和/或使用C++20 std::ranges时解决方案不同,我还想知道发生了什么变化

注2:我发现range-v3严重不足,所以我使用了。如果你知道更好的文档,我很感兴趣


编辑:

在我的真实代码中,我包装了一个java风格的遗留迭代器(它有一个
next()
方法,而不是
运算符+++
/
运算符*
。我将它们包装在一个与C++兼容的包装器中。然后我尝试将该包装器转换为一个视图,并最终对其进行筛选。我在上复制了一个最小的示例。按照建议使用
迭代器范围
,但它仍然无法编译(请参阅下面的第二次编辑)

#包括“range/v3/all.hpp”
#包括“range/v3/iterator_range.hpp”
类LegacyIteratorWrapper{
公众:
使用value_type=int;
使用差分类型=std::ptrdiff\t;
使用指针=值_类型*;
使用引用=值\类型&;
使用迭代器_category=std::input_iterator_标记;
//该类型不是默认可构造的,错误来自这里
LegacyIteratorWrapper()=删除;
静态LegacyIteratorWrapper create();
参考运算符*()常数;
指针运算符->();
LegacyIteratorWrapper&运算符++();
LegacyIteratorWrapper运算符++(int);
friend bool运算符==(常量LegacyIteratorWrapper&a,常量LegacyIteratorWrapper&b);
friend bool运算符!=(常量LegacyIteratorWrapper&a,常量LegacyIteratorWrapper&b);
};
void foo()
{
LegacyIteratorWrapper begin{LegacyIteratorWrapper::create()};
LegacyIteratorWrapper end{LegacyIteratorWrapper::create()};
范围::迭代器_range rng{begin,end};
自动
|范围::视图::过滤器(
[&](自动{return true;}
)
;
}
在/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/functional/reference_wrapper.hpp:24:0中包含的文件中,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/detail/variant.hpp:33,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/iterator/common_iterator.hpp:26,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/view/interface.hpp:24,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/view/ref.hpp:25,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/action/action.hpp:29,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/action.hpp:17,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/all.hpp:17,
发件人:1:
/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/functional/pipeable.hpp:在“constexpr auto ranges::operator |(Arg&&,Pipe)”的实例化中[with Arg=ranges::iterator_range&;Pipe=ranges::views::view;bool CPP_false=false;typename concepts::detail::identity::invoke=0]:
:33:11:从这里开始需要
/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/functional/pipeable.hpp:63:53:错误:调用“ranges::pipeable\u access::impl::pipe(ranges::iterator\u range&,ranges::views::view&)”时没有匹配的函数
返回管道可访问::impl::管道(静态管道转换(arg),管道);
^
在/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/range_fwd.hpp:22:0中包含的文件中,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/action/action.hpp:21,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/action.hpp:17,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/all.hpp:17,
发件人:1:
/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/view/view.hpp:114:35:注:候选:模板静态constexpr自动范围::视图::视图::管道(Rng&&,Vw&&,concepts::detail::enable_if________t)[带Rng=Rng;Vw=Vw;view=ranges ranges make_pipeable__fn::operator()(Fun)const[带Fun=ranges ranges detail::detail::bind_-back
静态constexpr自动CPP_fun(管道)(Rng和Rng、Vw和VV)(//
^
/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/view/view.hpp:114:35:注意:模板参数推断/替换失败:
:在函数“void foo()”中:
:33:11:错误:“void”的类型不完整
)
^
ASM生成编译器返回:1
在/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/functional/reference_wrapper.hpp:24:0中包含的文件中,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/detail/variant.hpp:33,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/iterator/common_iterator.hpp:26,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/view/interface.hpp:24,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/view/ref.hpp:25,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/action/action.hpp:29,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/action.hpp:17,
从/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/all.hpp:17,
发件人:1:
/opt/compiler explorer/libs/rangesv3/0.9.1/include/range/v3/functional/pipeable.hpp:在“constexpr auto ranges::operator |(Arg&,Pipe)”的实例化中[with Arg=ranges::i]
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
auto my_range = std::ranges::subrange{begi,endi};