什么是(template)rebind<>做? [英] what does (template) rebind<> do?
问题描述
试图了解标准库如何实际实现我正在检查visual studio中的所有容器。这里我看到一些好奇的结构:
在一些 std :: list的基类发现以下typedef
typedef typename _Alloc :: template rebind< _Ty> :: other _Alty;
其中_Alloc与分配器模板参数(以及_包含的类型)相对应。我找不到这个关键字的很好的解释。最好的thign我发现到目前为止,它是分配器接口的一部分。虽然即使 cppreference 在解释此问题上也不是很好。
这个模板rebind<>
是什么?
_Alloc
模板用于获得某种类型的对象。容器可以具有分配不同类型的对象的内部需要。例如,当你有 std :: list< T,A>
时,分配器 A
T
的对象,但是 std :: list
实际上需要分配某个节点类型的对象。调用节点类型 _Ty
, std :: list< T,A>
需要获取分配器 _Ty
使用由 A
提供的分配机制的对象。使用
typename _A :: template rebind< _Ty> :: other
pre>
指定相应的类型。现在,在此声明中有一些语法烦恼:
- 由于
rebind
_A
和_A
的成员模板是模板参数,rebind
成为依赖名称。要指示依赖名称是模板,需要使用template
作为前缀。如果没有模板
关键字,则<
将被视为小于运算符。
- 名称
其他
也取决于模板参数,即它也是一个依赖名称。要指示依赖名称是一个类型,需要typename
关键字。
trying to learn more about how the standard library is actually implemented I'm inspecting all containers in visual studio.. Here I see some curious structure:
In some base class of a
std::list<>
The following typedef is foundtypedef typename _Alloc::template rebind<_Ty>::other _Alty;
Where "_Alloc" corresponds with the allocator template argument (and _Ty the contained type). I have trouble finding a good explanation of this "keyword". Best thign I've found so far is that it is part of the allocator interface. Though even cppreference isn't very good in explaining this.
What does this
template rebind<>
do? And why is it necessary at that location?解决方案The
_Alloc
template is used to obtain objects of some type. The container may have an internal need to allocate objects of a different type. For example, when you have astd::list<T, A>
, the allocatorA
is meant to allocate objects of typeT
but thestd::list<T, A>
actually needs to allocate objects of some node type. Calling the node type_Ty
, thestd::list<T, A>
needs to get hold of an allocator for_Ty
objects which is using the allocation mechanism provided byA
. Usingtypename _A::template rebind<_Ty>::other
specifies the corresponding type. Now, there are a few syntactic annoyances in this declaration:
- Since
rebind
is a member template of_A
and_A
is a template argument, therebind
becomes a dependent name. To indicate that a dependent name is a template, it needs to be prefixed bytemplate
. Without thetemplate
keyword the<
would be considered to be the less-than operator.- The name
other
also depends on a template argument, i.e., it is also a dependent name. To indicate that a dependent name is a type, thetypename
keyword is needed.
这篇关于什么是(template)rebind<>做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!