2

我在view::join使用(惊人的) Ranges-v3 库的函数对象时遇到了一些困难。我的客户端代码依赖于返回方法(并且非常感谢随机访问迭代器)的存在,以获取范围集合的聚合视图。

在查看了相关文档之后,似乎一种back方法与 join_view 类模板的实例化兼容,但我一直无法将其实例化。

#include <iostream>
#include <vector>
#include <range/v3/all.hpp>

struct Foo{
    std::vector<int> i = {1,2,3,4};
    const std::vector<int>& data() const { return this->i; }
};

int main(){
    std::vector< Foo > foos = { Foo(), Foo(), Foo() };

    auto data = []( auto&& foo ){ return foo.data() | ranges::view::all; };
    auto flat = foos | ranges::view::transform(data) | ranges::view::join;
    std::cout << flat.back() << std::endl; // compiler error
}

编译器错误消息的相关位是:

main.cpp:17:28: error: no matching function for call to 'ranges::v3::join_view<ranges::v3::transform_view<ranges::v3::range<__gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> >, __gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> > >, main()::<lambda(auto:1&&)> >, void>::back()'

 std::cout << flat.back() << std::endl; // compiler error

/usr/local/include/range/v3/range_interface.hpp:116:34: note: candidate: template<class D, int _concept_requires_115, typename std::enable_if<((_concept_requires_115 == 43) || ((std::is_same<D, ranges::v3::join_view<ranges::v3::transform_view<ranges::v3::range<__gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> >, __gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> > >, main()::<lambda(auto:1&&)> >, void> >() && ranges::v3::concepts::models<ranges::v3::concepts::BoundedView, T>()) && ranges::v3::concepts::models<ranges::v3::concepts::BidirectionalView, T>())), int>::type <anonymous> > ranges::v3::range_reference_t<D> ranges::v3::range_interface<Derived, Inf>::back() [with D = D; int _concept_requires_115 = _concept_requires_115; typename std::enable_if<((_concept_requires_115 == 43) || ((std::is_same<D, Derived>() && ranges::v3::concepts::models<ranges::v3::concepts::BoundedView, D>()) && ranges::v3::concepts::models<ranges::v3::concepts::BidirectionalView, D>())), int>::type <anonymous> = <enumerator>; Derived = ranges::v3::join_view<ranges::v3::transform_view<ranges::v3::range<__gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> >, __gnu_cxx::__normal_iterator<Foo*, std::vector<Foo> > >, main()::<lambda(auto:1&&)> >, void>; bool Inf = false]

         range_reference_t<D> back()

/usr/local/include/range/v3/range_interface.hpp:115:17: error: no type named 'type' in 'struct std::enable_if<false, int>'

                 CONCEPT_REQUIRES_(Same<D, Derived>() && BoundedView<D>() && BidirectionalView<D>())>

第一个要求似乎是强制正确使用 CRTP,这是满足的。所以join_view违反了BoundedViewBidirectionalView概念(或两者)。我能够迅速消除前者作为一种可能性。

auto flat = foos 
  | ranges::view::transform(data) 
  | ranges::view::join 
  | ranges::view::bounded;
std::cout << flat.back() << std::endl; // compiler error

在这种情况下,flat满足BoundedView概念,但错误消息保持不变。

为了验证BidirectionalView,我尝试检查了 的迭代器join_view,但遇到(可疑的)是一个错误。

auto it = flat.begin();
std::cout << *it << std::endl; // correct
++it; std::cout << *it << std::endl; // correct
--it; std::cout << *it << std::endl; // doesn't actually decrement
auto other = --it;
std::cout << *it << ' ' << *other << std::endl; // also doesn't decrement

我已经编写了一个实时版本以便于检查。

有没有人有任何运气实例化双向join_view?关于如何在不复制基础数据的情况下实现类似行为的任何建议?

4

1 回答 1

2

Range-v3 的join观点满足InputRange,但不满足Forward或更强。它与如何完成连接有关。在迭代内部范围时,需要将范围存储在某个地方。某处位于join_view对象的成员中。换句话说,join_view在你迭代它时发生了变异。出于这个原因,它不能对任何强于 的范围类别进行建模Input

于 2017-03-15T21:34:23.323 回答