【问题标题】:boost::graph custom weight type: numeric_limits necessary?boost::graph 自定义权重类型:numeric_limits 必要吗?
【发布时间】:2026-02-19 18:15:01
【问题描述】:

我在 boost 图中有一个自定义边属性 (MyWeight),并希望应用 Dijkstra 最短路径搜索。

在 boost::operators 的帮助下,我的体重类型是可加、可减、小于可比和等式可比。

因为我知道搜索必须从某事开始,所以我需要一个相当于零的值,并找出一个节点是否可以到达,这是一个非常大的东西。

MyWeight weight_zero(...), weight_inf(...);

// now, weight_zero is less than all other weights, weight_inf greater than all other weights.

boost::dijkstra_shortest_paths( G, target_idx, boost::predecessor_map(&predecessors[0])
    .distance_map(&distances[0])
    .distance_inf(weight_inf)
    .distance_zero(weight_zero)
            );

这是我使用自定义重量的原则。它使用 gcc 4.8.1 编译(并且似乎可以正常工作),但使用 gcc 4.7.3 我得到以下错误(有点缩短):

/usr/include/c++/4.7/limits:-1: In instantiation of 'static constexpr _Tp std::numeric_limits<_Tp>::max() [with _Tp = MyWeight<2, MyEvaluator>]':
/usr/include/c++/4.7/limits:313: error: no matching function for call to 'MyWeight<2, MyEvaluator>::MyWeight(int)'

MyWeight其实是一个模板)

我将此消息解释为“您的类型没有 std::numeric_limit”,另一方面,我告诉 BGL 使用哪个极值,但不明白为什么它会尝试调用数字限制。我认为在这里尝试的最佳方法是让numeric_limits 识别我的自定义数据类型。

谁能指点我如何做到这一点?

我找到了第一个提示here,但该示例使用了digitsdigits10 之类的成员,我想知道它们的来源。

编辑

This 是我的一个例子。无法编译,错误见上。

【问题讨论】:

  • 如果还没有,您可能想要report it as boost bug
  • 我会这样做的。我创建了一个拒绝编译的示例程序,目前我无法使用其他版本进行测试,但它是相同的方案。

标签: c++ boost-graph numeric-limits


【解决方案1】:

这似乎是命名参数包装器中的一个错误(或缺陷;我不确定它是否可修复),它扩展了默认值,即使它们没有被使用。如果使用位置参数重载并手动填写所有参数,则不需要numeric_limits

【讨论】: