Предположим, что у нас есть
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';
}
ПРИМЕЧАНИЕ: диапазон для ref необходим, потому что cppcoro::generator
не разрешает копирование (ctor для удаленного копирования)
Распечатать
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?
Только несовместимые дизайнерские решения?
%23include+ %23include+ cppcoro::generator+gen_impl(int in) {%0A++Const auto upper = in ++10;%0A+ for (%3B+in %3C+upper%3B+++in)%0A+ +Co_yield in; } cppcoro::generator>+gen() {%0A+ for (int n =+1%3B+n <+100%3B+n +=+10)%0A+ +Co_yield+gen_impl(n); } int main() %7B+%0A+//iterate inner ranges, with range-for on reference%0A+//because+Cppcoro::generator doesn!'t allow+Copy+Ctor%0A+//+print:%0A+//+1...+10%0A+//+20...+30%0A+//..%0A+//+90...+100%0A+ for (auto&&+row :+gen() ) {%0A+ for (auto n :+row)%0A+std::cout+<%3C+n <%3C+!' !';%0A+ std::cout+<%3C+!'\n!';%0A+ }%0A+ %0A+//fail to flattern?%0A+//+view:join requires+Copy+Ctor of inner range?%0A+ auto rng =+gen();%0A+ for (auto n: rng | ranges::view::join) {%0A+ std::cout+<%3C+n <%3C+!'\n!';%0A+ }; } '),l:'5',n:'0',o:'C++ source #1',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:clang_trunk,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',trim:'1'),lang:c++,libs:!((name:rangesv3,ver:trunk),(name:cppcoro,ver:trunk)),options:'-fcoroutines-ts -stdlib=libc++ -std=gnu++2a -march=skx',source:1),l:'5',n:'0',o:'x86-64+Clang+(trunk) (Editor+#1,+Compiler+#1)+C++',t:'0')),k:50,l:'4',m:62.970936490850384,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compiler:1,editor:1,wrap:'1'),l:'5',n:'0',o:'#1 with x86-64+Clang+(trunk)',t:'0')),header:(),l:'4',m:37.029063509149616,n:'0',o:'',s:0,t:'0')),k:50,l:'3',n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4 rel=noreferrer>Godbolt (Полная %23include+ %23include+ cppcoro::generator+gen_impl(int in) {%0A++Const auto upper = in ++10;%0A+ for (%3B+in %3C+upper%3B+++in)%0A+ +Co_yield in; } cppcoro::generator>+gen() {%0A+ for (int n =+1%3B+n <+100%3B+n +=+10)%0A+ +Co_yield+gen_impl(n); } int main() %7B+%0A+//iterate inner ranges, with range-for on reference%0A+//because+Cppcoro::generator doesn!'t allow+Copy+Ctor%0A+//+print:%0A+//+1...+10%0A+//+20...+30%0A+//..%0A+//+90...+100%0A+ for (auto&&+row :+gen() ) {%0A+ for (auto n :+row)%0A+std::cout+<%3C+n <%3C+!' !';%0A+ std::cout+<%3C+!'\n!';%0A+ }%0A+ %0A+//fail to flattern?%0A+//+view:join requires+Copy+Ctor of inner range?%0A+ auto rng =+gen();%0A+ for (auto n: rng | ranges::view::join) {%0A+ std::cout+<%3C+n <%3C+!'\n!';%0A+ }; } '),l:'5',n:'0',o:'C++ source #1',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:clang_trunk,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',trim:'1'),lang:c++,libs:!((name:rangesv3,ver:trunk),(name:cppcoro,ver:trunk)),options:'-fcoroutines-ts -stdlib=libc++ -std=gnu++2a -march=skx',source:1),l:'5',n:'0',o:'x86-64+Clang+(trunk) (Editor+#1,+Compiler+#1)+C++',t:'0')),k:50,l:'4',m:62.970936490850384,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i:(compiler:1,editor:1,wrap:'1'),l:'5',n:'0',o:'#1 with x86-64+Clang+(trunk)',t:'0')),header:(),l:'4',m:37.029063509149616,n:'0',o:'',s:0,t:'0')),k:50,l:'3',n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4 rel=noreferrer>версия)