【发布时间】:2015-01-28 13:30:25
【问题描述】:
我正在编写一个 Graph 类,我想为同一个类提供多种类型的图。更具体地说,我想考虑有向图或无向图,以及顶点(和边)具有值的可能性。
现在,我的班级看起来像这样。
template<typename VertexValue = void, typename EdgeValue = void, bool directed = false>
class Graph {
public:
struct Vertex;
using Edge = Vertex::Edge;
Graph() = default;
//void addVertex(const VertexValue& vertex);
private:
std::vector<Vertex> m_vertices;
};
为方便起见,我将跳过与边缘实现相关的任何内容。
我现在要做的是“专门化” Vertex 类以提供(或不提供)一个值,以及取决于图形是否定向的不同方法。我希望能够执行以下操作(注意:Vertex 方法仅用于说明目的,Vertex 的有向和无向版本具有不同的方法)。
template<typename VertexValue = void, bool directed = false>
class Graph {
public:
struct Vertex;
private:
std::vector<Vertex> m_vertices;
};
// Directed, with value
template<typename VertexValue, bool directed>
struct Graph<VertexValue, directed>::Vertex {
using ValueType = VertexValue;
EdgeIterator inEdges();
EdgeIterator outEdges();
ValueType value;
};
// Directed, without value
template<bool directed>
struct Graph<void, directed>::Vertex {
using ValueType = void;
EdgeIterator inEdges();
EdgeIterator outEdges();
};
// Undirected, with value
template<typename VertexValue>
struct Graph<VertexValue, false>::Vertex {
using ValueType = VertexValue;
EdgeIterator edges();
ValueType value;
};
// Undirected, without value
template<>
struct Graph<void, false>::Vertex {
using ValueType = void;
EdgeIterator edges();
};
不幸的是,我必须专门化整个 Graph 类才能做到这一点。所以我尝试了其他方法:将 Vertex 类作为模板,并转发外部模板参数。
template<typename VertexValue = void, bool directed = false>
class Graph {
public:
template<typename Value, bool, typename dummy = void>
struct Vertex;
private:
std::vector<Vertex<VertexValue, directed>> m_vertices;
};
// Directed, with value
template<typename VertexValue, bool directed>
template<typename Value, bool, typename dummy>
struct Graph<VertexValue, directed>::Vertex {
using ValueType = Value;
EdgeIterator inEdges();
EdgeIterator outEdges();
ValueType value;
};
// Directed, without value
template<typename VertexValue, bool directed>
template<bool directed_, typename dummy>
struct Graph<VertexValue, directed>::Vertex<void, directed_, dummy> {
using ValueType = void;
EdgeIterator inEdges();
EdgeIterator outEdges();
};
// Undirected, with value
template<typename VertexValue, bool directed>
template<typename Value, typename dummy>
struct Graph<VertexValue, directed>::Vertex<Value, false, dummy> {
using ValueType = Value;
EdgeIterator edges();
ValueType value;
};
// Undirected, without value
template<typename VertexValue, bool directed>
template<typename dummy>
struct Graph<VertexValue, directed>::Vertex<void, false, dummy> {
using ValueType = void;
EdgeIterator edges();
};
这行得通;但是,我不喜欢转发模板参数的想法,而且它还引入了一个负担,即虚拟模板参数。我还考虑过在 Graph 类之外定义 Vertex 类,如下所示:
template<typename ValueType, bool directed>
class GraphVertex {
EdgeIterator inEdges();
EdgeIterator outEdges();
ValueType value;
};
// Specializations...
template<typename Vertex = GraphVertex<void, false>>
class Graph {
public:
// ...
private:
std::vector<Vertex> m_vertices;
};
...甚至像这样:
template<typename ValueType, bool directed>
class GraphVertex {
EdgeIterator inEdges();
EdgeIterator outEdges();
ValueType value;
};
// Specializations...
template<typename VertexValue = void, bool directed = false>
class Graph {
public:
// ...
private:
std::vector<GraphVertex<VertexValue, directed>> m_vertices;
};
...但我更喜欢嵌套类的想法,其中 Vertex 类实际上是 Graph 类的属性。另外,即使这三个解决方案中的这个解决方案仍然有我的偏好,我也找不到“GraphVertex”的任何其他名称。这让我很困扰。
最后,这是我的问题。有没有其他方法可以做我想做的事情(即提供不同的 Vertex 类 - 或 features - 取决于 Graph 模板参数)?如果是这样,如何做以及有什么好处;如果不是,我应该更喜欢什么,为什么?
【问题讨论】:
-
@IdeaHat 这根本不是重复的,我给出的三个解决方案确实有效(至少我很确定)。我要的是其他解决方案或建议。