【问题标题】:How to convert a pair of iterator into a view?如何将一对迭代器转换为视图?
【发布时间】:2021-04-08 16:11:24
【问题描述】:

我有一对迭代器,我想在上面使用ranges::views::filter(some_predicate)(使用管道运算符)。 AFAIU 我应该首先将我的一对迭代器转换成一个视图。我尝试使用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的documentation严重不足,所以我使用cppreference.com。如果你知道更好的文档,我很感兴趣。


编辑:

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

#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(已解决):我收到一个错误,因为 LegacyIteratorWrapper 不是默认可构造的。这是满足 C++ 迭代器要求的常规特征(由 range-v3 模拟)所必需的,其中包括可默认构造。

【问题讨论】:

  • 请在此处发布您的示例,而不是在 GodBolt 上,并提供(部分)错误消息。另外,请更清楚您是否对 C++14 解决方案(或任何其他特定版本的语言标准)感兴趣。
  • 完成。我对带有 range-v3 0.9.1 的 C++14 解决方案感兴趣,因为我现在需要它,但我也对更高版本感兴趣,以供我大致了解。

标签: c++ range-v3


【解决方案1】:

我有一对迭代器,我想在上面使用ranges::views::filter(some_predicate)(使用管道运算符)。 AFAIU 我应该首先将我的一对迭代器转换成一个视图。我尝试使用 ranges::subrange(first, last) 这样做,但我收到了可怕的错误消息。

由于您还没有真正提供代码来支持您的收到可怕的错误消息,因此这里有一个代码,它在一个向量上定义了两个迭代器,利用了ranges::subrange,并过滤了结果范围.

#include <iostream>
#include <range/v3/view/filter.hpp>
#include <range/v3/view/subrange.hpp>
#include <vector>

namespace r = ranges;
namespace rv = ranges::views;

int main() {
    std::vector<int> v{1,2,3,4,5};
    auto even = [](auto x){ return x % 2 == 0; };
    auto i1 = v.begin() + 1;
    auto ie = v.end() - 1;
    auto w = r::subrange(i1, ie) | rv::filter(even);
    for (auto i : w) {
        std::cout << i << std::endl;
    }
}

如果你澄清什么你的示例代码是什么以及你得到了什么错误,我或其他人可以给你一个更好的答案。

【讨论】:

  • 我在 OP 上添加了 minimal example。你是对的,我应该从一开始就添加它。
  • @Robin,我现在没时间看。但是,我建议您尝试消除问题并使其最小化。它看起来不像现在那么小。
  • 每次我尝试削减它时,它都在编译,直到我最终理解错误,这是因为我的LegacyIteratorWrapper 不是默认可构造的。我会相应地更新 OP。
【解决方案2】:

您可以将每对相等的迭代器转换为 std::ranges::subrange

auto my_range = std::ranges::subrange{begi,endi};

【讨论】:

    【解决方案3】:

    在 range-v3 中,您可以使用 iterator_range 将迭代器包装到一个范围对象中。

    在 C++20 中,您可以使用 std::span 将这些迭代器包装到一个范围对象中

    【讨论】:

    • 我试过了(我可以创建iterator_range),但我仍然无法将它通过管道传输到filter,正如您在我添加的godbolt 链接中看到的那样进入 OP。
    • 我认为 std::span 不适用于 OP 的情况。而且你不能将两个任意迭代器变成一个跨度。
    • 这个答案应该通过使用std::ranges::subrange来改进
    猜你喜欢
    • 2021-05-26
    • 2021-09-04
    • 1970-01-01
    • 2012-07-14
    • 2020-09-13
    • 2011-09-02
    • 2011-10-03
    相关资源
    最近更新 更多