【问题标题】:Use lambda in the boost::geometry::for_each_point在 boost::geometry::for_each_point 中使用 lambda
【发布时间】:2014-08-20 00:18:44
【问题描述】:

我想将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 正确性存在问题。但我真的不知道,关于const 的这段代码有什么问题。谁能解释一下,我的错误在哪里,并且修复它很热?

【问题讨论】:

    标签: c++ lambda boost-geometry


    【解决方案1】:

    问题是bg::append 的第一个参数应该引用result,而result 是由您的lambda 中的值捕获的。将捕获更改为

    [&result](point3d point)
    

    对于按值捕获,它会报告有关 const 正确性的错误,因为您将临时对象作为期望非 const 引用的参数传递。如果是 const 则不会报错。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多