我在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
违反了BoundedView
或BidirectionalView
概念(或两者)。我能够迅速消除前者作为一种可能性。
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
?关于如何在不复制基础数据的情况下实现类似行为的任何建议?