gpt4 book ai didi

c++ - 是否可以将 'range-v3' include(s) 与 c++ 'ranges' include 混合使用?

转载 作者:行者123 更新时间:2023-12-04 17:11:04 25 4
gpt4 key购买 nike

我可以访问 GCC 10 并使用 -std=c++20 进行编译,并且需要诸如 generate cache1concat 之类的 View 等可能要等到 C++23 才会出现。

我在下面写了一个简单的程序,它使用 range-v3 输出“1 2 2 3 3 3”。我尝试混合使用 range-v3 View 和来自 ranges 的 View ,但没有成功。 'ranges' 库似乎希望 View 继承自 view_interface,因此我创建了一个 wrap_view_t 来帮助实现这一点(未显示)。要查看工作范围-v3 代码,请注释该行:

//#define MIX_RANGES_WITH_RANGE_V3

代码如下:

#include <iostream>

#define MIX_RANGES_WITH_RANGE_V3

#ifdef MIX_RANGES_WITH_RANGE_V3
#include <ranges>
namespace test { using namespace std::ranges; }
#else
#include <range/v3/view/all.hpp>
#include <range/v3/view/interface.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/take_while.hpp>
#include <range/v3/view/transform.hpp>
namespace test { using namespace ranges; }
#endif
#include <range/v3/view/generate.hpp>
#include <range/v3/view/cache1.hpp>


auto my_view()
{
auto gen{ ranges::views::generate( [ n = 0 ]() mutable
{
return std::vector< int >( ++n, n );
} )
| ranges::views::cache1
};

return std::move( gen )
| test::views::take_while( []( auto const & value ) { return value.size() < 4; } )
| test::views::transform( []( auto const & value )
{ return test::views::all( value ); } );

}

int main()
{
for( auto const value : my_view() | test::views::join )
{
std::cout <<value <<" ";
}
std::cout <<std::endl;

return 0;
}

编译错误

test_rng/test_rng_simpler.C: In function ‘auto my_view()’:
test_rng/test_rng_simpler.C:28:3: error: no match for ‘operator|’ (operand types are ‘std::remove_reference<ranges::cache1_view<ranges::generate_view<my_view()::<lambda()> > >&>::type’ {aka ‘ranges::cache1_view<ranges::generate_view<my_view()::<lambda()> > >’} and ‘std::ranges::views::__adaptor::_RangeAdaptorClosure<std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()<{my_view()::<lambda(const auto:16&)>}>::<lambda(_Range&&)> >’)
27 | return std::move( gen )
| ~~~~~~~~~~~~~~~~
| |
| std::remove_reference<ranges::cache1_view<ranges::generate_view<my_view()::<lambda()> > >&>::type {aka ranges::cache1_view<ranges::generate_view<my_view()::<lambda()> > >}
28 | | test::views::take_while( []( auto const & value ) { return value.size() < 4; } )
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| std::ranges::views::__adaptor::_RangeAdaptorClosure<std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()<{my_view()::<lambda(const auto:16&)>}>::<lambda(_Range&&)> >
In file included from test_rng/test_rng_simpler.C:6:
/opt/rh/devtoolset-10/root/usr/include/c++/10/ranges:1180:4: note: candidate: ‘template<class _Tp> constexpr auto std::ranges::views::__adaptor::operator|(const std::ranges::views::__adaptor::_RangeAdaptorClosure<_Callable>&, const std::ranges::views::__adaptor::_RangeAdaptorClosure<std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()<{my_view()::<lambda(const auto:16&)>}>::<lambda(_Range&&)> >&)’
1180 | operator|(const _RangeAdaptorClosure<_Tp>& __x,
| ^~~~~~~~
/opt/rh/devtoolset-10/root/usr/include/c++/10/ranges:1180:4: note: template argument deduction/substitution failed:
test_rng/test_rng_simpler.C:28:84: note: ‘std::remove_reference<ranges::cache1_view<ranges::generate_view<my_view()::<lambda()> > >&>::type’ {aka ‘ranges::cache1_view<ranges::generate_view<my_view()::<lambda()> > >’} is not derived from ‘const std::ranges::views::__adaptor::_RangeAdaptorClosure<_Callable>’
28 | | test::views::take_while( []( auto const & value ) { return value.size() < 4; } )
| ^
In file included from test_rng/test_rng_simpler.C:6:
/opt/rh/devtoolset-10/root/usr/include/c++/10/ranges:1175:4: note: candidate: ‘constexpr auto std::ranges::views::__adaptor::operator|(_Range&&, const std::ranges::views::__adaptor::_RangeAdaptorClosure<_Callable>&) [with _Range = ranges::cache1_view<ranges::generate_view<my_view()::<lambda()> > >; _Callable = std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()<{my_view()::<lambda(const auto:16&)>}>::<lambda(_Range&&)>]’
1175 | operator|(_Range&& __r, const _RangeAdaptorClosure& __o)
| ^~~~~~~~
/opt/rh/devtoolset-10/root/usr/include/c++/10/ranges:1175:4: note: constraints not satisfied
test_rng/test_rng_simpler.C: In instantiation of ‘constexpr auto std::ranges::views::__adaptor::operator|(_Range&&, const std::ranges::views::__adaptor::_RangeAdaptorClosure<_Callable>&) [with _Range = ranges::cache1_view<ranges::generate_view<my_view()::<lambda()> > >; _Callable = std::ranges::views::__adaptor::_RangeAdaptor<_Callable>::operator()<{my_view()::<lambda(const auto:16&)>}>::<lambda(_Range&&)>]’:
test_rng/test_rng_simpler.C:28:84: required from here
/opt/rh/devtoolset-10/root/usr/include/c++/10/ranges:78:13: required for the satisfaction of ‘viewable_range<_Range>’ [with _Range = ranges::cache1_view<ranges::generate_view<my_view::._anon_131> >]
/opt/rh/devtoolset-10/root/usr/include/c++/10/ranges:79:31: note: no operand of the disjunction is satisfied
79 | && (borrowed_range<_Tp> || view<remove_cvref_t<_Tp>>);
| ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

也许我应该坚持使用“range-v3”,直到我需要的所有 View 都在“范围”中可用为止?

最佳答案

由于 range-v3 中的适配器不继承 ranges::view_interface,因此它们不是 view在标准中。为了避免悬挂的危险,该标准禁止传递不对 borrowed_range 建模的 rangeview(在您的示例中为 gen)进入管道。

你需要做的是使用一个临时变量接受generate使其成为左值range,这样borrowed_range就可以满足,可以使用标准管道操作:

#include <iostream>
#include <ranges>
#include <range/v3/view/generate.hpp>
#include <range/v3/view/cache1.hpp>

int main() {
auto gen = ranges::views::generate([n = 0]() mutable {
return std::vector<int>(++n, n);
}) | ranges::views::cache1;

for (auto const value : gen
| std::views::take_while([](auto const& value) { return value.size() < 4; })
| std::views::transform([](auto const& value) { return std::views::all(value); })
| std::views::join) {
std::cout <<value <<" ";
}
}

Demo.

关于c++ - 是否可以将 'range-v3' include(s) 与 c++ 'ranges' include 混合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69441401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com