使用自定义点类型在Boost 1.80.0中出现Boost Geometry编译错误
Boost 1.80.0自定义Point类型在
boost::geometry::within()中编译失败的问题解决 问题概述
使用Boost 1.80.0版本时,按照官方文档完成自定义point类型的注册后,调用boost::geometry::within(multiline, polygon)出现编译错误,核心报错包括:
std::vector<boost::geometry::model::point<...>>::push_back(const point&)无匹配函数boost::geometry::less<...>无法比较boost::geometry::model::point与自定义point
但在Boost 1.79.0版本编译正常,替换为Boost官方model::point类型也能正常编译。
错误原因分析
这是Boost 1.80.0中boost::geometry模块的回归问题:内部逻辑(如boundary_checker.hpp中的边界检查代码)错误地将自定义point类型与官方model::point类型混用,导致类型不匹配。官方文档未提及该版本的相关变更,属于未被记录的兼容性问题。
解决方案
临时规避方案(无需更换Boost版本)
方案1:添加自定义Point到官方Point的隐式转换
在自定义point结构体中添加隐式转换运算符,适配内部逻辑的类型要求:
struct point { double x, y; point(double x = 0, double y = 0) : x(x), y(y) {} // 隐式转换为Boost官方2D笛卡尔坐标Point operator boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian>() const { return {x, y}; } };
方案2:为自定义Point添加比较运算符
添加operator<让Boost的比较器能正确处理自定义point类型,解决比较不匹配的错误:
struct point { double x, y; point(double x = 0, double y = 0) : x(x), y(y) {} bool operator<(const point& rhs) const { return std::tie(x, y) < std::tie(rhs.x, rhs.y); } };
根本解决方法
- 降级Boost版本:切换回Boost 1.79.0,该版本不存在此兼容性问题
- 升级Boost版本:尝试升级到Boost 1.81.0及以上版本,确认是否已修复该回归问题
- 提交Bug报告:向Boost官方提交包含测试代码和错误信息的Bug报告,推动问题修复
验证测试
添加上述任一规避方案后,原测试代码即可在Boost 1.80.0中正常编译运行。
内容的提问来源于stack exchange,提问作者Benjamin
相关产品推荐
相关产品推荐

