boost::graph自定义权重类型:numeric_limits必需

boost::graph custom weight type: numeric_limits necessary?

本文关键字:limits 必需 numeric 权重 graph 自定义 boost 类型      更新时间:2023-10-16

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

在boost的帮助下,我的权重类型是:运算符、可加、可减、小于可比和相等可比。

因为我知道搜索必须从某个东西开始,我需要一个零的等价物,并找出一个节点是否可以到达,一个非常大的节点。

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识别我的自定义数据类型。

有人能告诉我如何做到这一点吗?

我在这里找到了第一个提示,但这个例子使用了digitsdigits10这样的成员,我想知道它们来自哪里。

编辑

这是一个和我一样的例子。无法编译它,有关错误,请参阅上文。

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