使用当前模板作为模板参数之一的模板参数
Use Current Template as a Template Parameter to one of the Template Parameters
我正在尝试创建一个通用的图形结构,但我 运行 陷入了顶点和边之间的这种循环依赖关系。我这样定义顶点和边 类:
template<typename EdgeType>
struct Vertex {
std::vector<EdgeType> successors;
};
template<typename EdgeCostType, typename VertexWrapper>
struct Edge {
EdgeCostType cost;
VertexWrapper source;
VertexWrapper dest;
};
我想用 Vertex<Edge<int, std::shared_ptr<decltype(v)>>> v;
之类的东西实例化它,但我显然不能。我该怎么做才能解决这种循环依赖?
编辑:
我认为这个问题归结为使用当前模板作为当前模板的模板参数之一的模板参数,例如如何做这样的事情:
template<typename VertexWrapper>
struct Vertex {
std::vector<pair<int, VertexWrapper<Vertex>>> successors;
};
使用模板模板参数,您可以执行以下操作:
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>;
你的很简单..(但我不知道如何打印和初始化成员向量)
#include <vector>
#include <iostream>
using namespace std;
template<typename EdgeType>
struct Vertex {
vector<EdgeType> successors;
};
template<typename EdgeCostType, typename VertexWrapper>
struct Edge {
EdgeCostType cost;
VertexWrapper source;
VertexWrapper dest;
};
int main (){
Vertex<int> vertx = {{5}};
Edge<int, decltype(vertx)> ed = {7};
cout<< ed.cost <<"\n"; //cout<< ed.dest.successors<<"\n"; // not work, ask help!
}
会起作用,但它确实会限制您只能加倍。例如,如果你想要一个更灵活的接受 int
s 的,你也有这个选项:
template<class T>
using myEdge = Edge<T, Vertex>;
template<class T>
using myVertex = Vertex<myEdge<T>>;
这将允许您使用其他类型的数字,如果出于某种原因您需要 shorthand。然后使用双打看起来像这样:
myVertex<double> v;
我正在尝试创建一个通用的图形结构,但我 运行 陷入了顶点和边之间的这种循环依赖关系。我这样定义顶点和边 类:
template<typename EdgeType>
struct Vertex {
std::vector<EdgeType> successors;
};
template<typename EdgeCostType, typename VertexWrapper>
struct Edge {
EdgeCostType cost;
VertexWrapper source;
VertexWrapper dest;
};
我想用 Vertex<Edge<int, std::shared_ptr<decltype(v)>>> v;
之类的东西实例化它,但我显然不能。我该怎么做才能解决这种循环依赖?
编辑:
我认为这个问题归结为使用当前模板作为当前模板的模板参数之一的模板参数,例如如何做这样的事情:
template<typename VertexWrapper>
struct Vertex {
std::vector<pair<int, VertexWrapper<Vertex>>> successors;
};
使用模板模板参数,您可以执行以下操作:
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>;
你的很简单..(但我不知道如何打印和初始化成员向量)
#include <vector>
#include <iostream>
using namespace std;
template<typename EdgeType>
struct Vertex {
vector<EdgeType> successors;
};
template<typename EdgeCostType, typename VertexWrapper>
struct Edge {
EdgeCostType cost;
VertexWrapper source;
VertexWrapper dest;
};
int main (){
Vertex<int> vertx = {{5}};
Edge<int, decltype(vertx)> ed = {7};
cout<< ed.cost <<"\n"; //cout<< ed.dest.successors<<"\n"; // not work, ask help!
}
int
s 的,你也有这个选项:
template<class T>
using myEdge = Edge<T, Vertex>;
template<class T>
using myVertex = Vertex<myEdge<T>>;
这将允许您使用其他类型的数字,如果出于某种原因您需要 shorthand。然后使用双打看起来像这样:
myVertex<double> v;