C++20 范围 - 如何将组合视图分配给变量?
C++20 ranges - How to assign composed views to a variable?
有没有办法使它与 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) {
...
}
我收到这个难看的错误:
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
我尝试用谷歌搜索一些 RangeAdaptorClosure 的东西,但没有多大意义
不,这不是可行的代码。 C++ 是一种静态类型语言,您有条件地构建的每个视图都是不同的类型。 C++ 不允许您更改现有对象的类型(至少,不能那样)。
因此,这些运行时条件的事情不容易完成。您的情况很简单,因为您只使用 filter
s,并且您可以完全控制过滤器本身。因此,您可以将条件逻辑放入过滤器中。
但是任何比这更复杂的东西,或者不受 filter
s 约束的东西,都行不通。
有一些方法可以在编译时条件下完成,但这些方法在元编程方面往往会变得非常难看。
有没有办法使它与 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) {
...
}
我收到这个难看的错误:
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
我尝试用谷歌搜索一些 RangeAdaptorClosure 的东西,但没有多大意义
不,这不是可行的代码。 C++ 是一种静态类型语言,您有条件地构建的每个视图都是不同的类型。 C++ 不允许您更改现有对象的类型(至少,不能那样)。
因此,这些运行时条件的事情不容易完成。您的情况很简单,因为您只使用 filter
s,并且您可以完全控制过滤器本身。因此,您可以将条件逻辑放入过滤器中。
但是任何比这更复杂的东西,或者不受 filter
s 约束的东西,都行不通。
有一些方法可以在编译时条件下完成,但这些方法在元编程方面往往会变得非常难看。