指向作为类参数的模板类的指针
Pointer to template class as that class parameter
基本上,当试图通过邻接列表实现图时,我在定义图节点时遇到了困难:
template <
typename Vertex,
typename Edge,
typename EdgeLink = std::pair< Edge, GraphNode* >,
typename Alloc = std::allocator< EdgeLink >
>
class GraphNode
{
public:
Vertex vertex;
std::list< EdgeLink, Alloc > neighbours;
};
我意识到我不能给GraphNode模板指针提供参数,因为它们还没有定义。我对c++模板大师们的问题是:在这种情况下使用了什么技术?
谢谢。
精确分配器并不需要精确分配器的用途。例如,在std::list<T>
中,传递的分配器是std::allocator<T>
,而list
将分配_ListNode<T>
(实现定义)。这是因为分配器需要提供rebind
机制。
template <
typename Vertex,
typename Edge,
typename Allocator = std::allocator<void*>
>
class GraphNode
{
public:
typedef GraphNode<Vertex, Edge, Allocator> NodeType;
typedef std::pair< Edge, NodeType* > LinkType;
typedef typename Allocator::template rebind<LinkType>::other AllocatorType;
Vertex vertex;
std::list< LinkType, AllocatorType > neighbours;
};
在ideone的行动中。
请注意,即使list
本身将执行rebind
,您仍然应该执行它,因为分配器类型reference
和pointer
(及其const版本)将在list
内作为typedef
进行提取。
编辑:允许容器规范。
这很棘手,因为不幸的是,分配器只在GraphNode
中定义一次,因此只需要将其传递给类中的容器,因此不能在外部的模板中使用它。
这意味着使用模板-模板参数,因此我们需要"修复"arity。由于vector
和list
都只取两个参数,我们在这里很幸运,但它可能并不总是成立。。。幸运的是,C++11允许模板别名,这对用户来说不会太苛刻。
template <
typename Vertex,
typename Edge,
template <typename, typename> class Container = std::vector,
typename Allocator = std::allocator<void*>
>
class GraphNode
{
public:
typedef GraphNode<Vertex, Edge, Container, Allocator> NodeType;
typedef std::pair< Edge, NodeType* > LinkType;
typedef typename Allocator::template rebind<LinkType>::other AllocatorType;
typedef Container<LinkType, AllocatorType> NeighboursType;
Vertex vertex;
NeighboursType neighbours;
};
这可以调用,以便:
GraphNode<std::string, int>
GraphNode<std::string, int, std::list>
GraphNode<std::string, int, std::vector>
演示。
如果EdgeLink将位于GraphNode的"内部",最好不要将其声明为模板参数。相反,使用这种方法:
template <
typename Vertex,
typename Edge
>
class GraphNode
{
public:
typedef GraphNode<Vertex, Edge> NodeType;
typedef std::pair< Edge, NodeType* > LinkType;
typedef std::allocator< Linktype > AllocType;
Vertex vertex;
std::list< LinkType, AllocType > neighbours;
};
相关文章:
- 为函数定义符号不明确的指针参数
- 构造函数 (C++) 中的 char 指针参数存在问题
- 按引用调用与按指针参数调用的差异 前递增和后递增
- 为什么具有指针参数的成员函数需要指向指针的指针?
- 通过引用函数传递指针参数是什么意思?
- 从double到double*的指针参数
- void 函数中的指针参数返回不一致的值
- 无法向上转换指针到指针参数
- 将数组动态分配到具有指针参数的函数中
- 模板函数指针参数与构造函数参数
- 使用通用函数指针参数化函数模板的简洁方法
- 如何为C++字符串分配空指针参数?
- C++带有指针参数的函数
- 指针参数的默认值
- 将常量添加到函数模板指针参数
- 如何传递unique_ptr<T>代替原始*输出*指针参数?
- 不带星号的函数指针参数
- 功能指针参数参数转换为const
- 将指针参数传递给双指针参数
- 如何调试指针参数是否通过函数修改