在boost::geometry::for_each_point中使用lambda
Use lambda in the boost::geometry::for_each_point
我想将boost::model::polygon
转换为boost::model::multi_point
,下面是我的实现:
namespace bg = boost::geometry;
typedef bg::model::point<double, 3, bg::cs::cartesian> point3d;
bg::model::multi_point<point3d> result;
std::function<void(point3d)> appendPoint = [result](point3d point){
bg::append(result, point);
};
bg::for_each_point(polygon, appendPoint);
但是这段代码给了我一个错误:
error: passing ‘boost::remove_reference<const boost::geometry::model::multi_point<boost::geometry::model::point<double, 3ul, boost::geometry::cs::cartesian> > >::type {aka const boost::geometry::model::multi_point<boost::geometry::model::point<double, 3ul, boost::geometry::cs::cartesian> >}’ as ‘this’ argument of ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = boost::geometry::model::point<double, 3ul, boost::geometry::cs::cartesian>; _Alloc = std::allocator<boost::geometry::model::point<double, 3ul, boost::geometry::cs::cartesian> >; std::vector<_Tp, _Alloc>::value_type = boost::geometry::model::point<double, 3ul, boost::geometry::cs::cartesian>]’ discards qualifiers [-fpermissive]
如果我理解正确的话,这类错误表明常量正确性存在问题。但是我真的不知道,这个关于const
的代码有什么问题。有谁能告诉我,我的错误在哪里,如何补救?
问题是bg::append
的第一个参数应该引用result
,而result
是通过lambda中的值捕获的。将捕获更改为
[&result](point3d point)
对于按值捕获,它报告关于const正确性的错误,因为您将临时对象作为参数传递,期望非const引用。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 可组合的lambda/std::函数与std::可选
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 如何将lambda作为模板类的成员函数参数
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 在 lambda 捕获中声明的变量的类型推导
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 模板函数指针和lambda
- 两组使用lambda函数的大括号
- 使lambda不可复制/不可移动
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- AWS Lambda C++运行时权限被拒绝
- 捕获lambda中的std::数组
- 这 4 个 lambda 表达式之间有什么区别?
- 在实现文件中使用头文件的通用 lambda
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?