gpt4 book ai didi

c++ - 你如何使用 range-v3 范围制作像 ranges::to() 这样的可管道函数?

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

我的一般问题是如何制作类似 ranges::to<T>() 的东西对于 ranges::to<T>() 的类(class)不起作用?

但具体来说,我正在寻找一种可通过管道构建升压几何体的方法 multi_linestring从线串的 range-v3 范围 View 。有点意外ranges::to在构建 linestring 时才有效但在构建 multi_linestring 时编译失败,如下所示。

namespace r = ranges;
namespace rv = ranges::views;
namespace bg = boost::geometry;

using point = bg::model::point<double, 2, bg::cs::cartesian>;
using polyline = bg::model::linestring<point>;
using polylines = bg::model::multi_linestring<polyline>;

int main() {

std::vector<point> some_points = { {1,1},{2,2},{3,3},{4,4},{5,5} };
auto poly = some_points | r::to<polyline>(); // <- this works

std::vector<std::vector<point>> vec_of_vec_of_pts = {
{{1,1},{2,2},{3,3},{4,4},{5,5}},
{{6,6},{7,7},{8,8}},
{{9,9},{10,10},{11,11},{12,12}}
};

auto polys = vec_of_vec_of_pts | rv::transform(
[](const auto& v) { return v | r::to<polyline>(); }
) | r::to<polylines>(); // <- this does not compile.

return 0;
}

来自 Visual Studio 的特定错误消息是

1>[...]: error C2678: binary '|': no operator found which takes a left-hand operand of type 'ranges::transform_view<ranges::ref_view<std::vector<std::vector<point,std::allocator<point>>,std::allocator<std::vector<point,std::allocator<point>>>>>,Arg>' (or there is no acceptable conversion)
1> with
1> [
1> Arg=main::<lambda_1>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\cstddef(42,27): message : could be 'std::byte std::operator |(const std::byte,const std::byte) noexcept' [found using argument-dependent lookup]
1>C:\libraries\range-v3\include\range\v3\view\any_view.hpp(66,24): message : or 'ranges::category ranges::operator |(ranges::category,ranges::category) noexcept' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include\regex(1191,1): message : or 'std::_Node_flags std::operator |(std::_Node_flags,std::_Node_flags) noexcept' [found using argument-dependent lookup]
1>[...]: message : while trying to match the argument list '(ranges::transform_view<ranges::ref_view<std::vector<std::vector<point,std::allocator<point>>,std::allocator<std::vector<point,std::allocator<point>>>>>,Arg>, ranges::detail::to_container::closure<meta::id<polylines>,ranges::detail::to_container::fn<meta::id<polylines>>>)'
1> with
1> [
1> Arg=main::<lambda_1>
1> ]

我目前的解决方法是使用函数模板来进行转换:

template<typename Rng>
polylines to_polylines(Rng lines) {
polylines polys;
polys.resize(r::distance(lines));
for (auto&& [i, line] : rv::enumerate(lines)) {
polys[i] = line;
}
return polys;
}

我可以像这样使用

auto polys = to_polylines(
vec_of_vec_of_pts |
rv::transform(
[](const auto& v) { return v | r::to<polyline>(); }
)
);

但它不能成为 range-v3 管道的目标。我怎样才能实现像上面这样的可以通过管道传输的东西?

最佳答案

管道只是重载了operator |,你可以自己重载。

struct to_polyline_tag{} to_polylines;

template<typename Range>
polylines opeator | (Range&& lines, to_polyline_tag) {
// the body of your to_polylines()
polylines polys;
polys.resize(r::distance(lines));
for (auto&& [i, line] : rv::enumerate(lines)) {
polys[i] = line;
}
return polys;
}
// use: some_range_like | to_polylines

关于c++ - 你如何使用 range-v3 范围制作像 ranges::to<T>() 这样的可管道函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74074633/

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