gpt4 book ai didi

c++ - 嵌套模板(即模板 >)

转载 作者:搜寻专家 更新时间:2023-10-31 00:55:22 28 4
gpt4 key购买 nike

我需要为我的类获取 2 个类型参数:T1,它是一个具有模板的类,以及 T2,它是 T1 模板的参数。

在我的例子中,一个顶点类型(有 2 个,一个从另一个继承),以及顶点存储的数据类型(在我的例子中是名称/id)。

我希望能够写出这样的东西:

template <   typename VertexType  < typename VertexIDType >    >

(这给了我错误:C2143 语法错误:在“<”之前缺少“,”)

所以我的类(class)会是这样的:

class Graph
{
public:
Graph(const List<VertexType<VertexIDType>>& verticesList);
VertexType<VertexIDType>& getVertexByID(const VertexIDType& ID) const;

private:
List<VertexType<VertexIDType>> vertices;
};

('List' 是我的(不是标准的)链表实现。)

我也试过template <typename VertexType, typename VertexIDType>但后来我在函数 Graph(const List<VertexType<VertexIDType>>& verticesList); 中出错(C2947 期望“>”终止模板参数列表,找到“<”)

还有这个template < typename VertexType < template <typename VertexIDType> > >

(这也给了我错误 C2143)

我真的是那种试图自己解决所有问题的人,但是越来越令人沮丧。我找不到我理解是否/如何在我的代码中实现的答案。我现在已经完成了 OOP (c++) 类(class)。我对模板有一些经验。我已经编写了成功获取 1 或 2 个参数的模板,但没有像这样。

请帮我解决这个问题,最好尽可能优雅:)

谢谢。

最佳答案

可以使用模板模板参数:

template <template <typename> class VertexType, typename VertexIDType>
class graph;

graph<MyVertexType, MyVertexIDType> //usage

或者,您可以只获取一个类型并在部分特化中提取 ID 类型:

template <typename Vertex>
class graph;

template <template <typename> class VertexType, typename VertexIDType>
class graph <VertexType<VertexIDType>> {
//...
};

graph<MyVertexType<MyVertexIDType>> //usage

关于c++ - 嵌套模板(即模板 <typename T< typename templateArgumentFor_T >>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42248493/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com