max*_*max 5 c++ generics boost boost-graph
我知道在通用编程中,算法与容器分离.因此,将通用算法实现为实例方法是没有意义的(相同的算法应该适用于多个具体类;我们不希望它们都从一个ABC继承,因为它会以指数方式增加类的数量).
但是source()
对于Boost图库中的函数,我不明白它为什么是全局函数而不是图类的实例方法.
据我所知,通过阅读BGL源代码,source(e, g)
需要知道图形的实现细节和传递给它的边缘对象; 只知道他们的界面是不够的.
所以source()
不是通用算法.换句话说,它需要知道图形实例的具体类.那么为什么不把它作为实例方法放在同一个类中呢?它不是比制作一个需要为每个被调用的类定制的全局函数更清晰/更少混淆吗?
UPDATE
相关源代码:
// dwa 09/25/00 - needed to be more explicit so reverse_graph would work.
template <class Directed, class Vertex,
class OutEdgeListS,
class VertexListS,
class DirectedS,
class VertexProperty,
class EdgeProperty,
class GraphProperty, class EdgeListS>
inline Vertex
source(const detail::edge_base<Directed,Vertex>& e,
const adjacency_list<OutEdgeListS, VertexListS, DirectedS,
VertexProperty, EdgeProperty, GraphProperty, EdgeListS>&)
{
return e.m_source;
}
namespace boost {
namespace detail {
template <typename Directed, typename Vertex>
struct edge_base
{
inline edge_base() {}
inline edge_base(Vertex s, Vertex d)
: m_source(s), m_target(d) { }
Vertex m_source;
Vertex m_target;
};
}
}
Run Code Online (Sandbox Code Playgroud)
这source(e, g)
不是通用算法.它是接口的一部分,通常在C++中称为概念.作为非成员函数的原因是它可以非侵入地实现.
比如说,你想要std::multimap
实现这个IncidenceGraph
概念.如果图形库需要source()
成为一个成员函数,那么你将失去运气,因为std::multimap
它没有提供一个.