I am trying to make a generic graph structure, but I am running into this circular dependency between vertices and edges. I define my Vertex and Edge classes like so:
template<typename EdgeType>
struct Vertex {
std::vector<EdgeType> successors;
};
template<typename EdgeCostType, typename VertexWrapper>
struct Edge {
EdgeCostType cost;
VertexWrapper source;
VertexWrapper dest;
};
I would like to instantiate it with something like Vertex<Edge<int, std::shared_ptr<decltype(v)>>> v;
, but I obviously cannot. What can I do to resolve this circular dependency?
Edit:
I think what this problem boils down to is using the current template as a template parameter to one of the template parameters of the current template, e.g. how to do something like this:
template<typename VertexWrapper>
struct Vertex {
std::vector<pair<int, VertexWrapper<Vertex>>> successors;
};
With template template parameter, you can do something like:
template<typename EdgeType>
struct Vertex {
std::vector<EdgeType> successors;
};
template<typename EdgeCostType, template <typename> class VertexWrapper>
struct Edge {
EdgeCostType cost;
VertexWrapper<Edge> source;
VertexWrapper<Edge> dest;
};
using myEdge = Edge<double, Vertex>;
using myVertex = Vertex<myEdge>;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With