假设我们有
cppcoro::generator<int> gen_impl(int in) {
const auto upper = in + 10;
for (; in < upper; ++in)
co_yield in;
}
cppcoro::generator<cppcoro::generator<int>> gen() {
for (int n = 1; n < 100; n += 10)
co_yield gen_impl(n);
}
所以我们可以很好地迭代内部范围
for (auto&& row : gen() ) {
for (auto n : row)
std::cout << n << ' ';
std::cout << '\n';
}
注意:因为cppcoro::generator
不允许复制(删除的副本ctor),因此必须在ref上进行范围转换
打印
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
但是当我们尝试使用view :: join来“扁平化”时
auto rng = gen();
for (auto n : rng | ranges::view::join) {
std::cout << n << '\n';
};
似乎view :: join需要 可复制 内部范围?
In file included from <source>:3:
In file included from /opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/view.hpp:38:
In file included from /opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/view/for_each.hpp:23:
/opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/view/join.hpp:320:50: error: call to deleted constructor of 'cppcoro::generator<cppcoro::generator<int> >'
return join_view<all_t<Rng>>{all(static_cast<Rng&&>(rng))};
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/view/view.hpp:112:21: note: in instantiation of function template specialization 'ranges::v3::view::join_fn::operator()<cppcoro::generator<cppcoro::generator<int> > &, false, nullptr>' requested here
v.view_(static_cast<Rng&&>(rng))
^
/opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/utility/functional.hpp:731:42: note: in instantiation of function template specialization 'ranges::v3::view::view<ranges::v3::view::join_fn>::pipe<cppcoro::generator<cppcoro::generator<int> > &, ranges::v3::view::view<ranges::v3::view::join_fn> &, false, nullptr>' requested here
pipeable_access::impl<Pipe>::pipe(static_cast<Arg&&>(arg), pipe)
^
<source>:35:21: note: in instantiation of function template specialization 'ranges::v3::operator|<cppcoro::generator<cppcoro::generator<int> > &, ranges::v3::view::view<ranges::v3::view::join_fn>, false, nullptr>' requested here
for (auto n : rng | ranges::view::join) {
^
/opt/compiler-explorer/libs/cppcoro/include/cppcoro/generator.hpp:174:3: note: 'generator' has been explicitly marked deleted here
generator(const generator& other) = delete;
^
/opt/compiler-explorer/libs/rangesv3/trunk/include/range/v3/view/join.hpp:76:36: note: passing argument to parameter 'rng' here
explicit join_view(Rng rng)
^
什么使它没有被编译?
range-v3或cppcoro是否存在任何错误?
只有不兼容的设计决策?
godbolt(完整)
答案 0 :(得分:10)
在range-v3中,仅移动视图是可以的。实施得太晚了,可能仍然有错误,但这不是这里发生的事情。
第一个问题是您要在此处改编cppcoro::generator
类型的左值:
auto rng = gen();
for (auto n : rng | ranges::view::join) {
由于生成器是视图,因此join
视图将要复制它。因为不能复制,所以不能。
您可以通过将发生器移入以下位置来解决此问题:
auto rng = gen();
for (auto n : std::move(rng) | ranges::view::join) {
然后,您遇到下一个问题,即generator<generator<int>>
的引用类型为const generator<int>&
,并且您再次遇到相同的问题:join
想要保存一个副本内部生成器在对其进行迭代时,但无法进行复制。
解决方法有点难看:更改生成器以返回非常量左值引用:
cppcoro::generator<cppcoro::generator<int>&> gen() {
for (int n = 1; n < 100; n += 10) {
auto tmp = gen_impl(n);
co_yield tmp;
}
}
,然后std::move
每个内部范围都有一个move
视图:
auto rng = gen();
for (auto n : std::move(rng) | ranges::view::move | ranges::view::join) {
std::cout << n << '\n';
}
结果编译。它是否运行取决于cppcoro如何妥善处理这种情况,即有人窃取了它安全地藏在协程的promise类型中的价值胆量。
关于未来std::view::join
的注释:
C ++ 20附带的join
视图略有不同。如果外部范围的引用类型是真实的引用(如本例所示),则它将不会尝试复制其引用的视图。这意味着在C ++ 20中,您不需要丑陋的view::move
hack。
但是,C ++ 20 View
概念当前需要可复制性,因此该解决方案仍然无法使用。我们有一个待办事项可以在C ++ 20发行之前放松这一点,但是并没有说明委员会会如何看待这个想法。