2021年3月25日星期四

C++20 ranges - How to assign composed views to a variable?

Is there a way to make this work with C++20?

auto view = std::views::all;    if (condition1) {      view = view | std::views::filter([](int i) {          return i%5 == 0;      });  }    if (condition2) {      view = view | std::views::filter([](int i) {          return i%10 == 0;      });  }    /* Do something with the view */  for (int i : list | view) {     ...  }  

I am getting this ugly error:

test.cpp:13:10: error: no match for 'operator=' (operand types are 'std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::<lambda(_Range&&) >' and 'std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::__adaptor::operator|<std::ranges::views::<lambda(_Range&&) >::<lambda(_Up&&)> >') 13 | }); | ^ In file included from test.cpp:2: /usr/include/c++/10/ranges:1155:14: note: candidate: 'constexpr std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::<lambda(_Range&&) >& std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::<lambda(_Range&&) >::operator=(const std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::<lambda(_Range&&) >&)' 1155 | struct _RangeAdaptorClosure : public _RangeAdaptor<_Callable> | ^~~~~~~~~~~~~~~~~~~~ /usr/include/c++/10/ranges:1155:14: note: no known conversion for argument 1 from 'std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::__adaptor::operator|<std::ranges::views::<lambda(_Range&&) >::<lambda(_Up&&)> >' to 'const std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::<lambda(_Range&&) >&' /usr/include/c++/10/ranges:1155:14: note: candidate: 'constexpr std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::<lambda(_Range&&) >& std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::<lambda(_Range&&) >::operator=(std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::<lambda(_Range&&) >&&)' /usr/include/c++/10/ranges:1155:14: note: no known conversion for argument 1 from 'std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::__adaptor::operator|<std::ranges::views::<lambda(_Range&&) >::<lambda(_Up&&)> >' to 'std::ranges::views::__adaptor::_RangeAdaptorClosurestd::ranges::views::<lambda(_Range&&) >&&' make: *** [Makefile:3: build] Error 1

I tried googling some of that RangeAdaptorClosure stuff but it's not making much sense

https://stackoverflow.com/questions/66810079/c20-ranges-how-to-assign-composed-views-to-a-variable March 26, 2021 at 10:03AM

没有评论:

发表评论