Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Current Template as a Template Parameter to one of the Template Parameters

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;
};
like image 917
Simon Berens Avatar asked Dec 31 '19 23:12

Simon Berens


1 Answers

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>;
like image 131
Jarod42 Avatar answered Oct 04 '22 14:10

Jarod42