c++ - 将当前模板用作模板参数之一的模板参数
问题描述
我正在尝试制作一个通用的图结构,但我遇到了顶点和边之间的这种循环依赖。我像这样定义我的 Vertex 和 Edge 类:
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>;
推荐阅读
- ios - React Native Push Notifications 未出现在设置中
- java - 遍历每个图节点并比较java
- mysql - SQL:如何为给定可变长度行名数组的行添加列
- sql - 如果 SQL Server 中最近 3 个月的任何一个值大于 3,则查询以显示数据
- python - 无法使用 Selenium WebDriver Python 在 iframe 中找到元素
- node.js - 从 Loopback 4 模型定义一个 MongoDB 上限集合
- vb.net - 成功过滤其他记录后如何从水晶视图中删除选择公式以查看所有记录
- python - 我应该能够使用 gspread 和 Python 来使用公式/函数更新 Google 表格中的单元格吗?
- javascript - 我的地图函数不返回任何信息?
- python - 根据值是否为空来创建熊猫数据框列